Detailed description of struts2 type converter (with example)

Source: Internet
Author: User

Struts2 has two types of converters:

Local, global.

 

How to implement:

Step 1: Define the converter

Part 2: register the converter

 

 

The following is an example of a local type converter.

We have mentioned in the preceding log that there is a variable of the date type. The date type is valid only when the input format is yyyy-MM-dd. Otherwise, it is invalid. In this case, I need to convert the data type to the date type of yyyy-MM-dd. What should I do?

At this time, we need a custom type converter.

First, we define a type converter.

Our class is: DateTypeChange. java

Package com. fish;

 

 

Import java. text. SimpleDateFormat;

Import java. util. Date;

Import java. util. Map;

 

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

 

Publicclass DateTypeChange extends DefaultTypeConverter {// you must first inherit the default converter class

 

@ Override

Public Object convertValue (Map <String, Object> context, Object value,

Class toType) {// The second parameter is the value of the brithday attribute of test. Java. The third parameter is the value of the number you pass in.

SimpleDateFormat format = new SimpleDateFormat ("yyyyMMdd"); // create a date-type typographical format.

Try {

If (toType = Date. class ){

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

Return format. parse (parm [0]);

} Elseif (toType = String. class ){

Date date = (Date) value;

Return format. format (date );

}

} Catch (Exception e ){

}

Returnnull;

}

 

 

}

After writing the type converter, you should write a registration file to associate the type converter with the class you want to convert the attribute (this file is written in the same package of the type converter)

Format: "type of the property to be converted (without package name)" + conversion. properties.

Write in the file: brithday = com. fish. DateTypeChange

 

By the way, write other test. Java:

Package com. fish;

 

Import java. util. Date;

 

 

Publicclass Test {

Private String name;

Private String id;

Private Date brithday;

 

Public Date getBrithday (){

Returnbrithday;

}

Publicvoid setBrithday (Date brithday ){

This. brithday = brithday;

}

Public String getName (){

Returnname;

}

Publicvoid setName (String name ){

This. name = name;

}

Public String getId (){

Returnid;

}

Publicvoid setId (String id ){

This. id = id;

}

Public String execute (){

Return "success ";

}

}

Next: Struts2.xml:

<Package name = "fish" namespace = "/test" extends = "struts-default">

<Action name = "redfish" class = "com. fish. Test" method = "execute">

<Param name = "name"> xxxx </param>

<Result name = "success">/index. jsp </result>

</Action>

Then: MyJsp. JSP

<Form action = "/struts2test4/test/redfish. action"> & nbsp;

<Input type = "text" name = "username">

<Input type = "text" name = "id">

<Input type = "text" name = "brithday">

<Input type = "submit" value = "OK">

 

Last: index. JSP

Name =$ {name} <br>

Id =$ {id} <br>

Brithday =$ {brithday} <br>

 

In this case, when we enter the yyyyMMdd type date type in the index. JSP page, struts2 can also be converted to the date format for processing.

 

 

Of course, if you want to write a global converter.

Just change the properties file:

Move it to web-inf/classess and name it

Xwrok-conversion.properties

Content:

Conversion Type (with package name) = converter (with package name)

So write it like this.

Java. util. Date = com. fish. DateTypeChange

This means that all date types can be converted to this format for processing.

Related Article

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.