Struts2中数据封装方式

一、通过ActionContext类获取

public class ActionContextDemo extends ActionSupport {

    @Override
    public String execute() throws Exception {
        //获取ActionContext对象
        ActionContext context = ActionContext.getContext();
        //调用getParameters对象获取参数
        Map<String, Object> map = context.getParameters();
        //遍历打印map集合
        for (String key : map.keySet()) {
            String[] val = (String[]) map.get(key);
            System.out.println(key + " : " + Arrays.toString(val));
        }
        return NONE;
    }
}

二、通过ServletActionContext类获取request类然后获取

public class ServletActionContextDemo extends ActionSupport {

    @Override
    public String execute() throws Exception {
        //获取request
        HttpServletRequest request = ServletActionContext.getRequest();
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] hobbies = request.getParameterValues("hobbies");
        System.out.println(username + " : " + password + " : " + Arrays.toString(hobbies));
        
        //操作域对象
        //request域
        HttpServletRequest request2 = ServletActionContext.getRequest();
        request2.setAttribute("request", "hello request");
        
        //获取session域
        HttpSession session = request2.getSession();
        session.setAttribute("session", "hello session");
        
        //获取servletcontext域
        ServletContext servletContext = request2.getServletContext();
        servletContext.setAttribute("servletContext", "application");
        return SUCCESS;
    }
    
}

三、属性封装

定义私有的成员变量,变量名称与表单中name属性值一致

提供成员变量的get和set方法(实际上,在数据封装时,仅提供set方法即可。成员变量的属性名也不一定非得跟name属性值一致,但set方法跟的字段setXXX中的XXX必须跟name属性名的首字符大写一致)
public class DataPackagingAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private String[] hobbies;
    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 String[] getHobbies() {
        return hobbies;
    }
    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }
    
    @Override
    public String execute() throws Exception {
        System.out.println("属性驱动:  " + username + " : " + password + "  " + Arrays.toString(hobbies));
        return NONE;
    }
    
}

四、基于模型驱动的数据封装方法

  1.让action类实现ModelDriven<T>接口

  2.实现ModelDriven<T>接口中的getModel方法

  3.在Action中创建私有的成员变量,并手动创建实体类


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

五、复杂数据的封装方法

1.封装数据到list集合中

  第一步: 在action中声明list成员变量,并手动创建实体类;

  第二部:  提供get和set方法;

  第三部: 在jsp页面中,提供基于list作为值得name属性

public class ListAction extends ActionSupport {
    private List<User> list = new ArrayList<User>();
    public List<User> getList() {
        return list;
    }
    public void setList(List<User> list) {
        this.list = list;
    }  
    @Override
    public String execute() throws Exception {
        for (int i = 0; i < list.size(); i ++) {
            System.out.println(list.get(i));
        }
        return NONE;
    }
}

  在jsp页面中name属性的赋值规则

    <form action="${pageContext.request.contextPath}/list.action" method="post">
        username: <input type="text" name="list[0].username" /><br/>
        password: <input type="password" name="list[0].password" /><br/>
        hobby: <input type="checkbox" name="list[0].hobbies" value="basketball" />basketball
        <input type="checkbox" name="list[0].hobbies" value="football" />football
        <input type="checkbox" name="list[0].hobbies" value="badminton" />badminton
        <hr/>
        
        username: <input type="text" name="list[1].username" /><br/>
        password: <input type="password" name="list[1].password" /><br/>
        hobby: <input type="checkbox" name="list[1].hobbies" value="basketball" />basketball
        <input type="checkbox" name="list[1].hobbies" value="football" />football
        <input type="checkbox" name="list[1].hobbies" value="badminton" />badminton
        <hr/>
        
        <input type="submit" value="提交" />
    </form>

2.封装数据到map集合中

  第一步: 在action中声明map成员变量,并手动创建实体类;

  第二部:  提供get和set方法;

  第三部: 在jsp页面中,提供基于map作为值得name属性

public class MapAction extends ActionSupport {
    private Map<String, User> map = new HashMap<String, User>();
    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(key + "  " + map.get(key));
        }
        return NONE;
    }
}

在jsp页面中name属性的命名规则

<form action="${pageContext.request.contextPath}/map.action" method="post">
        username: <input type="text" name="map['one'].username" /><br/>
        password: <input type="password" name="map['one'].password" /><br/>
        hobby: <input type="checkbox" name="map['one'].hobbies" value="basketball" />basketball
        <input type="checkbox" name="map['one'].hobbies" value="football" />football
        <input type="checkbox" name="map['one'].hobbies" value="badminton" />badminton
        <hr/>
        
        username: <input type="text" name="map['two'].username" /><br/>
        password: <input type="password" name="map['two'].password" /><br/>
        hobby: <input type="checkbox" name="map['two'].hobbies" value="basketball" />basketball
        <input type="checkbox" name="map['two'].hobbies" value="football" />football
        <input type="checkbox" name="map['two'].hobbies" value="badminton" />badminton
        <hr/>
        
        <input type="submit" value="提交" />
    </form>

3.使用属性封装数据到对象中

  第一步: 在action中声明实体类User的成员变量,可以不用实例化

  第二步: 提供实体类的get和set方法

  第三部: jsp中name属性基于实体类赋值
public class UserAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    //声明实体类
    private User user;
    //生成get和set方法
    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;
    }
}
jsp页面中name属性的命名规则

 <form action="${pageContext.request.contextPath}/user.action" method="post">
        username: <input type="text" name="user.username" /><br/>
        password: <input type="password" name="user.password" /><br/>
        hobby: <input type="checkbox" name="user.hobbies" value="basketball" />basketball
            <input type="checkbox" name="user.hobbies" value="football" />football
            <input type="checkbox" name="user.hobbies" value="badminton" />badminton
        <hr/>
        <input type="submit" value="提交" />
    </form>

原文地址:https://www.cnblogs.com/rodge-run/p/6441636.html