您的位置:首页 > 编程语言 > Java开发

struts日期类型转换器

2017-01-11 06:32 246 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: