Struts2的时间类型转换

Struts2默认能转换的时间书写方式是yyyy-MM-dd,想要转换其他类型的时间输入,就要利用到类型转换器。

首先要用一个Action类:

package com.hncj.edu;

import java.util.Date;

public class User {
	private int age;
	
	private Date birthday;  //要转换的时间类型

	private String name;

	private String password;

	public int getAge() {
		return age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	public String loginUser() {
		System.out.println(this.name);
		System.out.println(this.age);
		System.out.println(this.password);
		System.out.println(this.birthday);
		return "SUCCESS";
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}

配置的类型转换器:

package com.hncj.converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.opensymphony.xwork2.conversion.TypeConversionException;

public class DateConverter extends StrutsTypeConverter{
	
	/*这是看网上一位朋友的代码,感觉写的不错,可以增添自己想要的时间输入形式*/
	private final DateFormat[] dateFormat = {
			new SimpleDateFormat("yyyy年MM月dd日"),   
			new SimpleDateFormat("yyyy-MM-dd"),
			new SimpleDateFormat("yyyy.MM.dd"),
			new SimpleDateFormat("yyyy/MM/dd"),
	};
	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		String value = values[0];
		
		//尝试所以能转换的类型
		for(int i=0;i<dateFormat.length;i++) {
			try {
				return dateFormat[i].parse(value);
			} catch (ParseException e) {
				continue;
			}
		}
		//没找到能转换的类型,抛出异常
		throw new TypeConversionException();
	}

	@Override
	public String convertToString(Map context, Object o) {
		return new SimpleDateFormat("yyyy-MM-dd").format(o);
	}

}

下面是配置转换器的文件,名字统一为xwork-conversion.properties,内容为:(必须放在src目录下)

java.util.Date=com.hncj.converter.DateConverter

上面是全局的配置方式,还可以用局部的配置方式,文件名为User-conversion.properties,位置放在User类所在的包下。

birthday=com.hncj.edu.DateConverter

jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="${pageContext.request.contextPath}/login">
		<table>
			<caption>用户登录</caption>
			<tr>
				<td>用户名:</td>
				<td><input type="text" name="name"/></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input type="password" name="password"/></td>
			</tr>
			<tr>
				<td>年龄:</td>
				<td><input type="text" name="age"/></td>
			</tr>
			<tr>
				<td>生日:</td>
				<td><input type="text" name="birthday"/></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<input type="submit" value="submit"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值