本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

Struts2(5)之请求数据的封装

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


准备

为后面测试示例编写代码及配置如下:

// com.zze.bean.User
package com.zze.bean;

import java.util.Date;

public class User {
    private String name;
    private Integer age;
    private Date birthday;

    public String getName() {
        return name;
    }

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

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}
<!-- struts.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.devMode" value="true"/>
    <package name="test" extends="struts-default" namespace="/">
        <action name="*" class="com.zze.action.{1}Action">
            <result>/show.jsp</result>
        </action>
    </package>
</struts>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>v1:${requestScope.key}
v2:${sessionScope.key}
v3:${applicationScope.key}

请求数据的封装

Struts2 是一个 Web 层框架,它的提供了如下两种对请求数据封装的方式。

属性驱动

属性驱动这种方式其实就是要给 Action 中接收参数的成员变量提供 set 方法。

封装到基础类型变量

// com.zze.action.Test4Action
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;

import java.util.Date;

public class Test4Action extends ActionSupport {
    private String name;
    private Integer age;
    private Date birthday;

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

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

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

    @Override
    public String execute() throws Exception {
        System.out.println(name);
        System.out.println(age);
        System.out.println(birthday);
        return NONE;
    }
}

请求:

image.png

输出:

image.png

封装到JavaBean

// com.zze.action.Test5Action 
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zze.bean.User;

/**
 * 请求参数的参数名需按指定格式
 */
public class Test5Action extends ActionSupport {
    // 给接收参数的对象提供 get 方法
    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;
    }
}

请求:

image.png

输出:

image.png

封装到 List

// com.zze.action.Test6Action
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;

import java.util.Arrays;
import java.util.List;

/**
 * 请求参数的参数名需按指定格式
 */
public class Test6Action extends ActionSupport {
    public List<String> names;

    public List<String> getNames() {
        return names;
    }

    public void setNames(List<String> names) {
        this.names = names;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(Arrays.toString(names.toArray()));
        return NONE;
    }
}

请求:

image.png

输出:

image.png

封装到 List

// com.zze.action.Test7Action
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zze.bean.User;

import java.util.List;

public class Test7Action extends ActionSupport {
    private List<User> users;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

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

请求:

image.png
输出:

image.png

封装到 Map<String,String[]>

// com.zze.action.Test8Action
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;

import java.util.Arrays;
import java.util.Map;

public class Test8Action extends ActionSupport {
    private Map<String,Object> map;

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

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

    @Override
    public String execute() throws Exception {
        for (String key : map.keySet()) {
            String valStr=  Arrays.toString((String[])map.get(key));
            System.out.println(String.format("%s:%s",key,valStr));
        }
        return NONE;
    }
}

请求:

image.png

输出:

image.png

封装到 Map<String,POJO>

// com.zze.action.Test9Action
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;
import com.zze.bean.User;

import java.util.Map;

public class Test9Action extends ActionSupport {
    private Map<String, User> map;

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

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

    @Override
    public String execute() throws Exception {
        for (String key : map.keySet()) {
            System.out.println(String.format("%s : %s", key, map.get(key)));
        }
        return NONE;
    }
}

请求:
image.png

输出:
image.png

模型驱动

使用模型驱动需要让 Action 类实现 com.opensymphony.xwork2.ModelDriven 接口。

// com.zze.action.Test10Action
package com.zze.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zze.bean.User;

/**
 * 实现 ModelDriven 接口
 */
public class Test10Action extends ActionSupport implements ModelDriven<User> {
    private User user = new User();

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

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

请求:
image.png
输出:

image.png

# Struts2  

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×