Web application interaction is based on HTTP, HTTP request parameters are string types, the server receives the data from the user can only be a string or a character array, and Java is a strongly typed language, the object of the Web application may use such as Integer (int), Floating point (float), date, or custom data type, for which a string parameter is converted to the appropriate data type on the service side.
From an HTML form to an Action object, the type conversion is from a string to the corresponding type, showing the flow of the layer data and the required type conversions, for example,
In Struts2, the work of mapping request parameters to the Action property is the responsibility of the Parameters interceptor, which is a member of the default Defaultstack interceptor. Parameters interceptors can automatically convert between string and basic data types, such as converting to int, float and other simple data types do not need our own definition of the converter to convert, STRUTS2 internally itself provides us with a method of conversion, But like some complex types and our custom data types, we need to write the converter ourselves. STRUTS2 provides good extensibility, and developers can easily develop custom type converters to complete conversions between strings and custom composite types (for example, to complete a conversion of a string to a person instance).
If an unknown exception type converter occurs during a type conversion, the developer does not care about the exception handling logic, and the Struts2 Conversionerror automatically handles the exception and generates a hint on the page.
Most common types do not require developer's attention, they are done by struts2 built-in type converters, and struts built-in type converters have been able to perform type conversions between string types and these types: Boolean and Boolean,char and character, int and Integer,long and long,float and float,double and double,date, arrays, collections, etc.
The developer's concern is to convert the string to a composite type, either by implementing the Typeconvert interface provided by OGNL, or by implementing the Defaulttypeconvert class of the Typeconvert interface, or with the support of the OGNL expression (in a simple way). Implements a custom type converter. First look at the code of an action class.
Public classuser{PrivateString name;PrivateString Pass;//Name Setter and getter method Public void SetName(String name) { This. name = name; } PublicStringGetName(){return This. Name; }//Pass Setter and getter method Public void SetPass(String Pass) { This. pass = Pass; } PublicStringGetpass(){return This. Pass; }}
The action contains a user-type property that requires a type conversion, and the STRUTS2 framework accepts the HTTP request parameters to encapsulate the request parameters into a user object.
The OGNL expression provided by STRUTS2 allows the developer to define the Name property of the form field using the OGNL expression when defining the form field, without any special handling, and the JSP page code is as follows.
<s:form Action="Login"> <!--The form field encapsulates the request parameter named User.Name -- <s:textfield name="User.Name" label="username"/> <!--The form field encapsulates the request parameter named User.pass -- <s:textfield name="User.pass" label="password"/> <tr> <TD colspan="2"><s:submit value="Convert" theme="simple"/> <s:reset value="refill" theme="simple"/></td> </tr></s:form>
The above code defines two lines of single-line text box, corresponding to two request parameters, form request parameter name is not a normal parameter, Instead of the User.name,user.pass form, which is the form of the OGNL expression, struts2 copies the value of the User.Name parameter to the Name property of the action instance's user property and assigns the value of the User.pass parameter to the user property of the action instance. Pass property.
Struts2 the normal request parameters can be converted to composite type objects in this way, but there are a few things to be aware of when using them.
1. Because Struts2 will create an instance of a composite class (user Class) through reflection, the system must provide an parameterless constructor for the composite class.
2. If you want to assign a value to the Name property of the user property of the action instance using the form of the User.Name request parameter, you must provide the SetName () method for the composite class (user Class) that corresponds to the user property. Because Struts2 is called by the method to assign a value to the property. The action class should also contain the GetUser () method.
In extreme cases, collection, or map instances, can be generated directly. See the action class fragment below.
Public class loginaction extends actionsupport{ //Action class contains a member variable of type map //Map's value type is user type PrivateMap<string, user> users;//Users ' setter and Getter methods Public void setusers(Map<string, user> users) { This. Users = Users; } PublicMap<string, user>getusers() {return This. Users; } PublicStringExecute()throwsException {//Map object generated in the Struts 2 package in the console outputSystem.out.println (Getusers ());The control logic is determined based on the user instance of key one in the map collection if(Getusers (). Get ("One"). GetName (). Equals ("crazyit.org") && getusers (). Get ("One"). Getpass (). Equals ("Leegang") {Addactionmessage ("Login Successful!" ");returnSUCCESS; } addactionmessage ("Login failed!! ");returnERROR; }}
Here the action class defines a users property, which is of the type map
<s:form Action="Login"> <s:textfield name="users[' one '].name" label="First user name"/ > <s:textfield name="users[' one '].pass" label="First password" /> <s:textfield name="users[' two '].name" label="The second user name"/> <s:textfield name="users[' two '].pass" label="Second password"/ > <tr> <TD colspan="2"><s:submit value="Convert" theme="simple"/> <s:reset value="refill" theme="simple"/></td> </tr></s:form>
Set the Name property of the form field to the Action property name [' Key value ']. The form of the property name, where the Action property name is the map type attribute contained in the action class, and the next property name is the property name of the compound type object in the Map object. In this way, struts2 can convert the HTTP request parameters into the map attribute.
If you want to access the action's map type, you can also use the OGNL expression, which is the code below.
The user name for key one is:<s:property value="users[' one '].name"/><br/>Key is the password for one:<s:property value="users[' one '].pass"/><br/>The user name for key two is:<s:property value="users['"].name "/><br/>The password for the key is:<s:property value="users['"].name "/><br/>
If you change the Users property in Loginaction to list, that is, if you need struts2 to encapsulate the user request parameter as a list Type property, you can do so by using the OGNL expression, as long as the index specifies which element of the list to convert the request parameter to. The Name property of the form element in the following JSP page implements the conversion of the HTTP request parameter to the list type.
<s:form Action="Login"> <s:textfield name="Users[0].name" label="First user name"/ > <s:textfield name="Users[0].pass" label="First password"/> <s:textfield name="Users[1].name" label="Second user name"/> <s:textfield name="Users[1].pass" label="Second password"/ > <tr> <TD colspan="2"><s:submit value="Convert" theme="simple"/> <s:reset value="refill" theme="simple"/></td> </tr></s:form>
Specifying the first text field when the form field is defined in the code above will convert the request parameter represented by the text field to the Name property of the first element of the Users collection. Users[0].name,struts2.
If you want to output the property values of the collection elements in the List property in the action, you can access them by increasing the index after the collection properties, as shown in the code below.
The username for the first user instance is:<s:property value="Users[0].name"/><br/>The password for the first user instance is:<s:property value="Users[0].pass"/><br/>The username for the second user instance is:<s:property value="Users[1].name"/><br/>The password for the second user instance is:<s:property value="Users[1].name"/><br/>
Struts2 (v) type conversion