1.日期格式转化器
利用struts2写一个日期转换器,把表单填写的日期格式1999-1-1转换成1999/1/1。
- login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>简化的类型转换器</title>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script language="javascript">
function check()
{
var a=/^(\d{1,4})(-|v)(\d{1,2})\2(\d{1,2})/;
if(!a.test(document.form1.user.value))
{
alert("日期格式不正确!");
return false;
}
else
{
return true;
}
}
</script></head>
<body>
<form action="login.action" method="post" name="form1">
<table width="319" border="1" align="center">
<caption>
<h3><br>
<br>
用户注册</h3>
</caption>
<tr align="center">
<td width="105" height="26" bgcolor="#FFCCFF"><div align="center">用户名:</div></td>
<td width="198" bgcolor="#FFCCFF"><label>
<div align="left">
<input type="text" name="name">
</div>
</label></td>
</tr>
<tr>
<td height="26" bgcolor="#FFCCFF"><div align="center">密 码:</div></td>
<td height="26" bgcolor="#FFCCFF"><label>
<input type="password" name="password">
</label></td>
</tr>
<tr>
<td height="26" bgcolor="#FFCCFF"><div align="center">出生日期:</div></td>
<td height="26" bgcolor="#FFCCFF"><input type="text" name="user"/></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFCCFF"><input type="submit" value="注册" onclick="return check();"/>
<input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
- 一个User的类,有一个String类型的date属性:
package www.itheima.bean;
public class User
{
private String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
- 一个对象类型驱动的action处理类,包含了User类型和表单字段名称:
package www.itheima.action;
import www.itheima.bean.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
public User user;
public String name;
public String password;
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception
{
return SUCCESS;
}
}
- 写一个日期格式转换器UserConvertor继承StrutsTypeConvertor,利用SimpleDateFormate类的parse()方法和format()方法进行日期的转换。
package www.itheima.action;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import www.itheima.bean.User;
public class UserConverter extends StrutsTypeConverter{
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
// TODO Auto-generated method stub
User user=new User();
SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1=format1.parse(values[0]);//values[0]代表了什么
SimpleDateFormat format2=new SimpleDateFormat("yyyy/MM/dd");
String birthday=format2.format(date1);
user.setDate(birthday);
System.out.println(user.getDate());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return user;
}
@Override
public String convertToString(Map map, Object obj) {
// TODO Auto-generated method stub
User user=(User)obj;
return "<"+user.getDate()+">";
}
}
- 最后,一个成功跳转的页面success.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>转换成功</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: 16px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<p align="center" class="STYLE1"> </p>
<p align="center" class="STYLE1"> </p>
<p align="center" class="STYLE1">转换成功!</p>
<div align="center">
<table width="211" border="1">
<tr>
<td width="116" bgcolor="#FFCCFF"><div align="center">用户名:</div></td>
<td width="79"><s:property value="name"/></td>
</tr>
<tr>
<td bgcolor="#FFCCFF"><div align="center">密 码:</div></td>
<td><s:property value="password"/></td>
</tr>
<tr>
<td bgcolor="#FFCCFF"><div align="center">出生日期:</div></td>
<td><s:property value="user.date"/></td>
</tr>
</table>
</div>
</body>
</html>
效果如下图所示: