struts日期类型转换器

package com.zyf.d_type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

/**
 * 自定义类型转换器类
 * 
 * @author Jie.Yuan
 * 
 */
public class MyConverter extends StrutsTypeConverter {

	// 新需求: 要求项目中要支持的格式,如: yyyy-MM-dd/yyyyMMdd/yyyy年MM月dd日..

	// 先定义项目中支持的转换的格式
	DateFormat[] df = { new SimpleDateFormat("yyyy-MM-dd"),
			new SimpleDateFormat("yyyyMMdd"),
			new SimpleDateFormat("yyyy年MM月dd日") };

	/**
	 * 把String转换为指定的类型 【String To Date】
	 * 
	 * @param context
	 *            当前上下文环境
	 * @param values
	 *            jsp表单提交的字符串的值
	 * @param toClass
	 *            要转换为的目标类型
	 */
	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {

		// 判断: 内容不能为空
		if (values == null || values.length == 0) {
			return null;
		}
		// 判断类型必须为Date
		if (Date.class != toClass) {
			return null;
		}
		
		// 迭代:转换失败继续下一个格式的转换; 转换成功就直接返回
		for (int i=0; i<df.length; i++) {
			try {
				return df[i].parse(values[0]);
			} catch (ParseException e) {
				continue;
			}
		}
		return null;
	}

	@Override
	public String convertToString(Map context, Object o) {
		return null;
	}

}

package com.zyf.d_type;

import java.util.Date;

public class User {
	//封装请求数据
    private String name;  //必须给set、get可以不用给
    private String pwd;
    private int age;
    private Date birth;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
   
}

package com.zyf.d_type;

public class UserAction {
    private User user ;
    public void setUser(User user) {
		this.user = user;
	}
    public User getUser() {
		return user;
	}
    public String register() {
		// TODO Auto-generated method stub
        System.out.println(user.getName());
        System.out.println(user.getPwd());
        System.out.println(user.getAge());
        System.out.println(user.getBirth());
        return "success";
	}
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<package name="type" namespace="/" extends="struts-default">
		
		<action name="user_*" class="com.zyf.d_type.UserAction" method="{1}">
			<result name="success">/index.jsp</result>
			
			<!-- 当日期类型转换错误的时候,会跳到input视图(struts内部返回) -->
			<result name="input">/error.jsp</result>
		</action>
		
	</package>	
	
</struts>

user.birth=com.zyf.d_type.MyConverter

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<!-- 一、全局配置 -->
	<!-- 0. 请求数据编码 -->
	 <constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 1. 修改Struts默认的访问后缀 -->
	<constant name="struts.action.extension" value="action,do,"></constant>
	<!-- 2. 修改xml自动重新加载 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 3. 开启动态方法调用 (默认不开启)-->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
	<!-- 4. 修改上传文件的最大大小为30M -->
	<constant name="struts.multipart.maxSize" value="31457280"/>
	
	
</struts>

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 二、总配置文件:引入其他所有配置文件 -->
	
	<include file="constant.xml"></include>
    <include file="com/zyf/c_data/data.xml"></include>
    <include file="com/zyf/d_type/type.xml"></include>
</struts>

java.util.Date=com.zyf.d_type.MyConverter

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值