Struts2_2 数据封装机制

对极客学院视频的学习笔记

1.属性驱动

jsp页面

   <form action="<%=path %>/loginAction.action" method="post"> 
            pusername:<input type="text" name="username"><br>
            password:<input type="password" name="password"><br>
            <input type="submit" value="tijiao">
        </form>muserlogin
        <h1>anther mode</h1>

action页面

package com.jikexueyuan.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
        private String username;
        private String password;

        @Override
        public String execute() throws Exception {

            System.out.println(username);
            System.out.println(password);

            return "success";
        }

        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;
        }


}

struts.xml

<package name="userlogin" extends="struts-default">
        <action name="loginAction" class="com.jikexueyuan.action.LoginAction">
            <result name="success">/index.jsp</result>
        </action>

2.模型驱动

jsp

<form action="<%=path %>/muserlogin.action" method="post">
            username:<input type="text" name="username"><br>
            password:<input type="password" name="password"><br>
            <input type="submit" value="tijiao">
        </form>

action

注意:
1.接口的实现
2.必须实例化us
3.username password必须与jsp页面相对应

public class MloginAction extends ActionSupport implements ModelDriven<Users>{

    private Users us = new Users();

    @Override
    public String execute() throws Exception {

        System.out.println(us.getUsername());
        System.out.println(us.getPassword());
        return "success";
    }

    public Users getModel() {
        // TODO Auto-generated method stub
        return us;
    }

}

User

public class Users {
    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;
    }


}

struts.xml

添加两个拦截器

<action name="muserlogin" class="com.jikexueyuan.action.MloginAction">
            <result name="success">/index.jsp</result>
            <interceptor-ref name="modelDriven"></interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>

struts2标签实现模型驱动

jsp

写法略有不同

<s:form namespace="/userlogin" action="suserlogin" method="post">
            <s:textfield name = "us.username"/>用户名:
            <s:password name = "us.password"/>密码:
            <s:submit value="提交" />
        </s:form>

action

public class SloginAction extends ActionSupport {
    private Users us;//不需要实例化
    @Override
    public String execute() throws Exception {
        System.out.println("标签实现");
        System.out.println(us.getUsername());
        System.out.println(us.getPassword());
        return "success";
    }
    public Users getUs() {
        return us;
    }
    public void setUs(Users us) {
        this.us = us;
    }


}

struts.xml

<action name="suserlogin" class="com.jikexueyuan.action.SloginAction">
            <result name="success">/index.jsp</result>
        </action>

你可能感兴趣的:(struts,数据,struts2.0)