Two ways for struts 2 to accept parameters (property driven and model driven)

definition:

  • In struts 2, there are basically two ways to transfer data and action (control layer), one is property driven, the other is model driven.

Attribute driven

  1. Attribute correspondence of basic data type:
    It is the name property of html tag on the Web page or the getter/setter corresponding to the property. When the page is submitted, struts 2 will automatically retrieve the data from the request object, and then set it to the Action attribute according to the name.
    Disadvantages: if there are too many entity properties, there will be many getter/setter methods, and the Action class will be very messy
    public class LoginAction extends ActionSupport{
    
        private String userName;
        private String password;
    
    
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String login(){
    
            return "login";
        }
    }
    
    //Front page
    <body>
        <form action="${basePath }/login_login">
        //User name: < input type = "text" name = "username" / >
        //User password: < input type = "text" name = "password" / >
                <input type="submit" value="Submission"/>
        </form>
    </body>
    2. Use domain objects directly (recommended):
    In order to solve the above problems, struts 2 provides a direct use of property driven domain objects (that is, to move the properties and corresponding getter/setter methods out of Action, encapsulate them into a domain object separately, and then use the domain object directly in the Action class). What needs to be changed is to add a prefix of domain object to the corresponding name attribute, indicating which domain object this value corresponds to. (setter method required)
    //Front page
    <body>
        <form action="${basePath }/login_login">
        //User name: < input type = "text" name = "user. Username" / >
        //User password: < input type = "text" name = "user. Password" / >
                <input type="submit" value="Submission"/>
        </form>
    </body>
    
    //The Action class encapsulates the attribute as a user entity and instantiates an object user
    public class LoginAction extends ActionSupport{
    
        //Domain model driven
        User user = new User();
    
        public String login(){
    
            return "login";
        }
    }

    Model driven

    • Implementation method: let Action implement a ModelDriven interface. This interface requires us to implement a getModel method, which returns the data Model object used by Action. In the way of Model driven, only one Model is needed for an Action, so the name in the web interface does not need to be prefixed. (setter method required)
    <form action="${basePath }/login_login">
        //User name: < input type="text" name="userName" />
        //User password: < input type="text" name="password" />
                <input type="submit" value="Submission"/>
    </form>
    
    //LoginAction class
    public class LoginAction extends ActionSupport implements ModelDriven{
    
        User user = new User();//Model driven also needs to be instantiated
    
        public String login(){
    
            return "login";
        }
    
    
        @Override
        public Object getModel() {
            return user;
        }
    }

Tags: Attribute Struts

Posted on Mon, 04 May 2020 10:34:45 -0700 by laurton