本帖最后由 lucky_xingxing 于 2011-12-22 21:25 编辑
struts2.1.6和2.1.8中的类型转换问题,都配置好了但是执行没效果打印出来是null 给我自己的感觉是根本没有进去DateTypeConvert 类执行convertValue方法
转换器代码:
package com.lovo.convert;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
/**
* 自定义类型转换器
* @author lucky_xingxing
*
*/
public class DateTypeConvert extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
try {
SimpleDateFormat simple = new SimpleDateFormat("yyyymmdd");
if(toType == Date.class){
String [] values = (String []) value; //request.getParameterValues()
System.out.println(simple.parse(values[0]));
return simple.parse(values[0]);
}else if(toType==String.class){
Date date = (Date)value;
return simple.format(date);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Action的代码:
package com.lovo.convert;
import java.util.Date;
public class ConvertAction {
private Date date;
public String execute(){
System.out.println(date);
return "succes";
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
转换器配置文件内容 文件名: ConvertAction-conversion.properties
date=com.lovo.convert.DateTypeConvert
web.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="getParam" namespace="/GetParameter" extends="struts-default">
<action name="convertUI">
<result>/WEB-INF/page/convert.jsp</result>
</action>
<action name="convert" class="com.lovo.convert.ConvertAction" method="execute">
<result name="succes">/index.jsp</result>
</action>
</package>
</struts>
jsp页面: convert.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 'index.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">
</head>
<body>
自定义转换器实例
<form action="${pageContext.request.contextPath}/GetParameter/convert" method="post">
日期:<input type="text" name="date" />
<input type="submit" value="提交"/>
</form>
</body>
</html>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>自定义转换器</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
convert:${date}
</body>
</html>
|
|