Struts2--数据封装

1. 数据封装

1.1 属性驱动:提供属性set方法模式

  很少使用

form表单

 <hr>
    <h2>属性驱动:提供属性set方法模式</h2>
    <form action="${pageContext.request.contextPath}/userAction1.action" method="post">
        <input type="password" name="id" value="110"><br>
        <input type="text" name="uname" value="cat"><br>
        <input type="password" name="pwd" value="abc"><br>
        <input type="submit"><br>
    </form>

配置文件

    <package name="demo02" extends="struts-default" namespace="/">
        <action name="userAction1" class="com.action.UserAction" method="m1">
            <result name="users">/jsp/users.jsp </result>
        </action>
    </package>

实体类

package com.bean;

public class User {

    private Integer id;
    private String uname;
    private String pwd;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", uname='" + uname + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}

 

Action类

package com.action;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

public class UserAction extends ActionSupport {
    private Integer id;
    private String uname;
    private String pwd;

    public String m1(){
        //接受数据
        System.out.println(id);
        System.out.println(uname);
        System.out.println(pwd);
        //封装数据
        User user=new User();
        user.setId(id);
        user.setUname(uname);
        user.setPwd(pwd);
        ServletActionContext.getRequest().setAttribute("user",user);
        return "users";
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", uname='" + uname + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}

结果页面

<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    名字:${user.uname}<br>
    密码:${user.pwd}
</body>
</html>

 

1.2 属性驱动:页面提供表达方式

  使用比较方便,一次可以封装多个对象

form表单

<h2> 属性驱动:页面提供表达方式</h2>
    <form action="${pageContext.request.contextPath}/userAction2.action" method="post">
        <input type="password" name="user.id" value="110"><br>
        <input type="text" name="user.uname" value="cat"><br>
        <input type="password" name="user.pwd" value="abc"><br>
        <input type="submit"><br>
    </form>

配置文件

  <package name="demo03" extends="struts-default" namespace="/">
        <action name="userAction2" class="com.action.UserAction2" method="m1">
            <result name="users">/jsp/users.jsp </result>
        </action>
    </package>

Action类

package com.action;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class UserAction2 extends ActionSupport {
    //提供一个User对象和set,get方法
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String m1(){
        System.out.println("UserAction2------");
        //接受数据
        System.out.println(user);
        //封装数据
        ServletActionContext.getRequest().setAttribute("user",user);
        return "users";
    }
}

结果页面

<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    名字:${user.uname}<br>
    密码:${user.pwd}
</body>
</html>

 

1.3 模型驱动:采用模型驱动方式

  使用方便,开发中使用最多,但一次只能封装一个对象

form表单

    <h2> 模型驱动:采用模型驱动方式</h2>
    <form action="${pageContext.request.contextPath}/userAction3.action" method="post">
        <input type="password" name="id" value="110"><br>
        <input type="text" name="uname" value="cat"><br>
        <input type="password" name="pwd" value="abc"><br>
        <input type="submit"><br>
    </form>

配置文件

<package name="demo04" extends="struts-default" namespace="/">
        <action name="userAction3" class="com.action.UserAction3" method="m1">
            <result name="users">/jsp/users.jsp </result>
        </action>
    </package>

Action类

package com.action;

import com.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

public class UserAction3 extends ActionSupport implements ModelDriven<User> {
    //模型驱动必须手动创建一个模型接受参数
    private User user=new User();

    public String m1(){
        System.out.println("UserAction3------");
        //接受数据
        System.out.println(user);
        //封装数据
        ServletActionContext.getRequest().setAttribute("user",user);
        return "users";
    }

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

结果页面

<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    名字:${user.uname}<br>
    密码:${user.pwd}
</body>
</html>

 

上一篇:Struts2 为什么被淘汰?自己作死!


下一篇:什么是MVC模式?