1、拦截器获取 Web 资源模式
2、静态对象获取 Web 资源模式
1、拦截器获取 Web 资源模式:
(1)使用Struts2 Aware拦截器
index.jsp表单:
<body> <form action="<%=path %>/firstAction" method="post"> 姓名:<input type="text" name="name"><br> 密码:<input type="password" name="pwd"><br> <input type="submit" value="登录"> </form>
</body>
FirstAction.java:
package com.qyy.action; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; 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; /* 要实现拦截器接口,及其中的抽象方法 * 三个拦截器分别是 request,response,application * 重写它们的抽象方法将private属性初始化 * * 重写execute方法 */ public class FirstAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{ private ServletRequest request; private ServletResponse response; private ServletContext con; public String execute() { String username = request.getParameter("name"); System.out.println(username); return "success"; } @Override public void setServletRequest(HttpServletRequest arg0) { request = arg0; } @Override public void setServletResponse(HttpServletResponse arg0) { response = arg0; } @Override public void setServletContext(ServletContext arg0) { con = arg0; } }
struts.xml配置:
<struts> <package name="test" extends="struts-default"> <action name="firstAction" class="com.qyy.action.FirstAction"> <result name="success">/index.jsp</result> </action> </package> </struts>
(2)使用Struts2 RequestAware拦截器
index.jsp表单填写:
<body> <form action="<%=path %>/secondAction" method="post"> 姓名:<input type="text" name="name"><br> 密码:<input type="password" name="pwd"><br> <input type="submit" value="登录"> </form> </body>
SecondAction.java:
package com.qyy.action; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.apache.struts2.StrutsStatics; import org.apache.struts2.interceptor.RequestAware; /* * 使用Struts2 RequestAware拦截器 * 实现RequestAware接口 * * 这样就完成了程序在走到这个类之前先经过拦截器, * 拦截器负责调用这个类的setRequest()方法将所以Web资源的对象通过map集合获取。 */ public class SecondAction implements RequestAware{ private ServletRequest request; private ServletResponse response; private ServletContext con; public String execute() { String username = request.getParameter("name"); System.out.println("第二个action "+username); return "success"; } @Override public void setRequest(Map<String, Object> arg0) { request = (ServletRequest) arg0.get(StrutsStatics.HTTP_REQUEST); response = (ServletResponse) arg0.get(StrutsStatics.HTTP_RESPONSE); con = (ServletContext) arg0.get(StrutsStatics.SERVLET_CONTEXT); } }
Struts.xml配置:
<action name="secondAction" class="com.qyy.action.SecondAction"> <result name="success">/index.jsp</result> </action>
2、静态对象获取 Web 资源模式
(1)使用Struts2内置静态对象ActionContext
ThirdAction.java:(比较简便的方法,不用继承或实现任何接口)
package com.qyy.action; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; /* * 比较简便的方法,不用继承或实现任何接口 */ public class ThirdAction { public String execute() { ActionContext ac = ActionContext.getContext(); /*然后就可以使用ac来获得资源 */ ServletRequest request = (ServletRequest) ac.get(ServletActionContext.HTTP_REQUEST); ServletResponse response = (ServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE); ServletContext con = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT); String username = request.getParameter("name"); System.out.println("第三种 "+username); return "success"; } }
(2)使用Struts2内置静态对象ServletActionContext
***推荐使用的一种方式:最方便最简便的方法
package com.qyy.action; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.apache.struts2.ServletActionContext; public class FourthAction { public String execute() { ServletRequest request = ServletActionContext.getRequest(); ServletResponse response = ServletActionContext.getResponse(); ServletContext con = ServletActionContext.getServletContext(); String username = request.getParameter("name"); System.out.println("第四种 "+username); return "success"; } }