• Struts2类型转换


         假设有如下的输入界面:

    2016-12-04_19-43-53

        在上面输入abc,xyz模式的字符串,其中abc作为用户的用户名,xyz作为用户的密码,系统希望将上边的字符串转换成一个User实例。

         1、LoginAction.java

      1: package cc.openhome;
    
      2: import com.opensymphony.xwork2.Action;
    
      3: import com.opensymphony.xwork2.ActionSupport;
    
      4: public class LoginAction extends ActionSupport implements Action {
    
      5:   private User user;
    
      6:   private String tip;
    
      7:   
    
      8:   public User getUser() {
    
      9:     return user;
    
     10:   }
    
     11: 
    
     12:   public void setUser(User user) {
    
     13:     this.user = user;
    
     14:   }
    
     15: 
    
     16:   public String getTip() {
    
     17:     return tip;
    
     18:   }
    
     19: 
    
     20:   public void setTip(String tip) {
    
     21:     this.tip = tip;
    
     22:   }
    
     23: 
    
     24:   public String execute() throws Exception {
    
     25:     // TODO Auto-generated method stub
    
     26:     if(getUser().getName().equals("nihao") && getUser().getPass().equals("ni")){
    
     27:       setTip("转换成功");
    
     28:       return SUCCESS;
    
     29:     }else{
    
     30:       System.out.println("失败!");
    
     31:       setTip("失败成功");
    
     32:       return ERROR;
    
     33:     }
    
     34:   }
    
     35: }

        2、User.java

      1: package cc.openhome;
    
      2: public class User {
    
      3:   private String name;
    
      4:   private String pass;
    
      5:   public String getName() {
    
      6:     return name;
    
      7:   }
    
      8:   public void setName(String name) {
    
      9:     this.name = name;
    
     10:   }
    
     11:   public String getPass() {
    
     12:     return pass;
    
     13:   }
    
     14:   public void setPass(String pass) {
    
     15:     this.pass = pass;
    
     16:   }
    
     17: }

        3、实现类型转换器UserConverter.java

      1: package cc.openhome;
    
      2: import java.util.Map;
    
      3: import ognl.DefaultTypeConverter;
    
      4: public class UserConverter extends DefaultTypeConverter {  
    
      5:   //类型转换器必须重写convertValue的方法,该方法需要完成双向转换。
    
      6:   public Object convertFormString(Map context, String[] values, Class toType ){
    
      7:     System.out.println("此方法执行了、、、、、、");
    
      8:     //将需要将字符串向User类型转换时  
    
      9:     User user=new User();
    
     10:     String[] userValue= values[0].split(",");
    
     11:     user.setName(userValue[0]);
    
     12:     user.setPass(userValue[1]);
    
     13:     return user;  
    
     14:   }
    
     15:   public String convertToString(Map context, Object o){
    
     16:     User user=(User) o;
    
     17:     return "<"+user.getName()+","+user.getPass()+">";
    
     18:   }
    
     19: 
    
     20: }
    
     21: 

        4、定义一个局部类型转换器

            建立一个文件LoginAction-conversion.properties,该文件由key-value对组成。

      1: user=cc.openhome.UserConverter

        5、运行结果

    2016-12-04_19-27-30    6、全局类型转换器

            假如输入页面变为如下:用户信息和客人信息要转换成User类型,生日参数要转换成Date类型。

    2016-12-04_19-30-50     更新后的LoginAction.java

      1: package cc.openhome;
    
      2: 
    
      3: import java.util.Date;
    
      4: 
    
      5: import com.opensymphony.xwork2.Action;
    
      6: import com.opensymphony.xwork2.ActionSupport;
    
      7: public class LoginAction extends ActionSupport implements Action {
    
      8:   private User user;
    
      9:   private String tip;
    
     10:   private User customer;
    
     11:   private Date birth;  
    
     12:   public User getCustomer() {
    
     13:     return customer;
    
     14:   }
    
     15:   public void setCustomer(User customer) {
    
     16:     this.customer = customer;
    
     17:   }
    
     18:   public Date getBirth() {
    
     19:     return birth;
    
     20:   }
    
     21:   public void setBirth(Date birth) {
    
     22:     this.birth = birth;
    
     23:   }
    
     24:   public User getUser() {
    
     25:     return user;
    
     26:   }
    
     27:   public void setUser(User user) {
    
     28:     this.user = user;
    
     29:   }
    
     30:   public String getTip() {
    
     31:     return tip;
    
     32:   }
    
     33:   public void setTip(String tip) {
    
     34:     this.tip = tip;
    
     35:   }
    
     36:   public String execute() throws Exception {
    
     37:     // TODO Auto-generated method stub    
    
     38:     if(getUser().getName().equals("nihao") && getUser().getPass().equals("ni")){
    
     39:       setTip("转换成功");
    
     40:       return SUCCESS;
    
     41:     }else{
    
     42:       setTip("失败成功");
    
     43:       return ERROR;
    
     44:     }
    
     45:   }
    
     46: }

        7、提供全局转换器文件

        xwork-conversion.properties

      1: cc.openhome.User=cc.openhome.UserConverter

        8、运行结果

            用户信息框输入scott,tiger ,客人信息输入框输入”abc,xyz”,在生日框中输入”2003-02-12”,则

    2016-12-04_19-43-08

  • 相关阅读:
    导出csv 随便记录
    mysql 5.7.27 win64 安装步骤
    mysql 报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executin
    Mysql 基本命令
    遇到summernote编辑器的坑,解决上传图片性能问题。
    ajax提交json格式数组
    idea导入eclipse项目,tomcat启动报错org.apache.catalina.deploy.WebXml addFilter
    Spring(一)
    MyBatis源码分析(一)--SqlSessionFactory的生成
    MyBatis(八)--逆向工厂(generatorSqlmapCustom)
  • 原文地址:https://www.cnblogs.com/liuzhongfeng/p/6131627.html
Copyright © 2020-2023  润新知