Type conversions for STRUTS2
The type conversion of Struts2 is based on Xwork , which is actually based on OGNL , which is a powerful expression language
Struts2 is mainly done by implementing the TypeConverter interface in the OGNL class to complete the type conversion
This interface is more complex, it just describes the most common case, so it is usually not directly implemented by the interface
OGNL itself provides a class that implements the TypeConverter interface, which is the Defaulttypeconverter class
Our class inherits directly from the Defaulttypeconverter class and then overrides its convertvalue () method to
The Defaulttypeconverter class provides two Convertvalue () methods
Convertvalue (Map context, Object target, Member member,string PropertyName, Object value, Class totype)
Convertvalue (Map context, Object value, Class totype)
The second method is a simplification of the first method, and we just need to rewrite the second simpler method.
The Struts2 itself, by rewriting the Convertvalue () method, implements the type conversion of the data.
The context of the parameter map type represents the application contexts. This parameter is not available for the moment.
The value of the Parameter object type represents the values for which the type conversion is required
The totype of the parameter class type represents the target type to be converted to
There are usually two types of situations where a type conversion occurs. The first case is the conversion from the client string to the custom type
The second case is when you output content in a page, from a custom type to a string conversion
So ToType decides whether to convert from string to custom type, or from custom type to string.
So the totype here is divided into two situations, and it is necessary to judge ToType in the Convertvalue () method.
Struts2 also incorporates a simplified type conversion, the Org.apache.struts2.util.StrutsTypeConverter class
It is an abstract class that inherits the Ognl. Defaulttypeconverter class
It has a converfromstring (Map context, string[] values, Class toclass) method
Used to convert a string type to an object type, so its return value is type Object
There is also a convertostring (Map context,object O) method to convert an object type to a string type
The custom data type conversion in the actual project development is also implemented by inheriting the Strutstypeconverter class .
Defining the Pointaction-conversion.properties Property file
This file is used to specify a property file for the type conversion of the Pointaction property, which should be in the same package as the action
When the property file is defined, the -conversion.properties is fixed, and then the specific action is specified before it
Then, in Pointaction-conversion.properties, specify which of the properties in the Pointaction to convert the type
For example, add the line code to the property file point=com.jadyer.converter.pointconverter
So Struts2 know that we are going to use the Pointconverter class to convert the Point property in Pointaction
If you need to convert multiple properties of Pointaction, you can add multiple lines of similar code to the property file
STRUTS2 properties file for global type conversions
The filename of the property file for the STRUTS2 global type conversion is fixed, that is, xwork-conversion.properties
The file can only appear in the entire application, and it must be in the same directory as the struts.xml
It configures content such as com.jadyer.bean.Point = Com.jadyer.converter.PointConverter
A variable that indicates that all of the point types of the entire application are converted using the pointconverter converter
specific process for type conversion
After the client submits the form, the first step is toStruts.xmlTo find out if there is a name namedPointconverterOf<action/>
After finding it, I knew it wasPointaction classAn object to process our request < br/> The process is now in the pointaction,generates an instance of the Pointaction
When this instance is generated, it will first be based on the form that is submittedName PropertyTo get the value of an input field.
And then put these values of one by one callssetter ()Method assigns to each attribute in the Pointaction
If it checks for a property file that does not have a custom type conversion defined, it follows its own default behavior for type conversion
Forint, String, DateSTRUTS2 will automatically complete the type conversion, butObject Type, you mustManual Conversion
That is, if it finds that there is apointaction-conversion.propertiesFile words
It will look in the file to see which of the attributes in the pointaction are converted, and the corresponding transformation class
The process then switches to the transformation class, and the conversion is customized according to the code in the Transformation class
Finally, the returned value is assigned to the property in Pointaction
When you assign a value to all the properties of Pointaction, you execute theExecute ()method, and then turned to theoutput.jspIn
It will then call<s:property/>In thevalueValue that corresponds to theGetter ()Method gets the parameter value
But when the point property is output, it checks for a custom type conversion
The results are found, then the process goes toPointconverterAnd finally outputs its return value to the<s:property/>The location
Type conversions for STRUTS2