Struts2 type conversion, struts2 type
1. type conversion in Struts2
We know that the data submitted to the background through HTTP is in the string format, and the data type we need is of course not only a string type. Therefore, type conversion is required!
In Struts2, the concept of type conversion is not only used to convert the string passed through the interface to a specific type (convertFromString, it can also be used to convert a specific type to a string (that is, when we want to present a certain type of object in JSP, we want to convert it to a string for display) (converterToString ).
In Struts2, global type conversion and local type conversion are supported. Global type conversion means that all actions/models use the same type converter to convert objects of a specific type, it refers to the specific type converter defined for a property in an action/model.
Question 1: How to overwrite the default error message?
1). Create a new one in the package where the corresponding Action class is located.
ActionClassName. properties file, ActionClassName is the class name of the Action class containing the input field
2) Add the following key-value pairs to the property file: invalid. fieldvalue. fieldName = xxx
Question 2: If it is a simple topic, will the error message be automatically displayed? What if it is not displayed?
1) through the debug tag, we can see that if a conversion error occurs, there will be a fieldErrors attribute in the Action (implemented ValidationAware Interface) object of the value stack.
The attribute type is Map <String, List <String> key: field (attribute name), value: List composed of error messages. Therefore, you can use LE or OGNL.
Error message: $ {fieldErrors. age [0]}
2). You can also use the s: fielderror label to display. You can use the fieldName attribute to display errors of the specified field.
Question 3. If the simple topic is used and <s: fielderror fieldName = "age"> </s: fielderror> is used to display the error message, the message is displayed in
Ul, li, and span. How to remove ul, li, and span?
Fielderror. ftl under template. simple defines the style of the error message in the s: fielderror label under the simple topic. Therefore, modify
You can create the template. simple package under src, create the fielderror. ftl file, and add the content in native fielderror. ftl.
Copy to the newly created fielderror. ftl and remove ul, li, and span.
Question 4: How do I customize a type converter?
1). Why do I need a custom type converter? Because Struts cannot automatically convert strings to reference types.
2). How to define a type converter:
I. Development type converter class: Extended StrutsTypeConverter class.
II. configuration type converter:
There are two methods
①. Field-Based Configuration:
> Create a New ModelClassName-conversion.properties file under the Model (possibly Action, possibly a JavaBean) where the field is located
> In this file, enter the key-Value Pair fieldName = full Class Name of the type converter.
> The instance is created when the converter is used for the first time.
> The type converter is for a single instance!
②. Type-Based Configuration:
> New xwork-conversion.properties under src
> Type: type to be converted = full Class Name of the type converter.
> Create an instance when the current Struts2 application is loaded.
How to Write a type converter?
Whether it is global type conversion or local type conversion, the converter compilation method is the same! You only need to inherit StrutsTypeConverter and override the method.
Global type conversion
Defines the converter for the same type of attribute in the entire system.
Only:
1.Root directory of the class pathDefine xwork-conversion.properties files
2. Declare which type of converter is used in this format in the file:
A)Full path Class Name of attribute type = full path Class Name of Converter
3. Struts2 will automatically discover this file and call the specified type converter for Type Conversion Based on the definition.
For example, Point type:
PackageCn.com. leadfar. model; Public ClassPoint { Private IntLeft; Private IntRight; Public IntGetLeft (){ ReturnLeft; } Public VoidSetLeft (IntLeft ){ This. Left = left; } Public IntGetRight (){ ReturnRight; } Public VoidSetRight (IntRight ){ This. Right = right; } } |
Point type converter:
PackageCn.com. leadfar. struts2.actions; ImportJava. util. Map; ImportOrg. apache. struts2.util. StrutsTypeConverter; ImportCn.com. leadfar. model. Point; Public ClassPointConverterExtendsStrutsTypeConverter { @ Override PublicObject convertFromString (Map context, String [] value, Class toType ){ //TODOPrerequisite judgment String p = value [0]; String [] ps = p. split (","); IntLeft = Integer.ParseInt(Ps [0]); IntRight = Integer.ParseInt(Ps [1]); Point point =NewPoint (); Point. setLeft (left ); Point. setRight (right ); ReturnPoint; } @ Override PublicString convertToString (Map context, Object point ){ Point p = (Point) point; ReturnP. getLeft () + "-" + p. getRight (); } } |
The xwork-conversion.properties file is as follows:
Cn.com. leadfar. model. Point = cn.com. leadfar. struts2.actions. PointConverter |
Local type conversion
That is, the converter defined for an Action or Model attribute.
1.Same as Action/Model classSurface, definitionAction/Model class name-conversion. propertiesFile
2. The file uses the following format to declare which attribute needs to be used and which type converter:
Attribute name = full path Class Name of the converter
For example, for the java. util. Date type, we can declare that different classes use different types of converters.
PackageCn.com. leadfar. model; ImportJava. util. Date; Public ClassUser { PrivateDate endDate; PublicDate getEndDate (){ ReturnEndDate; } Public VoidSetEndDate (Date endDate ){ This. EndDate = endDate; } } |
PackageCn.com. leadfar. struts2.actions; ImportJava. util. Date; ImportCn.com. leadfar. model. Point; ImportCn.com. leadfar. model. User; ImportCom. opensymphony. xwork2.ModelDriven; Public ClassUserActionImplementsModelDriven { PrivateUser user; PrivateDate beginDate; @ Override PublicObject getModel (){ If(User =Null){ User =NewUser (); } ReturnUser; } PublicString add (){ Return"Success "; } PublicUser getUser (){ ReturnUser; } Public VoidSetUser (User user ){ This. User = user; } PublicDate getBeginDate (){ ReturnBeginDate; } Public VoidSetBeginDate (Date beginDate ){ This. BeginDate = beginDate; } } |
Assume that you want the beginDate attribute in the UserAction class and the endDate attribute in the User class to use different types of converters, as shown below:
PackageCn.com. leadfar. struts2.actions; ImportJava. text. ParseException; ImportJava. text. SimpleDateFormat; ImportJava. util. Map; ImportOrg. apache. struts2.util. StrutsTypeConverter; Public ClassBeginDateConverterExtendsStrutsTypeConverter { PrivateSimpleDateFormat format =NewSimpleDateFormat ("yyyy-MM-dd "); @ Override PublicObject convertFromString (Map context, String [] value, Class toType ){ String d = value [0]; Try{ ReturnFormat. parse (d ); }Catch(ParseException e ){ E. printStackTrace (); } Return Null; } @ Override PublicString convertToString (Map context, Object date ){ ReturnFormat. format (date ); } } |
PackageCn.com. leadfar. struts2.actions; ImportJava. text. ParseException; ImportJava. text. SimpleDateFormat; ImportJava. util. Map; ImportOrg. apache. struts2.util. StrutsTypeConverter; Public ClassEndDateConverterExtendsStrutsTypeConverter { PrivateSimpleDateFormat format =NewSimpleDateFormat ("yyyy/MM/dd "); @ Override PublicObject convertFromString (Map context, String [] value, Class toType ){ String d = value [0]; Try{ ReturnFormat. parse (d ); }Catch(ParseException e ){ E. printStackTrace (); } Return Null; } @ Override PublicString convertToString (Map context, Object date ){ ReturnFormat. format (date ); } } |
Then, we need to create a file under the User class package: cn.com. leadfar. model, named as follows:
User-conversion.properties, the content of the file is as follows:
EndDate = cn.com. leadfar. struts2.actions. EndDateConverter |
Indicates the endDate attribute in the User class. The EndDateConverter type converter is used.
Then, in the package of the UserAction class: cn.com. leadfar. struts2.actions, create a file named as follows:
UserAction-conversion.properties, the content of the file is as follows:
BeginDate = cn.com. leadfar. struts2.actions. BeginDateConverter |
Indicates the beginDate attribute in the UserAction class. The BeginDateConveter type converter is used.
Note: In Struts1, only global type conversion is supported, but local type conversion is not supported!
You can configure initialization parameters in web. xml.
Type conversion works with complex attributes (attributes can be mapped to one attribute)
Type conversion and Collection are used in combination (common in scenarios where batch data needs to be quickly input)