类型转换器

类型转换器的作用:
     把表单传到action的数据在中途中进行更改。

一、局部类型转换器
流程:
表单提交数据-> 调用配置文件properties-> 调用自定义的数据转换类->action接受数据->welcome.jsp输出数据。
login.jsp
< form action = "login.action" method = "post" >
学好: < input type = "text" name = "id" >< br >
时间: < input type = "text" name = "bri" >< br >
< input type = "submit" value = "提交" >
</ form >
LoginAction.java
package com.action;
import java.util.Date;
public class LoginAction {
       private int id ;
       private Date bri ;
       public int getId() {
             return id ;
      }
       public void setId( int id) {
             this . id = id;
      }
       public Date getBri() {
             return bri ;
      }
       public void setBri(Date bri) {
             this . bri = bri;
      }
       public String execute(){
            
            System. out .println( id );
            System. out .println( bri );
             return "success" ;
      }
}
定义类型转换类: DateConverter
package com.converter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateConverter extends DefaultTypeConverter {
       @Override
       public Object convertValue(Object arg0, Class arg1) {
            String str = ((String[])arg0)[0];
            
            System. out .println( "11111" );
             if (arg1==Date. class ){
                   try {
                        SimpleDateFormat sd = new SimpleDateFormat( "yyyy/mm/dd" );
                         return sd.parse(str);
                  } catch (ParseException e) {
                         // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
            }
         return null ;
      }
}
配置文件:LoginAction-conversion.properties
bri= com.converter.DateConverter //数据转换类的具体路径
该配置文件必须在action的同级目录下,且名称必须为:action名-conversion.properties。

注意:
1、定义类型转换类时,使用Date类型是,会自动导入 java.sql.Date包,要改为: java.util.Date。
2、在实现抽象类时,Object类型参数是个数组。

二、全局类型转换器
相对于局部类型转换器, 全局 类型转换器只需要更改properties配置文件即可。
新建配置文件必须在src目录下,且名称必须为: xwork-conversion.properties
配置文件: xwork-conversion.properties
java.util.Date=com.converter.DateConverter

三、转换器中异常信息的处理
修改转换器代码:
package com.converter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import com.opensymphony.xwork2.conversion.TypeConversionException;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateConverter extends DefaultTypeConverter{
       @Override
       public Object convertValue(Object arg0, Class arg1) {
            String str = ((String[])arg0)[0];

            System. out .println( "11111" );
             if (arg1==Date. class ){           
                   try {
                        SimpleDateFormat sd = new SimpleDateFormat( "yyyy/mm/dd" );
                         if (!str.matches( "^\\d{4}/\\d{2}/\\d{2}$" )){
                               throw new TypeConversionException();
                        }
                         return sd.parse(str);
                  } catch (ParseException e) {
                         // TODO Auto-generated catch block
                        e.printStackTrace();
                  }
            }      
             return null ;
      }
}
修改action继承关系:
package com.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
       private int id ;
       private Date bri ;
       public int getId() {
             return id ;
      }
       public void setId( int id) {
             this . id = id;
      }
       public Date getBri() {
             return bri ;
      }
       public void setBri(Date bri) {
             this . bri = bri;
      }
       public String execute(){
            System. out .println( id );
            System. out .println( bri );
             return "success" ;
      }
}
修改表单页面,添加错误提示信息:
${fieldErrors.bri[0] }
< form action = "login.action" method = "post" >
学好: < input type = "text" name = "id" >< br >
时间: < input type = "text" name = "bri" >< br >
< input type = "submit" value = "提交" >
</ form >


欢迎大家来提问或者提出意见。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值