Local and global converter of Struts2
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
/Index. jsp
Enter http: // localhost: Port Number/project name/... (namespace) ../list_execute.action? Birthday = 20121181 to complete the jump, in index. jsp outputs the corresponding birthday value through the el expression $ {birthday}, and outputs data with other information such as week, not the original string. The application type converter can achieve two-way data conversion.
There are two types of converters:
Local type converter: used for an action
Global converter: used for all actions
The so-called type converter inherits a class defatypetypeconverter and then overrides the method implementation. The application type converter has two scenarios:
1. Convert the value of the request parameter to the attribute value.
2. Use 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 defatypetypeconverter {@ Overridepublic Object convertValue (Map
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) {// convert the Date type to the String type Date date = (Date) value; return simpleDateFormat. format (date) ;}} catch (ParseException e) {} return null ;}}
Like Struts1, it also needs to be registered in Struts2. Register
Local Converter:
Place the ActionClassName-conversion.properties file under the package where the action class is located, ActionClassName is
Action class name, followed by-conversion. properties is a fixed syntax, for this example, the file name should
HelloWorldAction. properties.
The content in the properties file is: property name = full Class Name of the type converter
For this example, the content in the HelloWorldAction-conversion.properties file is:
Birthday = struts2.example. type. converter. DateTypeConverter
CustomGlobal Converter: Place the WEB-INF file under the xwork-conversion.properties/classes, in the properties File
The content is: the type to be converted = the full Class Name of the type converter
For this example, the content in the xwork-conversion.properties file is:
Java. util. Date = struts2.example. type. converter. DateTypeConverter
In addition, you must pay attention to the input data format symbol requirements when using the global type converter.