In the Web layer, we use beanutils to encapsulate the data of the form directly into JavaBean. In Struts2, the action gets asked to be encapsulated directly into the JavaBean. A large number of type converters are available in struts2 to complete data type conversion issues. Contains the following types:
Boolean and Boolean
char and Character
int and Integer
long and long
float and float
double and double
Date can be YYYY-MM-DD format string
array You can convert multiple parameters with the same name to a collection in an array
support to save data to a List or Map collection
The interface of a type converter in Struts2 is: com.opensymphony.xwork2.conversion.TypeConverter
1. Why do you want to customize type conversions.
Because the type conversion provided in STRUTS2 in our actual requirements development, there will be situations that cannot meet the development requirements, such as converting the string 2017/6/27 into time data Data2017 an anomaly on the June 27 of the year, because Struts2 does not have this type of converter, In order to solve our needs problem, we must write a custom type converter ourselves.
2. Custom type converter steps
<1> Create a custom type converter.
There are three ways to create a custom type converter:
(1) Implement TypeConverter interface
Override this method. However, because there are six parameters and too many parameters, it is not recommended. Public
Object Convertvalue (map<string, object> context, Object Target, member member, String PropertyName, Ob Ject value, Class totype);
(2) Inheriting Defaultconverter class
The overridden method parameters do not have so many public
Object convertvalue (map<string, object> context, Object value, Class totype) {
return Convertvalue (value, totype);
(3) Inheriting a subclass of the Defaulttypeconverter class: Strutstypeconverter
This method is recommended because in this class, the data that is passed from the page is encapsulated and how the data in the action is displayed on the page is separated.
//Parameter value: Form to convert value
//Parameter toclass: Type to convert public
abstract Object convertfromstring (Map context, string[) Values, Class toclass);
Public abstract String ConvertToString (Map context, Object O);
<2> rewrite the methods in the interface to implement the type conversion operation.
<3> register the type converter.
How to register a custom type converter.
(1) Local – for action
Location and name of the configuration file: In the package where the action class is located, create the action class name-conversion.properties
Configuration file Writing format: attribute name = Full class name of type converter
(2) Local – for model
Configuration file location and name: In package where model class is located, create model class name-conversion.properties
Configuration file Writing format: attribute name = Full class name of type converter
(3) Global
Configuration file location and name: Create a xwork-conversion.properties under SRC
Profile writing format: The full name of the type to be converted = All class name of the type converter
3. Type converter exception and its handling
For type converters in Struts2, if the form data is submitted and the data is to be model encapsulated, an error occurs, such as: No result defined for action cn.itcast.action.RegistAction and Result input.
Translating exception information means that the input results view is not configured in the Registaction configuration.
If you configure the following information, a type conversion problem occurs and jumps to the specified view.
<action name= "regist" class= "cn.itcast.action.RegistAction" >
<result "Input" name= Success.jsp</result>
</action>
So why would you jump to the input view?
The reason is the Interceptor (Intercepter) in the struts2.
For record type conversion issues
<interceptor name= "Conversionerror"
class= " Org.apache.struts2.interceptor.StrutsConversionErrorInterceptor "/>
//For problem, jump to input view.
<interceptor name= "Workflow" class= "Com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
Because the default interceptor stack is introduced in struts2. In this default interceptor stack, there is an interceptor that is conversionerror, and its role is to store the error message to the action when a type conversion occurs incorrectly. However, at the end of the stack there is a interceptor is workflow, its role is if the previous interceptor found the problem, and the problem is stored in the action, or thrown an exception, then the workflow interceptor will jump directly to the input view.
4. About error message display
By analyzing the Interceptor function, we know that when the type conversion error, automatically jump input view, the Input view page to display the error message.
* In the action package, create actionname.properties and configure the hint information in the local resource file: Invalid.fieldvalue. property name = Error message
If you are a custom type converter, a type conversion problem occurs, to jump to the input view, in the type converter, you must throw an exception.