02-资源共享及操作对象

2018-06-25  本文已影响4人  何惧l

struts2中采用容器实现资源共享,并并容器对象进行封装,降低与servletAPI的耦合性
操作方式:
- ActionContext
- ServletActionContext
- 实现接口

ActionContext

ActionContext属于Struts提供类,通过该类可以操作request,session,application容器,但是无法获得相关对象.Struts2对request,session,application容器进行了封装,因此可以通过ActionContext操作容器
特点: 可以实现对容器的操作,无法获得相关容器对象.降低了与Servlet的耦合度,操作Struts更加方便.

  1. 在web.xml中进行注册
    <!-- 注册struts框架 -->
  <filter>
    <filter-name>mystruts</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>mystruts</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  1. action中,使用了ActionContext
package action;

import com.opensymphony.xwork2.ActionContext;

public class TestAction1 {
    public String byActionContext() {
        System.out.println("TestAction1--byActionContext()!");
        //操作容器: request,session,application
        // 获取操作的对象,存放数据
        ActionContext context = ActionContext.getContext();
        
        context.put("req", "byActionContext--request容器");
        context.getSession().put("sess", "byActionContext--session容器");
        context.getApplication().put("app","byActionContext--application容器");
        return "success";
    }
}
  1. 在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>
    <package name="context" namespace="/" extends="struts-default">
        <!-- byActionContext -->
        <action name="byActionContext" class="action.TestAction1" method="byActionContext">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>
  1. 在test.html中访问资源
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    测试获得容器对象和操作容器 <br>

    <a href="byActionContext">byActionContext </a>

</body>
</html>

  1. 访问完成后的index.jsp页面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    获得容器中的数据. <br>
    request容器: ${requestScope.req }<br>
    session容器: ${sessionScope.sess }<br>
    application容器: ${applicationScope.app }
</body>
</html>

ServletActionContext

手动获得相关对象
只需要在action和struts.xml中进行改变就可以了

  1. 在action中
package action;
public class TestAction2 {
    public String byServletActionContext() {
        System.out.println("TestAction2--byServletActionContext()!");
        //获得request,session,application对象
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpServletResponse respose=ServletActionContext.getResponse();
        HttpSession session=request.getSession();
        ServletContext application=ServletActionContext.getServletContext();
        
        //向容器中放入数据
        request.setAttribute("req", "byServletActionContext--request容器");
        session.setAttribute("sess","byServletActionContext--session容器");
        application.setAttribute("app", "byServletActionContext--application容器");
        
        return "success";
    }
}

  1. 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>
    <package name="context" namespace="/" extends="struts-default"
        <!-- byServletActionContext -->
        <action name="byServletActionContext" class="action.TestAction2" method="byServletActionContext">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>

  1. test.html
  <!-- ServletActionContext: 手动获得  原始对象  response -->
  <a href="byServletActionContext">byServletActionContext</a>

实现接口

通过实现这几个接口重写这几个方法就可以了
自动获取相关对象
只需要在action和struts.xml中进行改变就可以了

  1. 在action中,实现这几个接口就可以了
package action;

public class TestAction3 implements ServletRequestAware,ServletResponseAware,ServletContextAware{
    //当执行当前Action时,Struts2框架自动调用接口方法并传入相关参数
    private HttpServletRequest request;
    private HttpServletResponse response;
    private ServletContext application;
    private HttpSession session;
    
    public void setServletContext(ServletContext application) {
        this.application=application;
    }
    public void setServletResponse(HttpServletResponse response) {
        this.response=response;
    }
    public void setServletRequest(HttpServletRequest request) {
        this.request=request;
        this.session=request.getSession();
    }
    
    public String byInterface() {
        System.out.println("TestAction3--byInterface()!");
        
        //向容器中存入数据
        request.setAttribute("req", "byInterface--request容器");
        session.setAttribute("sess","byInterface--session容器");
        application.setAttribute("app", "byInterface--application容器");
        
        return "success";
    }
}


  1. 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>
    <package name="context" namespace="/" extends="struts-default">
        <!-- byInterface -->
        <action name="byInterface" class="action.TestAction3" method="byInterface">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>

  1. test.html
<!-- 接口: 自动获得   原始对象  response
    ServletRequestAware,ServletResponseAware,ServletContextAware
     -->
    <a href="byInterface">byInterface</a>
上一篇 下一篇

猜你喜欢

热点阅读