JavaWeb学习:Struts2的数据封装

获取页面表单数据的方式:根本是通过request对象的getParameterMap();

Map<String, String[]> map=request.getParameterMap();

但是如果能把这些数据封装到对象中,我们使用起来是不是方便些。

Struts2中Params拦截器提供了表单数据封装的几种方式

  • 属性驱动:提供属性set方法(不常用)
  • 属性驱动:页面中提供表达式
    • 可以封装多个对象的数据
  • 模型驱动:采用模型驱动方式(最常用)
    • 只能封装一个对象的数据

一、属性驱动(属性set方法)

  ①、index.jsp

<form action="${pageContext.request.contextPath}/user1.action" method="post">
    UserName:<input type="text" name="username" /><br />
    password:<input type="password" name="password" /><br />
    age:<input type="text" name="age" /><br />
    birthday:<input type="text" name="birthday" /><br />
    salary:<input type="text" name="salary" /><br />
    <input type="submit" name="submit" />
</form>

  ②、User

public class User {
    private String username;
    private String password;
    private Integer age;
    private Date birthday;
    private Double salary;
    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 Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Double getSalary() {
        return salary;
    }
    public void setSalary(Double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
    return "User [username=" + username + ", password=" + password + ", age=" + age + ", birthday=" + birthday
        + ", salary=" + salary + "]";
    }
}

  ③、Action

public class User1Action extends ActionSupport {
    // 1、提供对应的属性
    private String username;
    private String password;
    private Integer age;
    private Date birthday;
    private Double salary;
    
    // 2、提供属性对应的set方法
    public void setUsername(String username) {
    this.username = username;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public void setAge(Integer age) {
    this.age = age;
    }

    public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }

    public void setSalary(Double salary) {
    this.salary = salary;
    }

    @Override
    public String execute() throws Exception {
    // 3、接收数据:
    System.out.println(username);
    System.out.println(password);
    System.out.println(age);
    System.out.println(birthday);
    System.out.println(salary);
    return NONE;
    }
}

  ④、struts_user.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="strutsdemo2" extends="struts-default" namespace="/">
        <action name="user1" class="com.xxx.web.action.user.User1Action"></action>
    </package>
</struts>

  ⑤、struts.xml

<include file="com/xxx/web/action/user/struts_user.xml"></include>

二、属性驱动(页面提供表达式)

  ①、index.jsp

<form action="${pageContext.request.contextPath}/user2.action" method="post">
    UserName:<input type="text" name="user.username" /><br />
    password:<input type="password" name="user.password" /><br />
    age:<input type="text" name="user.age" /><br />
    birthday:<input type="text" name="user.birthday" /><br />
    salary:<input type="text" name="user.salary" /><br />
    <input type="submit" name="user.submit" />
</form>

  ②、Action

public class User2Action extends ActionSupport {
    // 1、私有属性对象变量
    // 2、私有属性对象变量提供get、set方法
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    @Override
    public String execute() throws Exception {
        System.out.println(user);
        return NONE;
    }
}

  ④、struts_user.xml

<action name="user2" class="com.xxx.web.action.user.User2Action"></action>

三、模型驱动

  ①、index.jsp

<form action="${pageContext.request.contextPath}/user2.action" method="post">
    UserName:<input type="text" name="username" /><br />
    password:<input type="password" name="password" /><br />
    age:<input type="text" name="age" /><br />
    birthday:<input type="text" name="birthday" /><br />
    salary:<input type="text" name="salary" /><br />
    <input type="submit" name="submit" />
</form>

  ②、Action

public class User3Action extends ActionSupport implements ModelDriven<User>{
    // 1、implements ModelDriven<User>
    // 2、提供实例化对象
    // 3、重写getModel方法
    private User user=new User();

    @Override
    public User getModel() {
        return user;
    }
    
    @Override
    public String execute() throws Exception {
        System.out.println(user);
        return NONE;
    }
}

  ③、struts_user.xml

<action name="user3" class="com.xxx.web.action.user.User3Action"></action>

四、复杂数据结构(List) -- 以下的数据结构需要使用属性驱动(页面提供表达式)

public class ViewModel {
    private List<Product> products;
    
    public List<Product> getProducts() {
        return products;
    }
    public void setProducts(List<Product> products) {
        this.products = products;
    }
}

  ①、index.jsp(products和ViewModel中的products一致

<form action="${pageContext.request.contextPath}/product1.action" method="post">
    ProductName:<input type="text" name="model.products[0].name" /><br />
    ProductPrice:<input type="text" name="model.products[0].price" /><br />
    ProductName:<input type="text" name="model.products[1].name" /><br />
    ProductPrice:<input type="text" name="model.products[1].price" /><br />
    ProductName:<input type="text" name="model.products[2].name" /><br />
    ProductPrice:<input type="text" name="model.products[2].price" /><br />
    <input type="submit" name="submit" />
</form>

  ②、Action (Action 的model 和index.jsp中model对应的

public class Product1Action extends ActionSupport {
    
    private ViewModel model;

    public ViewModel getModel() {
        return model;
    }

    public void setModel(ViewModel model) {
        this.model = model;
    }

    @Override
    public String execute() throws Exception {
    for (Product product :  model.getProducts()) {
        System.out.println(product);
    }
    return NONE;
    }
}

  ③、struts_product.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="strutsdemo3" extends="struts-default" namespace="/">
        <action name="product1" class="com.xxx.web.action.product.Product1Action"></action>
    </package>
</struts>

  ④、struts.xml

<include file="com/xxx/web/action/product/struts_product.xml"></include>

五、复杂数据结构(Map)  

public class ViewModel {
    private Map<String,Product> map;

    public Map<String, Product> getMap() {
        return map;
    }

    public void setMap(Map<String, Product> map) {
        this.map = map;
    }
}

  ①、index.jsp(map和ViewModel中的map一致

<form action="${pageContext.request.contextPath}/product2.action" method="post">
    ProductName:<input type="text" name="model.map['0'].name" /><br />
    ProductPrice:<input type="text" name="model.map['0'].price" /><br />
    ProductName:<input type="text" name="model.map['1'].name" /><br />
    ProductPrice:<input type="text" name="model.map['1'].price" /><br />
    ProductName:<input type="text" name="model.map['2'].name" /><br />
    ProductPrice:<input type="text" name="model.map['2'].price" /><br />
    <input type="submit" name="submit" />
</form>

  ②、Action

public class Product2Action extends ActionSupport {
    
    private ViewModel model;

    public ViewModel getModel() {
        return model;
    }

    public void setModel(ViewModel model) {
        this.model = model;
    }

    @Override
    public String execute() throws Exception {
    for (String key :  model.getMap().keySet()) {
        System.out.println(key+"    "+model.getMap().get(key));
    }
    return NONE;
    }
}

 

posted @ 2020-11-26 16:46  一杯水M  阅读(127)  评论(0编辑  收藏  举报