你好,游客 登录 注册 搜索
背景:
阅读新闻

运用Struts2.0实现页面中的验证码

[日期:2012-07-30] 来源:Linux社区  作者:jimmy5z [字体:大 中 小]

<2>Struts2.0注册RegisterAction中验证码的逻辑实现

  1. package com.tarena.dang.user.action;  
  2.   
  3. import java.io.*;  
  4.   
  5. import org.apache.struts2.ServletActionContext;  
  6. import com.opensymphony.xwork2.ActionContext;  
  7. import com.tarena.dang.domain.User;  
  8. import com.tarena.dang.user.service.UserService;  
  9. import com.tarena.dang.user.service.UserServiceImpl;  
  10. /** 
  11.  * 注册页面的Action控制器业务实现 
  12.  * @author JimmyZhang 
  13.  * 
  14.  */  
  15. public class RegisterAction {  
  16.     private UserService userService;  
  17.     //使用实体对象简化与表单元素对应关系   
  18.     private User user;  
  19.     private String vcode;  
  20.     //定义与文件域upload赌赢的属性   
  21.     private File upload;//上传文件   
  22.     private String uploadFileName;//上传文件的文件名   
  23.     private String uploadContentType;//上传文件类型   
  24.     public String getVcode() {  
  25.         return vcode;  
  26.     }  
  27.     public void setVcode(String vcode) {  
  28.         this.vcode = vcode;  
  29.     }  
  30.     public User getUser() {  
  31.         return user;  
  32.     }  
  33.     public void setUser(User user) {  
  34.         this.user = user;  
  35.     }  
  36.     public RegisterAction(){  
  37.         userService = new UserServiceImpl();  
  38.         System.out.println("【RegisterAction】调用默认无参构造方法");  
  39.     }  
  40.       
  41.       
  42.     public String execute() throws Exception{  
  43.         //判断用户输入的验证与session中是否一直一致   
  44.         ActionContext ac = ActionContext.getContext();  
  45.         String session_vocode = (String)ac.getSession().get("vcode");  
  46.         if(!vcode.equals(session_vocode)){  
  47.             ac.put("vcode_info", "<img src='/dang/images/wrong.gif'/>验证码不匹配");  
  48.             return "input";  
  49.         }  
  50.         boolean isSuccess = userService.register(user);  
  51.         if(isSuccess){  
  52.             //上传头像,File upload-->/images/user/userName.jpg   
  53.             //得到服务器端的物理路径   
  54.             String path = ServletActionContext  
  55.                     .getServletContext().getRealPath("/images/user/"+user.getUserName()+".jpg");  
  56.             //创建服务器端文件的File对象   
  57.             File dest = new File(path);  
  58.             //upload-->dest   
  59.             InputStream fis = new FileInputStream(upload);  
  60.             OutputStream fos = new FileOutputStream(dest);  
  61.             byte[] b = new byte[1024];  
  62.             int n;  
  63.             while((n=fis.read(b,0,b.length))!=-1){  
  64.                 fos.write(b,0,n);                 
  65.             }  
  66.             fos.close();  
  67.             fis.close();  
  68.             //注册成功   
  69.             ac.put("user", user);  
  70.             return "ok";  
  71.         }else{  
  72.             return "input";  
  73.         }  
  74.           
  75.     }  
  76.     public File getUpload() {  
  77.         return upload;  
  78.     }  
  79.     public void setUpload(File upload) {  
  80.         this.upload = upload;  
  81.     }  
  82.     public String getUploadFileName() {  
  83.         return uploadFileName;  
  84.     }  
  85.     public void setUploadFileName(String uploadFileName) {  
  86.         this.uploadFileName = uploadFileName;  
  87.     }  
  88.     public String getUploadContentType() {  
  89.         return uploadContentType;  
  90.     }  
  91.     public void setUploadContentType(String uploadContentType) {  
  92.         this.uploadContentType = uploadContentType;  
  93.     }  
  94.   
  95.       
  96.       
  97. }  
linux
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/4/16 20:01:09
zhima365.com
很实用