① 、和Servlet API 耦合的方式:可以访问更多的Servlet API对象 ,且可以调用其原生的方法。
使用 ServletActionContext 类,
实现 ServletXxxAware 接口。
Struts.xml
<struts>
<package name="default" namespace="/" extends = "struts-default">
<action name="TestActionContext" class="com.atguigu.struts2.action.TestActionContextAction">
<result>/test-actionContext.jsp</result>
</action>
<action name="TestAware" class="com.atguigu.struts2.action.TestAwareAction">
<result>/test-aware.jsp</result>
</action>
<action name="TestServletActionContext" class="com.atguigu.struts2.action.TestServletActionContextAction">
<result>/success.jsp</result>
</action>
<action name="TestServletAware" class="com.atguigu.struts2.action.TestServletAwareAction">
<result>/success.jsp</result>
</action>
</package>
</struts>
TestServletActionContextAction.java
package com.atguigu.struts2.action;
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(){
/**
* ServletActionContext:可以获取到当前Action对象需要的一切Servlet API
* 相关的对象
* 常用的方法:
* 1.获取HttpServletRequest :ServletActionContext.getRequest();
* 2.获取HttpSession : ServletActionContext.getRequest().getSession();
* 3.获取ServletContext(这就是application) : ServletActionContext.getServletContext();
*/
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
request.getContextPath();
//这个就是application对象
ServletContext servletContext = ServletActionContext.getServletContext();
servletContext.setAttribute("servletContext", "application");
System.out.println("execute");
return "success";
//contextpath 环境上下文路径:request.getContextPath();
//servletpath 小程序路径:request.getServletPath();
//ServletContext 小程序上下文 即为application对象 :
//ServletActionContext.getServletContext();
}
}
TestServletAwareAction.java
package com.atguigu.struts2.action;
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;
/**
*通过实现ServletXxxAware接口的方式 可以由Struts2 注入需要的
*Servlet 相关的对象
*
* ServletRequestAware :注入HttpServletRequest对象(比较常用)
* ServletContextAware : 注入ServletContext对象(比较常用)
* ServletResponseAware : 注入HttpServletResponse对象
*
*/
public class TestServletAwareAction implements ServletRequestAware ,ServletContextAware
,ServletResponseAware{
public void setServletRequest(HttpServletRequest arg0) {
System.out.println(arg0);
}
private ServletContext context ;
public void setServletContext(ServletContext context) {
System.out.println(context);
this.context = context;
}
public void setServletResponse(HttpServletResponse response) {
System.out.println(response);
}
public String execute(){
return "success";
}
}
Index.jsp
<body>
<a href ="TestActionContext.action?name=atguigu">Test ActionContext</a>
<br/>
<br/>
<a href ="TestAware.action?name=atguigu">Test Aware</a>
<br/>
<br/>
<a href="TestServletActionContext">TestServletActionContext</a>
<br/>
<br/>
<a href="TestServletAware.action">TestServletAware</a>
<%
if(application.getAttribute("date") == null)
application.setAttribute("date",new Date());
request.setAttribute("req","reqvalue");
%>
</body>