Struts2 Property Driver (in fact, the parameter "old way servlet API encapsulates data into JavaBean (or beanutils)" in struts2 action, where the property driver is the new way of handling the request

Source: Internet
Author: User

1. Attribute-driven

A\ General Set

1  Public classUseractionextendsActionsupport {2 3 PrivateString username;4 PrivateString password;5 6  Public voidSetusername (String username) {7  This. Username =username;8 }9 Ten  Public voidSetPassword (String password) { One  This. Password =password; A } -  - @Override the  PublicString Execute ()throwsException { -System.out.println ("Username:" + username + "----Password:" +password); - returnNONE; - } +}
Set Mode

Page layer:

1 <formAction= "${pagecontext.request.contextpath}/user.action"Method= "POST">2 User name3             <inputtype= "text"name= "username" />4             <BR/>5 Password6             <inputtype= "Password"name= "Password" />7             <BR/>8             <inputtype= "Submit"value= "Submit" />9         </form>
JSP page

B\ognl Method implementation

1     <formAction= "${pagecontext.request.contextpath}/user2.action"Method= "POST">2 User name3             <inputtype= "text"name= "User.username" />4             <BR/>5 Password6             <inputtype= "Password"name= "User.password" />7             <BR/>8             <inputtype= "Submit"value= "Submit" />9         </form>
JSP page

Action side:

1  Public classUser2actionextendsActionsupport {2 3     Privateuser user;4 5      PublicUser GetUser () {6         returnuser;7     }8 9      Public voidsetUser (user user) {Ten          This. user =user; One     } A  - @Override -      PublicString Execute ()throwsException { the System.out.println (user.tostring ()); -         returnNONE; -     } -}
Action Side

The encapsulation of Struts2 parameters is divided into two categories:

* Attribute-driven:

* Model-driven:

Property-Driven: A set method that provides properties, encapsulating parameters: (Property driven)

Page:

User name: <input type= "text" name= "username" ><br/>

Action:

public class ParamAction1 extends actionsupport{

Private String username;

public void Setusername (String username) {

This.username = Username;

}

}

Question one: is the global variable provided in action, is it thread-safe or unsafe?

* Global variables are provided, and if the threads are unsafe, the global variables will have a thread security problem. The test provides a construction method. Discovers that each request creates an instance of the action. Indicates that the action is a multi-instance. No thread security issues.

Question two: Who completed the parameter encapsulation?

* <interceptor name= "params" class= "Com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

This is not particularly good! Encapsulates a single property and needs to be encapsulated into a JavaBean object itself. Passed to the business layer.

Second, through the OGNL expression of the package parameters: (Property-driven)

Page:

User name: <input type= "text" name= "User.username" ><br/>

Action:

public class ParamAction2 extends actionsupport{

private user user;

public void SetUser (user user) {

This.user = user;

}

Public User GetUser () {

return user;

}

}

OGNL expression encapsulation, which provides a set and get method for an object in the background action. The Get method must be provided. Get gets the object created to the framework. (encapsulates the encapsulation into different objects.)

The property driver in the

Struts2 (which is actually the parameter in struts2 action that handles the request "old way Servlet API encapsulates data into JavaBean (or beanutils)"), where attribute-driven is the new way

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.