Struts2 日期类型编辑器 和 struts.xml 的存放路径

Action 下的某一个bean 类 同路径下的 [ClassName-conversion.properties] 文件
类型转换器需继承的类DefaultTypeConverter

package my.converters;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import ognl.DefaultTypeConverter;

public class DateConverter extends DefaultTypeConverter {

@Override
public Object convertValue(Map context, Object value, Class toType) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日");

try {
if(toType == Date.class){//如何由字符串转换成日期类型
String[] params = (String[]) value;
return sdf.parseObject(params[0]);
}else if(toType == String.class){//如何由日期类型转换成字符串
Date date = (Date)value;
return sdf.format(date);
}

} catch (Exception e) {
// TODO: handle exception
}
return null;
}

}

对应的[ClassName-conversion.properties] 文件 内部是 键值对的形式:
hireDate = my.converters.DateConverter


struts.xml 路径问题:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>struts</display-name>

<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

<init-param>
<param-name>config</param-name>
<!-- //web容器初始化时 struts2 共初始化三个文件 当前存放路径-->WEB-INF 下的Class 文件下-->
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>

原文地址:https://www.cnblogs.com/leonkobe/p/3054387.html