在Struts开发中经常可以发现,用户提交的请求数据中,基本数据类型和java.sql.Date或java.sql.Time类型都能被封装到ActionForm类中,在页面可以直接取到用户输入的数值。唯有java.util.Date的数据不能封装,究其原因就是在提交请求时,RequestProcessor类在进行数据收集时没有将java.util.Date类型的数据进行转换。所以查看Struts源代码可以发现我们要做的就是简单的两件事
第一,如源代码中装换示例一样,自定义一个类实现org.apache.commons.beanutils.Converter接口
package com.javacrazyer.web.common;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.commons.beanutils.Converter;
/**
* 自定义的java.util.Date的类型转换器
*
*/
public class UtilDateConverter implements Converter {
public static final String DATE_PATTERN = "yyyy-MM-dd";
/* (non-Javadoc)
* @see org.apache.commons.beanutils.Converter#convert(java.lang.Class, java.lang.Object)
*/
@Override
public Object convert(Class arg0, Object arg1) {
Object obj = null;
if(java.util.Date.class == arg0){
if(arg1 != null && arg1 instanceof String){
DateFormat df = new SimpleDateFormat(DATE_PATTERN);
try {
obj = df.parse((String)arg1);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
return obj;
}
}
第二,在web项目进行部署时应该像源代码在ActionServlet中一样做一下注册的工作
要实现注册那么必须写一个注册的Servlet类;
package com.javacrazyer.web.common;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.beanutils.ConvertUtils;
public class UtilDateConverterRegisterServlet extends HttpServlet {
public void init() throws ServletException {
ConvertUtils.register(new UtilDateConverter(), java.util.Date.class);
}
}
然后在web.xml配置中将注册的Servlet类设定为自动启动但级别要比ActionServlet低些
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>UtilDateConverterRegisterServlet</servlet-name>
<servlet-class>com.javacrazyer.web.common.UtilDateConverterRegisterServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
示例
input.jsp
<%@ page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts应用:ActionForm作为类型转换器</title>
</head>
<body>
<h3>Struts应用:ActionForm作为类型转换器</h3><hr/>
<form id="myForm" name="myForm" action="registe.do" method="post">
<table border="1" width="600px">
<tr>
<td>int类型的数据</td>
<td><input type="text" name="intValue"/></td>
</tr>
<tr>
<td>double类型的数据</td>
<td><input type="text" name="doubleValue"/></td>
</tr>
<tr>
<td>char类型的数据</td>
<td><input type="text" name="charValue"/></td>
</tr>
<tr>
<td>boolean类型的数据</td>
<td><input type="text" name="boolValue"/></td>
</tr>
<tr>
<td>java.util.Date类型的数据</td>
<td><input type="text" name="dateValue"/></td>
</tr>
<tr>
<td>java.sql.Date类型的数据</td>
<td><input type="text" name="sqlDateValue"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
RegisterForm.java
package com.javacrazyer.web.formbean;
import java.util.Date;
import org.apache.struts.action.ActionForm;
/**
* 用来收集客户端提交数据.
* 要收集数据的属性的名一定要跟请求参数名相同
*
*/
public class RegisterForm extends ActionForm {
private static final long serialVersionUID = 6619272689058619128L;
private int intValue;
private double doubleValue;
private char charValue;
private boolean boolValue;
private Date dateValue;
private java.sql.Date sqlDateValue;
public java.sql.Date getSqlDateValue() {
return sqlDateValue;
}
public void setSqlDateValue(java.sql.Date sqlDateValue) {
this.sqlDateValue = sqlDateValue;
}
public Date getDateValue() {
return dateValue;
}
public void setDateValue(Date dateValue) {
this.dateValue = dateValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(double doubleValue) {
this.doubleValue = doubleValue;
}
public char getCharValue() {
return charValue;
}
public void setCharValue(char charValue) {
this.charValue = charValue;
}
public boolean getBoolValue() {
return boolValue;
}
public void setBoolValue(boolean boolValue) {
this.boolValue = boolValue;
}
}
WEB-INF/struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ================================================ Form Bean Definitions -->
<form-beans>
<form-bean name="loginForm" type="com.javacrazyer.web.formbean.LoginForm"/>
<form-bean name="regForm" type="com.javacrazyer.web.formbean.RegisterForm"/>
</form-beans>
<!-- =========================================== Action Mapping Definitions -->
<action-mappings>
<action path="/registe"
name="regForm"
scope="request"
validate="false"
type="com.javacrazyer.web.action.RegisterAction">
<forward name="result" path="/result.jsp"/>
</action>
</action-mappings>
</struts-config>
result.jsp
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册信息</title>
</head>
<body>
<h3>注册信息</h3><hr/>
intValue: ${regForm.intValue }<br/>
doubleValue: ${regForm.doubleValue }<br/>
charValue: ${regForm.charValue }<br/>
boolValue: ${regForm.boolValue }<br/>
java.sql.Date: ${regForm.sqlDateValue }<br/>
java.util.Date: ${regForm.dateValue }<br/>
</body>
</html>
执行效果
看到了吗,成功的接收了java.util.Date的值