struts2 类型转化(typeConverter)

当需要特定类型的参数时,需要类型转换.在某些情况下,要使用ModelDriven和Preparable接口,情况又有发生不同.

struts2中的Action实现了ModelDriven和Preparable接口,Action的属性就要写明gett/sett方法,否则仍然不能转换成功

类型转换的配置也分三种,Action,model,全.名称分别是:EmployeeAction-conversion.properties,Employee-conversion.properties,xwork-conversion.properties.其中EmployeeAction-conversion.properties中的内容也分两种写法,如

#employee.birth=com.fengye.DateConverter
#birth=com.fengye.DateConverter

 页面访问也会有

<s:form name="save" action="emp_save" method="POST">
    <s:textfield name="name" label="username" ></s:textfield>
    <s:textfield name="birth" label="birth"></s:textfield>
    <s:submit label="submit" value="submit"/>
</s:form>
<s:form name="save" action="emp_save" method="POST">
    <s:textfield name="employee.name" label="username" ></s:textfield>
    <s:textfield name="employee.birth" label="birth"></s:textfield> 
   <s:submit label="submit" value="submit"/>
</s:form>

下面的表格列出了可能出现的结果(左侧代表了页面的访问方法,EmployeeAction/Employee/xwork代表了配置文件的位置,上方的第二行代表配置文件中的内容,如:employee.birth代表了

在EmployeeAction-conversion.properties放置在EmployeeAction的同级目录下,并且内容为employee.birth=com.fengye.DateConverter)

使用paramsPrepareParamsStack

 

 

 

EmployeeAction

Employee

xwork

employee.birth

birth

birth

java.util.Date

 

无异常

转换

无异常

转换

无异常

转换

无异常

转换

name="birth"

"employee.birth"

使用paramsPrepareParamsStack修改 PrepareInterceptor 拦截器的 alwaysInvokePrepare 属性值为 false

 

EmployeeAction

Employee

xwork

employee.birth

birth

birth

java.util.Date

 

无异常

转换

无异常

转换

无异常

转换

无异常

转换

name="birth"

"employee.birth"

使用默认拦截器defaultStack

 

EmployeeAction

Employee

xwork

employee.birth

birth

birth

java.util.Date

 

无异常

转换

无异常

转换

无异常

转换

无异常

转换

name="birth"

"employee.birth"

不使用ModelDriven

 

EmployeeAction

Employee

xwork

employee.birth

birth

birth

java.util.Date

 

无异常

转换

无异常

转换

无异常

转换

无异常

转换

name="birth"

不能访问

"employee.birth"

public class DateConverter extends StrutsTypeConverter {
    public Object convertFromString(Map context, String[] values, Class toClass) {
        if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            try {
                return sdf.parse(values[0]);
            }
            catch(ParseException e) {
                throw new TypeConversionException(e);
            }
        }
        return null;
    }
    public String convertToString(Map context, Object o) {
        if (o instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            return sdf.format((Date)o);
        }
        return "";
    }
}

 

转载于:https://www.cnblogs.com/fengyexjtu/p/4626655.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值