package com.ethan.converter;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
/**
* value String[]数组类型,考虑到了同名复选框多个值
* @author ETHAN
*
*/
public class DateTypeConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
System.out.println("converter-----------------");
try {
if(toType==Date.class) {
String[] params = (String[]) value;
return format.parse(params[0]);
} else if(toType==String.class) {
Date date = (Date) value;
return format.format(date);
}
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
package com.ethan.action;
import java.util.Date;
import com.ethan.domain.Person;
public class HelloWorldAction {
private String msg;
private String username;
private Date creationTime;
private Integer id;
private Person person;
public String addUI() {
this.msg = "add";
return "success";
}
public String execute() throws Exception {
this.msg="execute";
//由用户传递过来的
//this.username = URLEncoder.encode("秦彬阳","UTF-8");
return "success";
}
//el表达式由get后边的名称决定${message},会通过反射得到method:getMessage()
public String getMessage() {
return msg;
}
public void setMessage(String message) {
this.msg = message;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String add() {
return "message";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
System.out.println(creationTime);
}
}
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'message.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">
-->
</head>
<body>
${message}<br/>
id=${id }<br/>
username=${person.name }<br/>
<%-- creationTime=<%=String.format("%tF",new SimpleDateFormat("yyyyMMdd").parse(request.getParameter("creationTime"))) %>
--%> <hr/>
${applicationScope.app }<br/>
${sessionScope.session }<br/>
${requestScope.req }
<br/>
${msg }
</body>
</html>
HelloWorldAction-conversion.properties放在对应action的包下,注意命名规则:
creationTime=com.ethan.converter.DateTypeConverter
下边是全局转换器:
xwork-conversion.properties 放在classpath下,注意命名规则:
java.util.Date=com.ethan.converter.DateTypeConverter