Struts2 core knowledge

Strust2 get / set automatically get / set data

1. custom action, add attributes

public class HelloStuts2Action implements Action{
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

 

2. Jump new jsp page, call the property name

<body>
    ${name}HELLO WORD!
</body>

 

ActionSupport class introduced

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable 

 

1. New action inheritance Actionsupport

public class HelloStuts2Action2 extends ActionSupport{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        System.out.println ( "the implementation of the action the default method" );
         // TODO Auto-Generated Method, Stub 
        return Action.SUCCESS;
    }

}

 

2.Action setting data

The first way: attribute drive (FieldDriven) 

 A, the basic data type attribute

public class HelloStuts2Action2 extends ActionSupport{

    private String name;

 

B, JavaBean type attribute

public class User {
    private String name;
    private String pwd;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", pwd=" + pwd + "]";
    }
    
}

 

test

1. set up a test environment, the new login page

login page

<body>
    <form action="" method="post">
        用户名:<input name="name" type="text" /><p/>
        密    码:<input name="pwd" type="text" /><p/>
        <input type="submit" value="登陆" /><p/>
    </form>
</body>

 

success page

<body>
    Username: $ {name}
    Username: $ {pwd}
</body>

 

error page

<body>
    account or password incorrect! Login failed
</body>

 

2. Configure struts.xml

		<action name="user_login" class="com.lin.action.UserAction">
			<result name="success">succssful.jsp</result>
			<result name="error">error.jsp</result>
		</action>

3.action logical storage

Basic data type attribute:   

@Override
    public String execute() throws Exception {

        if (name.equals("lin") && pwd.equals("123")) {
            return Action.SUCCESS;
        }

        return Action.ERROR;
    }

JavaBean property type    

    public String execute() throws Exception {

        LoginSerivce serivce = new LoginSerivce();
        boolean result = serivce.isLogin(new User(name, pwd));
        if (result) {
            return Action.SUCCESS;
        }
        return Action.ERROR;
    }

 


The second way: model-driven (ModelDriven) only accept a bean object

1. Implement Interface

public class UserAction extends ActionSupport implements ModelDriven<User>{

 

2. Re-getmodel method

    User user = new User();
    @Override
    public User getModel() {
        return user;
    }

 

 

Guess you like

Origin www.cnblogs.com/linbin7/p/11965213.html