1. 定义一个类继承自: com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
package com.eric.util.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateTypeConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String, Object> context, Object value, Class toType) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd") ;
try {
if(toType == Date.class){//当字符串向Date类型转换时
String[] params = (String[]) value ;
return dateFormat.parse(params[0]);
}else if(toType == String.class){//当Date类型转换为字符串类型时
Date date = (Date) value ;
return dateFormat.format(date) ;
}
} catch (ParseException e) {}
return null ;
}
}
2. 在src 目录 下创建一个xwork-conversion.properties
java.util.Date=com.eric.util.converter.DateTypeConverter
3. action 的代码:
package com.eric.aciton;
import java.net.URLEncoder;
import com.eric.bean.Person;
public class HelloWorldAction {
private String msg ;
private String username ;
private String savepath ;
private String flag ;
private Person person ;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getFlag() {
return flag;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMessage() {
return this.msg;
}
public String getSavepath() {
return savepath;
}
public void setSavepath(String savepath) {
this.savepath = savepath;
}
public String execute() throws Exception{
//this.flag = "execute" ;
//this.username = URLEncoder.encode("张作强" , "UTF-8") ;
//this.msg = "我的第一个Struts2应用~!" ;
return "success";
}
public String add() {
this.flag = "add" ;
return "message" ;
}
}
4. Person复合类型:
package com.eric.bean;
import java.util.Date;
public class Person {
private Integer id ;
private String name ;
private Date birthday ;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
System.out.println(birthday);
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5. index.jsp参数接收视图
<%@ page language="java" import="java.util.*,java.net.URLDecoder" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
</head>
<body>
<form action="<%=request.getContextPath()%>/employee/person.action">
ID<input type="text" name="person.id"><br>
NAME<input type="text" name="person.name"><br>
BIRTHDAY<input type="text" name="person.birthday"><br>
<input type="submit" value="发送">
</form>
</body>
</html>
6. 页面回显视图
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Person</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<br>
<center>
ID:${person.id }<br>
NAME:${person.name }<br>
BIRTHDAY:${person.birthday }
</center>
</body>
</html>