Automatic encapsulation of request data in struts

Source: Internet
Author: User

The Struts 2 framework sets the form's parameters to the properties of the corresponding action in the same way. the work is mainly done by the parameters interceptor. In this interceptor , the conversion between string and the basic data type has been implemented automatically. In struts, interceptors are used by default

class= "Com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

The request data is automatically encapsulated, which automatically transforms the data submitted in the JSP (base data type, String, and date).

Sometimes, you need to map form fields to different properties of multiple objects, and the form label can be mapped to the property of an attribute in the action. Struts 2 first creates an address object from the default constructor of address by using reflection technology, and then invokes the setter method of the address in the call with the property with the same name as the request parameter by using reflection technology to get the request parameter value.

1. Two kinds of request data

If the action in the property is only the basic data type, you need to give the set () method, The Get () method can not give. The data name of the form needs to match the name of the property in the action class, and struts completes the corresponding auto-transform, and the default format for the Date object is YYYY-MM-DD.

If the property in action is an object type, you must give the Get () and set () methods, and the form's data name is the object. The way the property is.

<formAction= "${pagecontext.request.contextpath}/user_register.action"Method= "POST">User name:<inputtype= "text"name= "User.username"><BR>Password:<inputtype= "Password"name= "User.password"><BR>Age:<inputtype= "text"name= "User.age"><BR>Birthday:<inputtype= "text"name= "User.birth"><BR>    <inputtype= "Submit"value= "Submit"><BR></form>
Custom Converters in 2.struts

If the existing converter does not meet your needs, you can customize the converter. There are two types of converters in struts: Local type converters, global type converters. The procedure for customizing the converter is:

    • Inherit Strutstypeconverter, rewrite the abstract method in convert;
 Public classMyconverterextendsstrutstypeconverter{//the formats to be supported are: YYYY-MM-DD, YYYYMMDD, yyyy mm month DD Day//pre-defined three formatssimpledateformat[] formats = {            NewSimpleDateFormat ("Yyyy-mm-dd"),            NewSimpleDateFormat ("YYYYMMDD"),            NewSimpleDateFormat ("yyyy mm month DD Day")    }; /*** Converts a string to the specified type *@paramMap Current Context *@paramstrings The value of the string submitted by the JSP Codex *@paramAClass the type of target to convert to*/@Override PublicObject convertfromstring (map map, string[] strings, Class aclass) {System.out.println ("Into Conversion"); //content cannot be empty            if(Strings = =NULL|| Strings.length = = 0)                return NULL; //type must be a date            if(Date.class!=AClass)return NULL;  for(intI =0;i<formats.length;i++){            Try {                returnFormats[i].parse (strings[0]); } Catch(ParseException e) {//throw new RuntimeException ("The first I conversion failed", e);                Continue; }        }        return NULL; } @Override PublicString converttostring (map map, Object o) {return NULL; }}
    • Notify struts to use the converter;

If, as a local type converter, creates a new Actionclassname-conversion.properties file in the action class directory that needs to be converted, the file is written in: The field name that needs to be converted = The fully qualified name of the custom converter class, for example:

Birthday=cn.itcast.convertor.datetypeconvertor

If you are a global type converter, you need to create a new Xwork-conversion.properties file in the project src directory and write it in the file:

The class type that needs to be converted = The fully qualified name of the converter, for example:

Java.util.date= Cn.itcast.converter.DateConverter

Automatic encapsulation of request data in struts

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.