在上一文中我们是通过自己写一个实现了Convert接口的类,并通过一个Serverlet在init的时候注册这个Convert。
但这样实现不够好。
在struts中可以通过插件来实现。
完整代码如下:
convert.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="convertAction.do" method="post"> intvalue:<input type="text" name="intval"/><br> doublevalue:<input type="text" name="doubleval"/><br> booleanvalue:<input type="text" name="booleanval"/><br> utildatevalue:<input type="text" name="dateval"/><br> <input type="submit" value="submit"/> </form> </body> </html>
form.
package com.lwf.struts.form; import java.util.Date; import org.apache.struts.action.ActionForm; public class ConvertForm extends ActionForm { private int intval; private double doubleval; private boolean booleanval; private Date dateval; public int getIntval() { return intval; } public void setIntval(int intval) { this.intval = intval; } public double getDoubleval() { return doubleval; } public void setDoubleval(double doubleval) { this.doubleval = doubleval; } public boolean isBooleanval() { return booleanval; } public void setBooleanval(boolean booleanval) { this.booleanval = booleanval; } public Date getDateval() { return dateval; } public void setDateval(Date dateval) { this.dateval = dateval; } }
action
package com.lwf.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 ConvertAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("success"); } }
结果:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> intvalue:${convertForm.intval }<br> doublevalue:${convertForm.doubleval }<br> booleanvalue:${convertForm.booleanval }<br> utildatevalue:${convertForm.dateval }<br> </body> </html>
配置:
<action path="/convertAction" type="com.lwf.struts.action.ConvertAction" name="convertForm" scope="request"> <forward name="success" path="/convertResult.jsp"></forward> </action>
<form-bean name="convertForm" type="com.lwf.struts.form.ConvertForm"></form-bean>
下面是自定义的convert类,用来将String类转换为java.util.Date类型
package com.lwf.struts.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.beanutils.Converter; public class UtilDateConverter implements Converter { public UtilDateConverter(Object object) { } public Object convert(Class type, Object value) { if(value == null){ return value; } if(value instanceof Date){ return value; } if(value instanceof String){ try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); return sdf.parse((String)value); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } }
以下代码关键:UtilDateConvertPlugIn用来实现PlugIn接口,并实现init方法
package com.lwf.struts.util; 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; public class UtilDateConvertPlugIn implements PlugIn { public void destroy() { } public void init(ActionServlet arg0, ModuleConfig arg1) throws ServletException { ConvertUtils.register(new UtilDateConverter(null), Date.class); } }
配置:
<plug-in className="com.lwf.struts.util.UtilDateConvertPlugIn" ></plug-in>
这样Struts启动的时候会加载这个UtilDateConvertPlugIn并执行init方法。。
是不是比新建一个servlet更合理呢。。