Struts2 local and global type converters

Source: Internet
Author: User

Define a test class

Package Struts2.example.action;import Java.util.date;public class Helloworldaction {    private Date birthday;    Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {System.out.print (birthday); this.birthday = Birthday;} Public String Execute () throws Exception{return "Success";}}
Defined in the Struts.xml file

<action name= "list_*" class= "struts2.example.action.HelloWorldAction" method= "{1}" >
<!--define the relationship between processing results and view resources--
<result name= "Success" >/index.jsp</result>
</action>
Runtime at URL input http://localhost: port number/project name/. (namespace): /list_execute.action?birthday=20121181 can complete the jump, the index.jsp through the EL expression ${birthday} output the corresponding birthday value, the output is a week and other information data, is not the original string. Application type converters enable bidirectional conversion of data.

Type converters are divided into two types
Local type converter: works on an action
Global type converter: works on all actions

The so-called type converter inherits a class Defaulttypeconverter and then overrides the method implementation. There are two scenarios for applying a type converter:
1. Conversion of the value of the request parameter to the value of the property
2, using the Struts2 label, for data Echo

Package Struts2.example.type.converter;import Java.sql.date;import Java.text.parseexception;import Java.text.simpledateformat;import Java.util.map;import Com.opensymphony.xwork2.conversion.impl.defaulttypeconverter;public class Datetypeconverter extends Defaulttypeconverter{@Overridepublic Object Convertvalue (map<string, object> context, Object Value,class ToType ) {//TODO auto-generated method Stubsimpledateformat simpledateformat=new SimpleDateFormat ("yyyynndd"); Try{if (ToType ==date.class) {//convert String type to Date type string[] params= (string[]) Value;return simpledateformat.parse (Params[0]);} else if (totype==string.class) {//converts date type to String type Date date= (date) Value;return Simpledateformat.format (date);}} catch (ParseException e) {}return null;}}
As with Struts1, registration is also required in Struts2. Registered Local type converters
Place the Actionclassname-conversion.properties file under the package where the action class is located, Actionclassname is
The class name of the action, followed by the-conversion.properties is a fixed notation, for this example, the name of the file should
For Helloworldaction.properties.

The content in the properties file is: Property name = Full class name of the type converter
For this example, the contents of the Helloworldaction-conversion.properties file are:
Birthday=struts2.example.type.converter.datetypeconverter

Custom Global type converters : Place xwork-conversion.properties files under Web-inf/classes, in the properties file
The type to be converted = The full class name of the type converter
For the purposes of this example, the contents of the Xwork-conversion.properties file are:
Java.util.date=struts2.example.type.converter.datetypeconverter
In addition, use a global type converter to pay attention to the input data format symbol requirements.

Struts2 local and global type converters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.