Struts 2 reading notes -- type conversion of struts 2

Source: Internet
Author: User

All MVC frameworks must be parsed. HTTP request parameters, and pass the request parameters to the Controller component. Because HTTP request parameters are all string types, but JAVA is a strongly-typed language ., Therefore, MVC the framework must convert these string parameters to the corresponding data type.

Struts 2Provides a very powerful type conversion mechanism.Struts 2Type conversion can be based onOnglExpression, as longHTTPThe parameter nameOnglExpression to make full useStruts 2Type conversion mechanism.

 

1,Struts 2Built-in type converter

Struts 2Most common types can be converted. UseStruts 2The built-in type converter is complete. As follows:

2, Based onOnglType conversion

With the helpOngl,Struts 2The request parameter can be converted to a composite type in another simple way.

 1   Public   Class Loginaction Implements  Action {  2   3       Private  Userbean user;  4       Private  String tip;  5   6       Public  Userbean getuser (){  7           Return  User; 8   }  9   10       Public   Void  Setuser (userbean user ){  11           This . User = User;  12   }  13   14       Public  String gettip (){  15          Return  Tip;  16   }  17   18       Public   Void  Settip (string tip ){  19           This . Tip = Tip;  20   }  21   22       // Request Handling Method  23       Public String execute () Throws  Exception {  24           If (Getuser (). getname (). Equals ("chenssy") & getuser (). GetPassword (). Equals ("chentmt" )){  25 Settip ("converted successfully" );  26               Return  Success;  27  }  28           Else  {  29 Settip ("Conversion failed" );  30               Return  Error;  31   }  32   }  33   34 }

 

Struts 2ReceivedHTTPAfter the request parameters, You Need To encapsulate these parametersUserbeanObject. However, we do not need to do any processing, but only need to useOnglExpression to define the form fieldNameAttribute.

 1    <  S: Form  Action  = "Login"  >  2          <  S: textfield  Name  = "User. Name"  Label  = "User Name"  > </  S: textfield  >  3           <  S: textfield  Name  = "User. Password"  Label  = "Password"  > </ S: textfield  >  4           <  S: Submit  Value  = "Conversion"   > </  S: Submit  >  5           <  S: Submit  Value  = "Reset"   > </  S: Submit >  6      </  S: Form  > 

 

User. Name is used for the request parameter name.AndUser. Password ---This isOnglExpression format.Struts 2The corresponding parameter is assignedActionInstanceUser.

In the above method,Struts 2Common request parameters can be converted to compound objects, but pay attention to the following points.

1BecauseStruts 2A composite class instance is created through reflection. Therefore, the system must provide a non-parameter constructor for this composite class.

2 . If you want to use User. name the request parameter format is action instance User attribute name attribute value, must be User composite classes of attribute objects setname () method.

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.