美文网首页Java Web知识程序员Java 杂谈
Struts2框架获取Servlet API之注入方式详解

Struts2框架获取Servlet API之注入方式详解

作者: 359afe052eac | 来源:发表于2018-04-13 07:28 被阅读31次

    在struts-default.xml的配置文件中

    有一个 servletConfig拦截器

    查看 servletConfig的对应的类的 ServletConfigInterceptor源码

    首先获取action类,接着判断这个action实现了哪个接口.

    例如实现了ServletRequestAware接口,那么Struts2框架就帮你把request对象注入到当前的action中.

    测试代码如下:

    建立一个login.jsp页面. 里面有一个简单的注册姓名和密码的输入框

    name属性分别为username, password

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
    <h2>注入方式获取request对象</h2>
    <h3>注册页面</h3>
        <form action="${ pageContext.request.contextPath }/login.action" method="post">
            姓名:<input type="text" name="username" /><br/>
            密码:<input type="password" name="password" /><br/>
            <input type="submit" value="注册" />
        </form>
    </body>
    </html>
    

    新建一个LoginAction的Java类实现ServletRequestAware接口,重写了setServletRequest方法

    把request对象,传递给了当前的action类

    public class LoginAction extends ActionSupport implements ServletRequestAware {
        private HttpServletRequest request;
        public String login(){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            System.out.println(username+" "+password);
        }
        @Override
        public void setServletRequest(HttpServletRequest request) {
            this.request=request;
        }
    }
    

    在LoginAction类中,先执行的是setServletRequest()方法,是由StrutsPrepareAndExecuteFilter这个Filter过滤器调用setServletRequest方法, 那么LoginAction这个类就得到了request对象.再执行login()方法获取参数.

    • 这也涉及到了一个知识点:

      在一个类中,里面如果有很多的方法,这些方法不是从上到下依次执行的,主要是看谁先被调用. 方法里面的代码是从上到下执行的.

      由于是先加载struts-default.xml配置文件,setServletRequest(HttpServletRequest request)方法先被调用.

      后加载struts.xml配置文件,配置了login()方法,那么就会被调用和执行.

    <struts>
        <package name="demo" extends="struts-default" namespace="/" >
            <action name="login" class="com.thc.action.LoginAction" method="login"/>
        </package>
    </struts>
    
    • Struts2框架配置文件的执行顺序

      1. default.properties 该配置文件声明了struts2框架的常量
      2. Struts-default.xml struts2框架的核心功能都是在该配置文件中配置的。
      3. struts.xml 在src的目录下,代表用户自己配置的配置文件
      4. 最后加载web.xml的配置文件

      注意:后加载的配置文件会覆盖掉之前加载的配置文件(在这些配置文件中可以配置常量)

    注入的方式执行的流程:

    login.jsp中填写了姓名和密码的参数,当点击注册按钮,会向服务器发送请求,StrutsPrepareAndExecuteFilter就会进行拦截,会加载一开始提到的struts-default.xml中的interceptor拦截器.其中就有servletConfig

    在ServletConfigInterceptor类中,其中一段代码如下

    public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics {
    
        private static final long serialVersionUID = 605261777858676638L; 
    
        public String intercept(ActionInvocation invocation) throws Exception {
         final Object action = invocation.getAction();
         final ActionContext context = invocation.getInvocationContext();
    
         if (action instanceof ServletRequestAware) {
             HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
             ((ServletRequestAware) action).setServletRequest(request);
         }
     }     
    

    其中:

    • final Object action = invocation.getAction(); 是获取当前的action类
    • if (action instanceof ServletRequestAware) 是通过多态的方式判断action继承了哪个类
    • HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);框架底层获取了request 对象
    • ((ServletRequestAware) action).setServletRequest(request); 执行了setServletRequest(request)方法.
    • LoginAction重写了setServletRequest(request)方法,通过this.request=request; 那么就拿到了request对象.

    总结:

    • 当servletConfig对应的类执行时,会得到当前访问的Action对象
    • 在通过判断是否实现指定的接口来将需要的servlet api对象注入到action类当中

    相关文章

      网友评论

        本文标题:Struts2框架获取Servlet API之注入方式详解

        本文链接:https://www.haomeiwen.com/subject/iamqkftx.html