struts中数据处理

数据处理方式

把数据保存到域中

方式1
    直接获取Servlet api
    核心类: ServletActionContext
//方式1: 直接拿到ServletApi, 执行操作
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ServletContext application = ServletActionContext.getServletContext();
// 操作
request.setAttribute("request_data", "request_data1");
session.setAttribute("session_data", "session_data1");
application.setAttribute("application_data", "application_data1");
方式2 
    通过ActionContext获取不同(代表request、session、application)的map
// 【推荐:解耦的方式实现对数据的操作】
// Struts中对数据操作,方式2: 通过ActionContext类 
ActionContext ac = ActionContext.getContext();
// 得到Struts对HttpServletRequest对象进行了封装,封装为一个map
// 拿到表示request对象的map
Map<String,Object> request =  ac.getContextMap(); 
// 拿到表示session对象的map
Map<String, Object> session = ac.getSession();
// 拿到表示servletContext对象的map
Map<String, Object> application = ac.getApplication();
方式3 
    实现接口的方法: RequestAware/SessionAware/ApplicationAware
/** * 数据处理, 方式3: 实现接口的方法 */
public class DataAction extends ActionSupport implements RequestAware, SessionAware, ApplicationAware{

    private Map<String, Object> request;
    private Map<String, Object> session;
    private Map<String, Object> application;

    // struts运行时候,会把代表request的map对象注入
    @Override
    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }

    // 注入session
    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    // 注入application
    @Override
    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }


    @Override
    public String execute() throws Exception {

        // 数据
        request.put("request_data", "request_data1_actionAware");
        session.put("session_data", "session_data1_actionAware");
        application.put("application_data", "application_data1_actionAware");
// 
        return SUCCESS;
    }

}

请求数据自动封装

方式1

jsp表单数据填充到action中的属性

register.jsp:

<body>
    <form action="${pageContext.request.contextPath }/user_register.action" method="post">
        用户名:<input type="text" name="name"><br/>
        密码:<input type="text" name="pwd"><br/>
        年龄:<input type="text" name="age"><br/>
        生日:<input type="text" name="birth"><br/>

        <input type="submit" value="注册">
    </form>
  </body>

UserAction:

/** * Struts核心业务: 请求数据自动封装以及类型转换 */
public class UserAction {

    // 封装请求数据
    private String name;  // 必须给set / get可以不用给
    private String pwd;
    private int age;
    private Date birth;
    public void setName(String name) {
        this.name = name;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }

    // 处理注册请求
    public String register() {
        System.out.println(user.getName());
        System.out.println(user.getPwd());
        System.out.println(user.getAge());
        System.out.println(user.getBirth());
        return "success";
    }
}

方式2

jsp表单数据填充到action的对象中的属性

register.jsp:

<body>
    <form action="${pageContext.request.contextPath }/user_register.action" method="post">
        用户名:<input type="text" name="user.name"><br/>
        密码:<input type="text" name="user.pwd"><br/>
        年龄:<input type="text" name="user.age"><br/>
        生日:<input type="text" name="user.birth"><br/>

        <input type="submit" value="注册">
    </form>
  </body>

User、UserAction:

public class User {

    // 封装请求数据
    private String name;  // 必须给set / get可以不用给
    private String pwd;
    private int age;
    private Date birth;
    public void setName(String name) {
        this.name = name;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public String getName() {
        return name;
    }
    public String getPwd() {
        return pwd;
    }
    public int getAge() {
        return age;
    }
    public Date getBirth() {
        return birth;
    }

}
/** * Struts核心业务: 请求数据自动封装以及类型转换 */
public class UserAction {

     // 对象类型,一定给get方法
    private User user; 
    public void setUser(User user) {
        this.user = user;
    }
    public User getUser() {
        return user;
    }

    // 处理注册请求
    public String register() {
        System.out.println(user.getName());
        System.out.println(user.getPwd());
        System.out.println(user.getAge());
        System.out.println(user.getBirth());
        return "success";
    }
}

你可能感兴趣的:(struts)