Struts框架之处理流程和类型转换器

处理流程

Created with Raphaël 2.1.0 用户请求 StrutsPrepareAndExecuteFilter Interceptor(struts2内置的拦截器和用户自定义的拦截器) Action(业务逻辑处理) jsp/html(页面视图) 请求响应

在上述流程图中,控制层Action可以通过拦截器对其属性自动赋值,但是多个数据类型有时候并不一定能够成功转换。

自定义类型转换器

常见的类型转化器主要是针对日期类型的,对于自定义类型转换器,只需要继承com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter或者org.apache.struts2.util.StrutsTypeConverter(StrutsTypeConverter 继承与DefaultTypeConve)。 主要有两种方式进行转换:
1、全局范围的类型转换器
在源目录(src/main/java)下新建xwork-conversion.properties文件。其中属性文件的key值为需要转换的类的全名,value值为进行类型转换的类的全名。如下:

#全局类型转换器:类全名=类型转换器全名
#java.util.Date=com.struts.typeconverter.DefaultDateTypeConverter
java.util.Date=com.struts.typeconverter.StrutsDateTypeConverter

其中在本类中定义了两个类型转换器,一个是继承DefaultTypeConverter,一个是继承StrutsTypeConverter。这两个类的实现方式不同并且前台获取的方式也不一致。参见以下代码:
继承StrutsTypeConverter:

package com.struts.typeconverter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

/**
 * @TODO 类型转换器
 * @author Administrator
 * @date 2015年6月26日 下午10:40:49
 * @version 1.0
 */
public class DefaultDateTypeConverter extends DefaultTypeConverter{
    private static final String[] DATE_FORMAT = {"yyyy-MM-dd","yyyy/MM/dd","yyyyMMdd"};

    /**
     *@param context
     *@param value
     *@param toType 
     */
    @Override
    public Object convertValue(Map<String, Object> context, Object value,
            @SuppressWarnings("rawtypes") Class toType) {
        Object formatValue = null;
        for(String format:DATE_FORMAT){
            if(toType == Date.class){
                try{
                    String source = ((String[])value)[0];
                    formatValue = new SimpleDateFormat(format).parse(source);
                    return formatValue;
                }catch(Exception e){
                }
            }else if(toType == String.class){
                try{
                    Date source = ((Date[])value)[0];
                    formatValue = new SimpleDateFormat(format).format(source);
                    return formatValue;
                }catch(Exception e){
                }
            }
        }
        return formatValue;
    }
}

继承StrutsTypeConverter:

package com.struts.typeconverter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

/**
 * @TODO TODO
 * @author Administrator
 * @date 2015年6月26日 下午11:40:12
 * @version 1.0
 */
public class StrutsDateTypeConverter extends StrutsTypeConverter{
    private static final String[] DATE_FORMAT = {"yyyy-MM-dd","yyyy/MM/dd","yyyyMMdd"};
    /**
     * 对前台传递的日期参数进行转换
     */
    @SuppressWarnings("rawtypes")
    @Override
    public Object convertFromString(Map paramMap, String[] paramArrayOfString,
            Class paramClass) {
        Object formatValue = null;
        for(String format:DATE_FORMAT){
            if(paramClass == Date.class){
                try{
                    formatValue = new SimpleDateFormat(format).parse(paramArrayOfString[0]);
                    return formatValue;
                }catch(Exception e){
                }
            }else if(paramClass == String.class){
                try{
                    formatValue = new SimpleDateFormat(format).format(paramArrayOfString[0]);
                    return formatValue;
                }catch(Exception e){
                }
            }
        }
        return formatValue;
    }

    /**
     * 在前台使用struts标签<s:property/>时显示使用
     */
    @SuppressWarnings("rawtypes")
    @Override
    public String convertToString(Map paramMap, Object paramObject) {
        return new SimpleDateFormat("yyyy-MM-dd").format(paramObject);
    }

}

使用struts2.1.16,如果需要使用EL表达式,则需要在使用的页面添加以下代码:

<%@page isELIgnored="false" %>

前台页面获取属性代码如下:

<s:property value="login.username"/>登录成功!<br/>
    出生日期:<s:date name="login.birthday" format="yyyy/MM/dd" /><br/>
    <!-- 继承StrutsTypeConverter 可以使用此标签获取日期值,继承DefaultTypeConverter则无法获取属性值 -->
    <s:property value="login.birthday" /><br/>
    EL表达式:${login.birthday }

2、特定类的类型转换器
做法:在特定类的相同目录下创建名为ClassName-conversion.properties的属性文件,内容为:特定类的属性名=类型转换器类全名,在Login的同级目录下新建配置为文件:Login-conversion.properties,内容如下:

#适用于Login类属性birthday的转换器
birthday=com.struts.typeconverter.DefaultDateTypeConverter
#birthday=com.struts.typeconverter.StrutsDateTypeConverter

局部类型转换器同样能达到全局类型转换器的作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值