Struts2环境配置及全局自定义类型转换器

  • 格式:pdf
  • 大小:112.32 KB
  • 文档页数:7

<-------------说明:

程序为Struts2的配置环境程序.

全局———>自定义类型转换器------------------->

<---------------------------------------------------------->

<---------------------------------------------------------->

<---Date时间自定义转换器,用两步就可以将时间的类型进行转换。

全局类型转换器,必须在类里面的Date的时间方法都是19920912或者

1992-09-12或者

1992/09/12在统一的情况下才能用全局类型转换,或者会出现错误。建议

在Date不确定

的情况下最好是用局部转换器。---><---------------------------------------------------------->

<---------------------------------------------------------->

<-------------------HelloWorldAction.java------------------>

<---------------------------------------------------------->

packagecn.itcast.action;

importjava.util.Date;

/**

*说明:

*@authorAdministrator

*类型转换器分为两种:

*1、局部类型转换器

*2、全局类型转换器

*

*/

publicclassHelloWorldAction

{

privateDatebirthday;

publicDategetBirthday()

{

returnbirthday;

}

publicvoidsetBirthday(Datebirthday)

{

System.out.println(birthday);

this.birthday=birthday;

}

publicStringaddUI()

{

return"success";

}

publicStringexecute()throwsException

{

return"success";

}

}

<---------------------------------------------------------->

<-------------------HelloWorldAction.java------------------>

<---------------------------------------------------------->

packagecn.itcast.type.Converter;

importjava.text.ParseException;

importjava.text.SimpleDateFormat;

importjava.util.Date;

importjava.util.Map;

import

com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

publicclassDateTypeConverterextendsDefaultTypeConverter

{

@Override

publicObjectconvertValue(Mapcontext,Object

value,ClasstoType){

SimpleDateFormatdateFormat=new

SimpleDateFormat("yyyyMMdd");

try{

if(toType==Date.class)

{

String[]params=(String[])value;

returndateFormat.parse(params[0]);

}elseif(toType==String.class)

{

Datedate=(Date)value;

returndateFormat.format(date);

}

}catch(ParseExceptione){}

returnnull;

}

}

<---------------------------------------------------------->

<-------------------struts.xml----------------------------->

<---------------------------------------------------------->

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration

2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

value="false"/>

extends="struts-default">

class="cn.itcast.action.HelloWorldAction"method="{1}">

name="success">/WEB-INF/page/message.jsp

<---------------------------------------------------------->

<-------------------xwork-conversion.properties------------><---------------------------------------------------------->

java.util.Date=cn.itcast.type.Converter.DateTypeConverter

<---------------------------------------------------------->

<-------------------hello.jsp------------------------------>

<---------------------------------------------------------->

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%>

"http://www.w3.org/TR/html4/loose.dtd">

charset=ISO-8859-1">

hello

${message}

<---------------------------------------------------------->

<-------------------hello.jsp------------------------------>

<---------------------------------------------------------->

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%>

"http://www.w3.org/TR/html4/loose.dtd">

charset=ISO-8859-1">

hello

${birthday}

<---------------------------------------------------------->

<-------------------message.jsp---------------------------->

<---------------------------------------------------------->

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"

pageEncoding="UTF-8"%>

"http://www.w3.org/TR/html4/loose.dtd">

charset=ISO-8859-1">

hello

${birthday}

<---------------------------------------------------------->

<-------------------web.xml-------------------------------->

<---------------------------------------------------------->

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPr

epareAndExecuteFilter