Struts2学习第五课 通过和ServletAPI耦合的方式获取WEB资源

与Servlet耦合的访问方式

直接访问Servlet API将使Action与环境Servlet环境耦合在一起,测试时需要有Servlet容器,不便对Action的单元测试。

直接获取HttpServletRequest对象:

servletActionContext.getRequest()

获取HttpSession:ServletActionContext.getRequest().getSession()

直接获取ServletContext对象:

servletActionContext.getServletContext()

通过实现ServletRequestAware,ServletContextAware等接口的方式。

看代码:

package logan.struts2.study;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

public class TestServletActionContextAction {
    
    public String execute(){
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        ServletContext servletContext = ServletActionContext.getServletContext();
        
        System.out.println("execute...");
        
        return "success";
    }

}
<?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>
    <!-- action  VS  Action类
    action:代表一个Struts2的一个请求
    Action类:能够处理Struts2请求的类
     -->
    <package name="default" namespace="/" extends="struts-default">
    
        <action name="TestActionContext" class="logan.struts2.study.TestActionContext">
            <result>/test-actionContext.jsp</result>
        </action>
        
        <action name="TestAware" class="logan.struts2.study.TestAwareAction">
            <result>/test-aware.jsp</result>
        </action>
        
        <action name="TestServletActionContextAction" class="logan.struts2.study.TestServletActionContextAction">
            <result>/success.jsp</result>
        </action>
    
    </package>
    
</struts>

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

index.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <a href="TestActionContext.action?name=logan&name=logan2">Test ActionContext</a>
    <br><br>
    <a href="TestAware.action?name=logan">Test Aware</a>
    <br><br>
    <a href="TestServletActionContextAction">testServletActionContextAction</a>
    <%
        if(application.getAttribute("date") == null){
            application.setAttribute("date", new Date());
            
        }
    %>
    
</body>
</html>

访问网页:http://localhost:8080/Struts2-3/index.jsp

下面看第二种方式:

看代码:

package logan.struts2.study;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

public class TestServletAwareAction implements ServletRequestAware,
ServletContextAware,ServletResponseAware{

    @Override
    public void setServletResponse(HttpServletResponse response) {
        // TODO Auto-generated method stub
        System.out.println(response);
        
    }

    @Override
    public void setServletContext(ServletContext context) {
        // TODO Auto-generated method stub
        System.out.println(context);
        
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        System.out.println(request);
        
    }
    
    public String execute(){
        return "success";
    }

}

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>
    <!-- action  VS  Action类
    action:代表一个Struts2的一个请求
    Action类:能够处理Struts2请求的类
     -->
    <package name="default" namespace="/" extends="struts-default">
    
        <action name="TestActionContext" class="logan.struts2.study.TestActionContext">
            <result>/test-actionContext.jsp</result>
        </action>
        
        <action name="TestAware" class="logan.struts2.study.TestAwareAction">
            <result>/test-aware.jsp</result>
        </action>
        
        <action name="TestServletActionContextAction" class="logan.struts2.study.TestServletActionContextAction">
            <result>/success.jsp</result>
        </action>
        
        <action name="TestServletAware" class="logan.struts2.study.TestServletAwareAction">
            <result>/success.jsp</result>
        </action>
    
    </package>
    
</struts>

index.jsp

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <a href="TestActionContext.action?name=logan&name=logan2">Test ActionContext</a>
    <br><br>
    <a href="TestAware.action?name=logan">Test Aware</a>
    <br><br>
    <a href="TestServletActionContextAction">testServletActionContextAction</a>
    <br><br>
    <a href="TestServletAware">estServletAware</a>
    <%
        if(application.getAttribute("date") == null){
            application.setAttribute("date", new Date());
            
        }
    %>
    
</body>
</html>

运行输出结果:

org.apache.struts2.dispatcher.StrutsRequestWrapper@68614863
org.apache.catalina.connector.ResponseFacade@1f70cda2
org.apache.catalina.core.ApplicationContextFacade@428669b6

通过实现ServletXXXAware接口的方式可以有Struts2注入

需要的Servlet相关的对象

ServletRequestAware:注入HttpServletRequest对象(比较常用)

ServletContextAware:注入ServletContext对象(比较常用)

ServletReponseAware:注入HttpServletResponse对象

posted @ 2017-06-09 16:56  alittlecomputer  阅读(201)  评论(0编辑  收藏  举报