• Struts2第四篇【请求数据自动封装、Action得到域对象】


    前言

    前三篇的Struts博文基本把Struts的配置信息讲解完了…..本博文主要讲解Struts对数据的处理

    一般地,我们使用Servlet的时候都是分为几个步骤的:

    1. 得到web层的数据、封装数据
    2. 调用service层的逻辑业务代码
    3. 将数据保存在域对象中,跳转到对应的JSP页面

    现在问题来了,我们自己编写的Action类是没有request、response、Session、application之类的对象的….我们是怎么得到web层的数据、再将数据存到域对象中的呢??

    请求数据封装

    前面已经说过了,Struts预先帮我们完成了对数据封装的功能,它是通过params拦截器来实现数据封装的

                <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

    register.jsp

    首先,我们填写表单页面的数据,请求Action处理数据

    
    <form action="${pageContext.request.contextPath}/date01" method="post">
        用户名:<input type="text" name="username"><br>
        密码:<input type="text" name="psd"><br>
        年龄:<input type="text" name="age"><br>
        生日:<input type="text" name="birthday"><br>
        <input type="submit" value="注册"><br>
    </form>

    Action封装基本信息

    在Action设置与JSP页面相同的属性,并为它们编写setter方法

    
        private String username;
        private String psd;
        private int  age;
        private Date birthday;
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public void setPsd(String psd) {
            this.psd = psd;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }

    我们直接在业务方法中访问这些变量,看是否能得到表单的值。

    这里写图片描述


    Action封装对象

    一般地,我们注册的时候,都是在Servlet上把基本信息封装到对象上…那么在Struts怎么做呢?

    • 创建一个User类,基本的信息和JSP页面是相同的。
    package qwer;
    
    import java.util.Date;
    
    /**
     * Created by ozc on 2017/4/27.
     */
    public class User {
    
        private String username;
        private String psd;
        private int  age;
        private Date birthday;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPsd() {
            return psd;
        }
    
        public void setPsd(String psd) {
            this.psd = psd;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    }
    
    • 在Action中定义User对象出来,并给出setter和getter方法….值得注意的是:基本信息只要setter就够了,封装到对象的话,需要setter和getter
    public class ccAction extends ActionSupport {
    
        private User user;
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        public String register() {
    
            System.out.println(user.getUsername());
            System.out.println(user.getPsd());
            System.out.println(user.getAge());
            System.out.println(user.getBirthday());
    
            return "success";
        }
    
    
    }
    
    
    • 在JSP页面,提交的name要写成user.username之类的
    <form action="${pageContext.request.contextPath}/register" method="post">
        用户名:<input type="text" name="user.username"><br>
        密码:<input type="text" name="user.psd"><br>
        年龄:<input type="text" name="user.age"><br>
        生日:<input type="text" name="user.birthday"><br>
        <input type="submit" value="注册"><br>
    </form>
    

    这里写图片描述

    得到域对象

    Struts怎么把数据保存在域对象中呢???Struts提供了三种方式

    一、得到Servlet API

    我们可以通过ServletActionContext得到Servlet API

    由于每个用户拥有一个Action对象,那么底层为了维护用户拿到的是当前线程的request等对象,使用ThreadLocal来维护当前线程下的request、response等对象…

    
    
            //通过ServletActionContext得到Servlet API
            javax.servlet.ServletContext context = ServletActionContext.getServletContext();
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpSession session = request.getSession();
            HttpServletResponse response = ServletActionContext.getResponse();

    二、ActionContext类

    我们还可以通过ActionContext类来得到request、response、session、application被Struts封装的Map集合

            //得到ActionContext 对象
            ActionContext context = ActionContext.getContext();
            Map<String, Object> session = context.getSession();
            Map<String, Object> application = context.getApplication();
    
            //这是request的Map
            Map<String, Object> request = context.getContextMap();

    三、实现接口

    当web容器发现该Action实现了Aware接口,会把相对应的资源通过Aware接口注射进去,实际上就是一种IOC。

    Aware实际就是一种拦截器,拦截代码在执行Action之前执行、将资源注射到Action中

    实现SessionAware, RequestAware, ApplicationAware接口,它就要在程序中实现三个方法:

    
    
        private Map<String, Object> request;
        private Map<String, Object> session;
        private Map<String, Object> application;
    
    
        @Override
        public void setApplication(Map<String, Object> map) {
            this.application = map;
        }
    
        @Override
        public void setRequest(Map<String, Object> map) {
    
            this.request = map;
        }
    
        @Override
        public void setSession(Map<String, Object> map) {
            this.session = map;
        }
    

    通过这些方法,我们就可以得到对应的Map对象…..

    小总结

    那么,我们有三种方法可以得到Servlet对应的对象,那么该使用哪一种呢???

    分析:

    • 第一种方法:需要导入Servlet的包,与Struts耦合了
    • 第二种方法:只能在业务方法中使用ActionContext类得到对应的Map对象,如果有多个方法,那么每个方法都需要写类似的代码
    • 第三种方法:可以在类上定义成员变量,以至于整个类都能使用。但是需要实现类、实现对应的方法

    如果我们需要使用到对象的其他方法,类似getContextPath()之类的,那么只能使用第一种

    如果我们就按照平常的开发,我们就使用第二种【获取简单,没有耦合】

    至于第三种,当我们将来可能开发BaseAction的时候,就使用它!


  • 相关阅读:
    什么是Flex 布局
    wx.navigateTo、wx.redirectTo和wx.switchTab三种导航方式的区别
    Ajax 工作原理 及 实例
    NodeJS之 Express框架 app.use(express.static)
    Parcel 入门 (一)
    打包工具的介绍
    CSS网页布局
    《拖延心理学》阅读要点
    PHP实现页面静态化
    PHP中的魔术方法
  • 原文地址:https://www.cnblogs.com/zhong-fucheng/p/7202989.html
Copyright © 2020-2023  润新知