struts类型转换:有两种方法(a)实现HttpServlet接口;(b)实现PlugIn接口;
converter_index.jsp(输入页面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'converter_index.jsp' starting page</title>
</head>
<body>
<form action="typeconvert.do" method="post">
<table>
<tr>
<td>intValue:</td><td><input type="text" name="intValue"/></td>
</tr>
<tr>
<td>doubleValue:</td><td><input type="text" name="doubleValue"/></td>
</tr>
<tr>
<td>booleanValue:</td><td><input type="text" name="booleanValue"/></td>
</tr>
<tr>
<td>sqlValue:</td><td><input type="text" name="sqlValue"/></td>
</tr>
<tr>
<td>utilDateValue:</td><td><input type="text" name="utilDateValue"/></td>
</tr>
<tr>
<td>submit:</td><td><input type="submit" name="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
converter_success.jsp(用于显示)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.eplugger.struts.form.TypeConvertActionForm" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'converter_success.jsp' starting page</title>
</head>
<body>
EL表达式取值:<br>
intValue:${typeConvertForm.intValue }<br>
doubleValue:${typeConvertForm.doubleValue }<br>
booleanValue:${typeConvertForm.booleanValue }<br>
java.sql.DateValue:${typeConvertForm.sqlValue }<br>
java.util.DateValue:${typeConvertForm.utilDateValue }<br>
jsp取值:<br>
<%
TypeConvertActionForm tcaf =(TypeConvertActionForm) request.getAttribute("typeConvertForm");
%>
<%
out.print(tcaf.getIntValue());
%><br>
<%
out.print(tcaf.getDoubleValue());
%><br>
<%
out.print(tcaf.getBooleanValue());
%><br>
<%
out.print(tcaf.getSqlValue());
%><br>
<%
out.print(tcaf.getUtilDateValue());
%><br>
</body>
</html>
TypeConverterAction.java(只作跳转)
package com.eplugger.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class TypeConvertAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward("success");
}
}
TypeConverterActionForm.java(接收页面传来的值)
package com.eplugger.struts.form;
import org.apache.struts.action.ActionForm;
public class TypeConvertActionForm extends ActionForm {
private int intValue;
private double doubleValue;
private boolean booleanValue;
private java.sql.Date sqlValue;
private java.util.Date utilDateValue;
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 boolean getBooleanValue() {
return booleanValue;
}
public void setBooleanValue(boolean booleanValue) {
this.booleanValue = booleanValue;
}
public java.sql.Date getSqlValue() {
return sqlValue;
}
public void setSqlValue(java.sql.Date sqlValue) {
this.sqlValue = sqlValue;
}
public java.util.Date getUtilDateValue() {
return utilDateValue;
}
public void setUtilDateValue(java.util.Date utilDateValue) {
this.utilDateValue = utilDateValue;
}
}
UtilDateConverter.java(对传来的String类型的数据进行转换)
package com.eplugger.struts;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
public class UtilDateConverter implements Converter {
public Object convert(Class type, Object value) {
System.out.println("value="+value);
if(value==null){
return value;
}
Date date=null;
if(value instanceof String){
SimpleDateFormat sdf = new SimpleDateFormat("yyy-mm-dd");
try{
date = sdf.parse((String)value);
}catch(ParseException e){
e.printStackTrace();
}
}
return date;
}
}
两种方法上面的都一样。下面的就是两种方法的区别了
下面如果用HttpServlet代码如下:
UtilDateConverterInitWithServlet.java
package com.eplugger.struts.converter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.commons.beanutils.ConvertUtils;
import com.eplugger.struts.UtilDateConverter;
/**
* 注册java.util.date转换器
*/
public class UtilDateConverterInitWithServlet extends HttpServlet {
@Override
public void init() throws ServletException {
ConvertUtils.register(new UtilDateConverter(), Date.class);
}
}
用这个方法必须在web.xml中加上配置
<servlet>
<servlet-name>UtilDateConverterInitWithServlet</servlet-name>
<servlet-class>com.eplugger.struts.converter.UtilDateConverterInitWithServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
如果用PlugIn方法代码:
package com.eplugger.struts.converter;
import java.util.Date;
import javax.servlet.ServletException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import com.eplugger.struts.UtilDateConverter;
public class UtilDateConverterInitWithPlugIn implements PlugIn {
public void destroy() {
}
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
ConvertUtils.register(new UtilDateConverter(), Date.class);
}
}
Struts-config.xml(如果用PlugIn方法必须在最后加上<plug-in className="com.eplugger.struts.converter.UtilDateConverterInitWithPlugIn"/>)
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="fileupload" type="com.eplugger.struts.form.FileUploadActionForm"/>
<form-bean name="blankfield" type="com.eplugger.struts.form.BlankFieldActionForm"/>
<form-bean name="typeConvertForm" type="com.eplugger.struts.form.TypeConvertActionForm"/>
</form-beans>
<action-mappings>
<action path="/typeconvert"
type="com.eplugger.struts.action.TypeConvertAction"
name="typeConvertForm"
scope="request"
>
<forward name="success" path="/converter_success.jsp"/>
</action>
</action-mappings>
<plug-in className="com.eplugger.struts.converter.UtilDateConverterInitWithPlugIn"/>
</struts-config>