一为什么要使用转换器
首先struts2通过request.getParameter("");获取String数据,并通过拦截器将
String转换为各种常用的数据类型。但这些Struts2默认的转换器并不总是满足
需求。
二我们自己编写的类型拦截器
package com.MyStruts2Test.convertor; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Map; import ognl.DefaultTypeConverter; public class DateTimeConvertor extends DefaultTypeConverter{ private DateFormat [] dateFormat = { new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("yy-MM-dd") }; private DateFormat [] timeFormat = { new SimpleDateFormat("HH:mm:ssss"), new SimpleDateFormat("HH:mm") }; public Object convertValue(Map context,Object value,Class toType){ if(toType.equals(java.sql.Date.class)){ String [] parameterValues = (String [])value; for(DateFormat format:dateFormat){ try{ return new java.sql.Date (format.parse(parameterValues[0]).getTime()); }catch(ParseException e){ } } }else if(toType.equals(java.sql.Time.class)){ String [] parameterValues = (String [])value; for(DateFormat format:timeFormat){ try{ return new java.sql.Time (format.parse(parameterValues[0]).getTime()); }catch(ParseException e){ } } }else if(toType.equals(java.util.Date.class)){ String [] parameterValues = (String [])value; for(DateFormat format:dateFormat){ try{ return format.parse (parameterValues[0]); }catch(ParseException e){ } } }else if(toType.equals(String.class)){ if (value instanceof java.sql.Date){ }else if(value instanceof java.sql.Time){ }else if(value instanceof java.util.Date){ return dateFormat[0].format ((java.util.Date)value); } } return super.convertValue(context, value, toType); } }
convertValue(Map context, Object value, Class toType)
的说明详见
http://www.opensymphony.com/ognl/api/ognl/DefaultTypeConverter.html
三编写相应拦截器之后,我们必须配置转换器
java.sql.Date=com.MyStruts2Test.convertor.DateTimeConvertor java.sql.Time=com.MyStruts2Test.convertor.DateTimeConvertor java.util.Date=com.MyStruts2Test.convertor.DateTimeConvertor以及Action
package com.MyStruts2Test.action; import java.sql.Date; import java.sql.Time; import com.opensymphony.xwork2.ActionSupport; public class ConverterAction extends ActionSupport{ private Date sqlDate; private Time sqlTime; private java.util.Date utilDate; public String execute(){ return INPUT; } public String convert(){ return SUCCESS; } public Date getSqlDate() { return sqlDate; } public void setSqlDate(Date sqlDate) { this.sqlDate = sqlDate; } public Time getSqlTime() { return sqlTime; } public void setSqlTime(Time sqlTime) { this.sqlTime = sqlTime; } public java.util.Date getUtilDate() { return utilDate; } public void setUtilDate(java.util.Date utilDate) { this.utilDate = utilDate; } }1这里的意思就是如果我们要转换到java.sql.Date类型,我们就要使用类
DateTimeConvertor去进行转换。我们在Action中不需要进行任何的处理,我们
只需要声明相应的类型,转换器会自动进行调用。
2.java.util.Date java.sql.Date区别
http://www.cnblogs.com/lvjianjun/archive/2010/08/24/1807501.html
四编写配置文件和其他页面
1
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name = "converter" extends = "struts-default" namespace = "/converter"> <action name = "converter" class = "com.MyStruts2Test.action.ConverterAction"> <result name = "input">converter.jsp</result> <result name= "success">success.jsp</result> </action> </package> </struts>
2
<body> This is my JSP page. <br> <s:form action = "converter"> <s:label label = "转换器"/><br> <s:textfield name = "sqlDate" label = "SQL Date:"/><br> <s:textfield name = "sqlTime" label = "SQL Time:"/><br> <s:textfield name = "utilDate" label = "Util Date:"/><br> <s:submit value = "提交" method = "convert"/> </s:form> </body>注意这里的<s:submit value = "提交" method = "convert"/>,它会调用相应
的action类的方法,而不是通过动态方法或其他方法调用.
3
<body> This is my JSP page. <br> java.sql.Date:<s:property value = "sqlDate"/><br> java.sql.Time:<s:property value = "sqlTime"/><br> java.util.Date:<s:property value = "utilDate"/><br> </body>
4
<body> This is my JSP page. <br> <a href = "<s:url action = "converter" namespace = "/converter"/>">ConverterTest</a> </body>
5
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>特别要注意一个以前没发现的问题,如果我们这里的urlpattern不使用/*,而使
用*.action,也就是针对所有后缀为.action的页面执行过滤,但是如果我们在
其他非action后缀的页面中使用struts标签的话就会发生问题,这是由于struts
标签需要struts的过滤器进行处理。
有关类型转换器的知识可见
http://blog.csdn.net/q3498233/article/details/7074095或者
http://blog.163.com/zhangfei_jiayou/blog/static/56244178200982622041863/