There is a field in the action that is of the birthday or date type. When we pass the value, it must be written as to automatically convert the type, however, in some cases, you may need to write 19990101 so that automatic type conversion is not performed by default. In this way, you can write a custom type converter to achieve conversion.
Struts2 has two types of converters:
Partial: it applies to an action conversion type.
First, you need to customize the type converter:
Public class datetypeconverter extends defaulttypeconverter {@ overridepublic object convertvalue (Map <string, Object> context, object value, class totype) {simpledateformat dateformat = new simpledateformat ("yyyymmdd "); try {If (totype = date. class) {// string [] Params = (string []) value when the string is converted to the date type; // request. getparametervalues () return dateformat. parse (Params [0]);} else if (totype = string. class) {// when date is converted to a string, date = (date) value; return dateformat. format (date) ;}} catch (parseexception e) {} return NULL ;}}
To register:
Creating the HelloWorldAction-conversion.properties file helloworldaction is the action to be registered followed by the prescribed statement
Which of the following attributes of the file content is converted to a type converter in action:
Birthday = cn. Test. type. converter. datetypeconverter
Global: it applies to all action conversion types.
Create a xwork-conversion.properties file under SRC
Content: Java. util. Date = cn. Test. type. converter. datetypeconverter