Struts2 property model and driving model

Source: Internet
Author: User

One, attribute-drivenThe name in each form of a page in a JSP and the properties in the action correspond to it.

<span style= "FONT-SIZE:18PX;" ><form action= "register.do" name= "Registerform" method= "POST" >            username:<input type= "text" Name= " Username "><br>            password:<input type=" Password "name=" Password "><br></form></ Span>

the definition class in action:

<span style= "FONT-SIZE:18PX;" >public class Loginaction extends Actionsupport {    private String username;    private String password;    Public String get username () {        return uname;    }    public void set username (String username) {This        . Username = username;    }    Public String GetPassword () {        return password;    }    public void SetPassword (String password) {This        . Password = password;    }        @Override public    String execute () throws Exception {                return actionsupport.success;}    } </span>

The name attribute in the From form is exactly the same as the property in the action, which is the property driver, and when the form is submitted to action, STRUTS2 automatically assigns the corresponding property setter in the Acton based on the Name property of the form. second, model-drivenThere is a Pojo object in each action that encapsulates the form properties, that is, placing attributes in the object and more object-oriented.

<span style= "FONT-SIZE:18PX;" ><form action= "register.do" name= "Registerform" method= "POST" >            username:<input type= "text" Name= " Username "><br>            password:<input type=" Password "name=" Password "><br></form></ Span>

The Pojo code, in fact, is a common Java Bean:

<span style= "FONT-SIZE:18PX;" >public class User {    private String username;    private String password;    Public String get username () {        return uname;    }    public void set username (String username) {This        . Username = username;    }    Public String GetPassword () {        return password;    }    public void SetPassword (String password) {This        . Password = password;    }} </span>

code in action:

<span style= "FONT-SIZE:18PX;" >public class Loginaction extends Actionsupport implements modeldriven<user> {    Private user user = new user () ;    The model driver must implement a method that is also the only method in the Modeldriven interface public     User Getmodel () {        return user;    }    @Override public    String execute () throws Exception {                return actionsupport.success;}    } </span>

There is a big difference between a property-driven action:

(1) The model-driven action must implement the Modeldriven interface, and to provide the corresponding generics, here is of course the Java bean used specifically.

(2) The implementation of the Getmodel method of Modeldriven is actually a simple return to the generic type of an object.

(3) The action provides a generic private object, here is the user object that defines a user, and provides the appropriate getter and setter.

Which one do you use? Suggestions:

(1) Ask you to unify the drive model that is used by the action in the entire system, either by attribute-driven or by model-driven.

(2) If the persistence Layer object in your db corresponds to one by one of the properties in the form, then use the model driver, after all, it looks like the code is much neater.

(3) If the form's properties are not one by one corresponding, then you should use attribute-driven, otherwise, your system must provide two beans, one corresponding to the form submitted data, the other with the persistence layer.

three, the realization principleHow does the system encapsulate the request parameters into the model property of Loginaction? We look at the Sttuts-default.xml file again:

The system's default interceptor stack is configured in the above configuration fragment, which contains two interceptor references: Model-driven and params, both of which refer to the Model-driven and params interceptors in the system. The params interceptor is responsible for extracting the request parameters, and if the attribute-driven mode is used, it is also responsible for passing the request parameters to the properties of the action instance, while the Model-driven interceptor is responsible for passing the request parameters to the properties of the model.

Attribute-driven, for example, there is a label on the page name= "user" value= "admin". The final submission to the background will be in the form of user=admin. After being informed by the interceptor, it will find out if the action has the user parameter, and if found, call Setuser to assign the admin to user. When the request is returned to the JSP page when the JSP is compiled into a servlet, if the page has name= "user" label, or directly with ${user}, the interceptor invokes the GetUser method of the user parameter in action to send the value of user through responce.

Iv. Summary

SSH model driver, just start not to know what things, feel very magical, now a summary, enlightened.





Reference article: http://www.blogjava.net/jjshcc/archive/2010/06/17/323750.html


Struts2 property model and driving model

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.