Although the built-in type converters provided by STRUTS2 can satisfy the overwhelming majority of requirements, it is sometimes necessary to use a custom type converter to implement specific requirements. Because STRUTS2 cannot automatically complete the conversion of a string to a reference type
Requirement: Implement date conversion in the specified format YYYY-MM-DD
1. Preparing the UserAcrion2 class
@SuppressWarnings ("Serial") public class UserAction2 extends Actionsupport{private String name;private Integer age; Private Date birth;public String getName () {return name;} public void SetName (String name) {this.name = name;} Public Integer Getage () {return age;} public void Setage (Integer age) {this.age = age;} Public Date Getbirth () {return birth;} public void Setbirth (Date birth) {This.birth = birth;} Public String AddUser () {System.out.println ("User2"); return SUCCESS;}}
2.index.jsp
3. Custom type converters need to inherit Strutstypeconverter
public class Dateconversion extends Strutstypeconverter {private DateFormat df = null; Private DateFormat GetDateFormat () {if (df = = null) {ServletContext context = Servletactioncontext.getservletcontext (); * * in Web. XML defines the pattern type */string pattern = context.getinitparameter ("pattern"), if (pattern = = NULL | | Pattern.trim (). IsEmpty ()) {pattern = "yyyy-mm-dd";} DF = new SimpleDateFormat (pattern);} return DF;} @Overridepublic Object convertfromstring (Map arg0, string[] values, Class obj) {if (obj = = Date.class) {if (values! = NULL && values.length > 0) {String value = values[0];try {System.out.println (GetDateFormat (). Parseobject (value)) ; return GetDateFormat (). Parseobject (value);} catch (ParseException e) {throw new RuntimeException (e);}}} return values;} @Overridepublic String converttostring (Map arg0, Object obj) {if (obj instanceof date) {Date date = (Date) Obj;return getd Ateformat (). Format (date);} return null;}}
4. Configure There are two types of type conversion configuration methods:
One, field-based configuration
1. Under the package of the Model (Action/javabean) where the field is located, create a new modelclassname-conversion.properties file
2. Enter the key-value pair: The full class name of the Fieldname= type converter.
File name Useraction2-conversion.propertiesbirth=cn.cil.conversion.dateconversion
PS: The first time you use this converter to create an instance, so the type converter is a single instance!
II. Type-based configuration
The UserAction2 only attributes are extracted to form a javabean.
public class Useraction extends Actionsupport implements modeldriven<user>{private user user = new User ();p ublic use R GetUser () {return user;} public void SetUser (user user) {this.user = user;} Public String AddUser () {System.out.println ("User1"); return SUCCESS;} @Overridepublic User Getmodel () {return user;}}
Create a new xwork-conversion.properties under SRC
Type to convert = Full class name of the type converter
Java.util.date=cn.cil.conversion.dateconversion
PS: Create an instance when the current Struts2 app is loaded
Overriding the default error message:
1). New in the package where the corresponding Action class is located
Actionclassname.properties file, Actionclassname is the class name of the Action class that contains the input field
2). Add the following key-value pairs in the properties file: Invalid.fieldvalue.fieldname=xxx
Useraction.properties
Invalid.fieldvalue.birth=\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u751f\u65e5
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
About Struts2 (iv)-Custom converters