Type conversion instance code in struts 2

Type conversion

All the data transmitted by the page and controller are of string type. Various data types may be used when processing them. The program cannot automatically complete the data type conversion, which requires manual operation in the code. This process is called type conversion.

Built in type converter

In web applications, the data entered by users in the view layer is a string. When the business control layer processes these data, it must type convert the string passed from the view layer. Struts 2 provides a simple and easy-to-use data type conversion mechanism. The type conversion provided by struts 2 is as follows:

1) String: convert array objects of type int, long, double, Boolean, string to string 2) Boolean / Boolean: convert between string and Boolean 3) character / character: convert between string and character 4) int / integer, float / float, long / long Double / double: convert between string and numeric data. 5) date: convert between string and date class. For date types, input and output are processed in short format, Use the locale associated with the current request to determine the date format (6) array type (array): because the array element itself has a type, Struts2 uses the type converter corresponding to the element type to convert the string to the type of the array element, and then set it to the new array. 7) collection, list, set: Struts2 will use the getparametervalues (string STR) method of the request object for the string data submitted by the user, Converts the returned string data to a collection type

Ognl expression

Struts 2 framework supports ognl expression, which can convert user requests to composite types.

Using type conversion annotations

Struts 2 provides some type conversion annotations to configure the converter so that it can replace classname conversion Properties file, including the following comments:

1) Typeconversion annotation. This annotation applies at the attribute and method levels. 2) Conversion annotation. The conversion annotation allows type conversions to be applied at the type level, that is, to class, interface, or enumeration declarations. The annotation has only one parameter, conversions. 3) Element annotation. The element annotation is used to specify the element type in the collection or map. This annotation can only be used at the field or method level. 4) Key annotation. The key annotation is used to specify the type of key in the map. This annotation can only be used at the field or method level. 5) Keyproperty annotation. The keyproperty annotation specifies the attribute name used to index the collection element. This annotation is only applicable to the field or method level 6) createlfnull annotation. The createifnull annotation specifies whether to let the framework recreate the collection element when the referenced collection element is null. This annotation applies only at the field or method level

A simple example of adding commodity information: after configuring the struts 2 environment, the commodity class:

Action:

struts. Configuration in XML:

Successfully added page: <% @ taglib prefix = "s" URI = "/ struts tags"% >

Custom type converter instance:

The commodity class uses the above class, action,

to configure:

Add a global type converter:

xwork-conversion. properties, com. mxl. entity. Product=com. mxl. converter. ProductConverter

Add interface:

Jump interface after successful addition:

Composite type conversion exception handling instance:

User class,

to configure:

Add local resource file: user exceptionaction properties,

Content:

Registration page Z:

Jump interface:

summary

The above is all about the example code of type conversion in struts 2. I hope it will be helpful to you. Interested friends can refer to this site: struts 2 realizes projection through ognl expression, struts 2 development process and detailed configuration, struts 2 modification upload file size limit method analysis, etc. if there are deficiencies, please leave a message to point out. Here are some related books to download for your reference. I also hope friends will give more support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>