Struts2 type conversion

Source: Internet
Author: User

Struts2 type conversion
In struts development, objects must be given the get method.

Struts type conversion

LWhy type conversion in Struts2?

HTML form data collection --> submit form --> Action

The underlying layer relies on HTTP to transmit data, but HTTP does not have the concept of "type ".

Form input can only be a string or a string array. Therefore, the server-side Action

The String must be converted to the specific data type required by the business.

LIn Struts2, how does one pass request parameters to Action?

The Struts2 framework sets the form parameters to the corresponding Action attributes in the same name.

This work is mainly done by the Parameters interceptor. The interceptor has been automatically implemented.

Conversion between String and basic data types. Similar to: Beanutils tool.
When JSP submits data in struts, struts automatically converts the data type to the attribute type in the action. The basic data type and date type are automatically converted. The date type only supports automatic conversion in the YYYY-MM-dd format. If the data obtained from other date formats is null, the page jumps, but an error is reported in the background. For the date type, you need to customize the type converter for processing.
If an error occurs during automatic type conversion, the struts framework will find the result named input for processing by default. Therefore, developers need to configure a result named input in red in the result configuration of this action. The premise is that this action inherits ActionSupport.
Custom converters are classified into global converters and local converters.

 

If garbled characters occur during request parameter encapsulation, check whether the form submission method is post. The default encoding method of Struts2 is UTF-8.

 

Struts API converter StrutsTypeConverter class, which implements the TypeConverter Interface

 

| -- Defatypetypeconverter

 

| -- StrutsTypeConverter the converter compiled by the user inherits this class.

 

LHow do I customize the type converter in Struts2?

Interface à implementation class

LHow do I configure a custom CONVERTER IN Struts2?

1. Custom converter inherits StrutsTypeConverter

 

2. Rewrite the convertFromString and convertToString methods.

 

3. register the converter

 

3.1 create Action name-conversion. properties in the Action package

3.2 Add the following field names to be converted to the 3.1 file = Full qualified names of the custom converter class

Birthday = cn. itcast. convertor. DateTypeConvertor

LSummary

The above converter registration is coupled with the Action name, so it can only be used internally in your own Action, called the local converter registration method.

 

How do I define a global converter?

 

The Implemented interfaces and inherited classes are the same. In essence, they are configured in different ways.

LImplementation

1. Custom converter inherits StrutsTypeConverter

 

2. Rewrite the convertFromString and convertToString methods.

 

3. register the converter

 

3.1 create the following fixed files under the src directory of the project

 

3.2 Add the following data to file 3.1:

 

Type of the class to be converted = fully qualified name of the converter class

 

For example, java. util. Date = cn. itcast. converter. DateConverter

LSummary

The interceptor intercepts error messages.

 

Class = "org. apache. struts2.interceptor. StrutsConversionErrorInterceptor"/>

 

The form tag can be mapped to an attribute in the Action or to a collection object..

Struts2 first calls the default constructor of address to create the address object through reflection technology, and then calls the setter method of the attribute with the same name as the request parameter in address to obtain the value of the request parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.