Struts2 type converter

Source: Internet
Author: User

Struts2 custom Type converters are divided into local Type converters and Global Type converters

API address for ognl: http://www.opensymphony.com/ognl/api/

The ognl source code is automatically downloaded from the Internet.

 

1. type conversion supported by struts2

Reference

Http://ejb3.blogbus.com/logs/22922214.html

Http://www.cnblogs.com/wushiqi54719880/archive/2011/07/09/2101773.html

 

Types that can be automatically converted:

Eight basic types: Date (local format: yyyy-mm-dd), array, list, map, and set

Bean. firstname is assigned to the firstname attribute of the bean object in the action.

Friends ['Patrick ']. Name is assigned to the name attribute of the object whose key is 'Patrick' in the map object.

Friends ['Patrick '] assigns the value of key 'Patrick' to the map object.

Note: When using a set, pay attention to Using Generics. If you do not use generics, you need to write additional configuration files.

Reference http://hi.baidu.com/sqzhangling/item/b951bf8e39840e29100ef312

 

2. Custom type converter

Type converter Syntax:

 

Write a class. Class requirements: implement the typeconverter interface or inherit the defaulttypeconverter class or inherit the strutstypeconverter class, and then override the methods in the class.

Org. Apache. struts2.util. strutstypeconverter is

Com. opensymphony. xwork2.conversion. impl. ulttypeconverter subclass,

Defaulttypeconverter is the implementation class of typeconverter.

 

If defatypetypeconverter is inherited and the convertvalue method is rewritten, this method is described as follows:

Public object convertvalue (Map <string, Object> context, object value, class totype ){

Context: context under which the conversion is being done

Value: an object to be converted to the given type

Totype: class type to be converted

Return: converted value of the type given, or value ifthe value cannot be converted to the given type.

This method is bidirectional. For example, <s: textfield name = "user"/> corresponds to the user attribute in the action on the page:

Convert the form element string-> User: If (totype = user. Class) {convert the value to string [], get the 1st elements, encapsulate them into user objects, and return the user
}; Form backfilling from user-> String Conversion: If (value instanceof user) {return
String}

Code reference http://blog.csdn.net/furongkang/article/details/6921440

 

If the strutstypeconverter class is inherited, rewrite the convertfromstring method and the converttostring method. From the method name, you will know that one is from object to string, and the other is from string to object. this method is simpler than the preceding class. We recommend that you use this method.

/**

*
Date type converter

*/

PublicclassDatetypeconversion
ExtendsStrutstypeconverter {

PublicstaticfinalString
Datetype= "Yyyy/mm/DD ";

PublicstaticfinalSimpledateformat
Format=NewSimpledateformat (Datetype);

/**

*
Conversion of attributes from form to action

*/

@ Override

PublicObject convertfromstring (MapContext, string [] values,
ClassTotype ){

If(Totype = java. util. Date.Class){

Try{

If (!"". Equals (Values [0]. Trim ()))

ReturnFormat. Parse (Values [0]);

}Catch(Parseexception e ){

E. printstacktrace ();

}

}

Returnnull;

}

 

/**

*
Converts an attribute of action to a string.

*/

@ Override

PublicString converttostring (MapContext, object Value ){

If(Value
InstanceofJava. util. Date ){

ReturnFormat. Format (Java. util. Date) value );

}

Returnnull;

}

}

 

A) Local type converter,

Create a ActionClassName-conversion.properties file under the package where the action class is located, actionclassname is the class name, followed by-conversion. properties is a fixed syntax,
Such as: HelloWorldAction-conversion.properties
The content in the properties file is: property name = full Class Name of the type converter (both package name and class name)
For example, Birthday = com. ljq. type. converter. datetypeconverter

B) Global type conversion

Register the above type converter as a global type converter, create a file under SRC: xwork-conversion.properties, the name is fixed, content format: Java. util. Date = cn. itcast. converter. datetypeconverter

 

There is also an annotation-based type converter that shares the same principle as above. We will introduce it in the struts2 annotation. For more information, see:

Http://blog.sina.com.cn/s/blog_883da5580100w3bt.html

Http://blog.csdn.net/xiaoping8411/article/details/4775936

 

3. struts2 type conversion error handling

The action class is required to inherit actionsupport; otherwise, an error message cannot be added!

In the struts2-default.xml, you can find the package name = "struts-Default"


There is a reference to the above interceptor in <Interceptor-stack name = "defaultstack">. If the conversion fails, for example, an exception is thrown in the interceptor, then, struts2 encapsulates the error information into fielderror and puts it into actioncontext. By default, it enters the view named input.

 

Internationalization of error messages:

If it is a custom converter, the conversion of a specific action property fails: write in the actionname_zh_cn.properties internationalized resource file

Invalid. fieldvalue. attribute name = prompt information

 

If it is the struts2 default type converter and you want to change the English prompt after the conversion fails, write in the global resource file:

Xwork. Default. invalid. fieldvalue = {0} field type conversion failed!

 

4. How to set the date format in struts2

 

Struts. Date. format = yyyy-mm-dd can only control the default value of the format attribute in the <s: Date> label.

Therefore, you need to write the date type converter to implement the custom format.

Global: Java. util. Date = converter. mydatedefatypetypeconverter

Or local user. optime = converter. mydatedefatypetypeconverter

You can use the <s: Properties value = "Birthday"/> label when displaying the information on the page. You must also enter the custom format when entering the information.

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.