一.Intercept是Struts2的核心,所有用户请求都经过Intercept处理并经过Intercept处理返回数据。另外,介绍一下Struts2如何获得Servlet域对象。
二.API测试代码
直接上代码,具体解释见代码注释
ServletAPI.java
@SuppressWarnings("all")
public class ServletAPI extends ActionSupport implements ServletRequestAware{
//演示如何获得域,无测试
public String execute() throws Exception {
/*如何在action中获得原生ServletAPI,使用ActionContext*/
// request域=> map (struts2并不推荐使用原生request域)
// 不推荐,因为ActionContext与request生命周期相同
Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");
// 推荐,request域=> map
ActionContext.getContext().put("name", "requestTom");
// 推荐,session域 => map
Map<String, Object> sessionScope = ActionContext.getContext().getSession();
sessionScope.put("name", "sessionTom");
// 推荐,application域=>map
Map<String, Object> applicationScope = ActionContext.getContext().getApplication();
applicationScope.put("name", "applicationTom");
/* 如何在action中获得原生ServletAPI,使用ServletActionContext */
// 不推荐,原生request
HttpServletRequest request = ServletActionContext.getRequest();
// 不推荐,原生session
HttpSession session = request.getSession();
// 不推荐,原生Cookie
Cookie[] cookies = request.getCookies();
// 不推荐,原生response
HttpServletResponse response = ServletActionContext.getResponse();
// 不推荐,原生servletContext
ServletContext servletContext = ServletActionContext.getServletContext();
/*Struts2已经对Servlet原生的域进行了封装
*所以Struts2可以取到Servlet原生的域的值
*Struts2设计的理念就是与servlet(线程不安全)完全解耦
*最好使用推荐的方式设置属性
*/
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
}
}
三.Intercept测试代码
直接上代码,具体解释见代码注释
InterceptActionStruts.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="Intercept" namespace="/Intercept" extends="struts-default">
<!-- 1.注册拦截器 -->
<interceptors>
<interceptor name="myinterceptor" class="cjx.intercept.Intercept3"></interceptor>
<!-- 2.注册拦截器栈 -->
<interceptor-stack name="mystack">
<!-- 引用自定义拦截器(建议放在默认拦截器之前) -->
<interceptor-ref name="myinterceptor">
<!-- 指定不拦截的方法 -->
<param name="excludeMethods">add,delete</param>
<!-- 指定拦截的方法 :只能同时设置一个
<param name="excludeMethods">add,delete</param>
-->
</interceptor-ref>
<!-- 引用默认拦截器栈(20个) -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 3.指定包中默认拦截器栈 -->
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<!-- 自定义全局结果-->
<global-results >
<result name="error" type="redirect">/index.jsp</result>
</global-results>
<!-- 如果出现 java.lang.RuntimeException异常,跳转到error的结果-->
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<action name="Intercept*" class="cjx.intercept.DemoAction" method="{1}">
<!-- 为action单独指定走哪个拦截器(栈)
<default-interceptor-ref name="mystack"></default-interceptor-ref>
-->
<result name="success" >/hello.jsp</result>
</action>
</package>
</struts>
Intercept1.java
public class Intercept1 implements Interceptor{
//自定义拦截器方式一:实现 Interceptor接口
/*拦截器生命周期
* 随着项目启动创建,随着项目关闭销毁
*/
@Override
public void init() {//初始化方法
}
@Override
public void destroy() {//销毁方法
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
//拦截方法
return null;
}
}
Intercept2.java
public class Intercept2 extends AbstractInterceptor{
//自定义拦截器方式二:继承AbstractInterceptor
/*实际上AbstractInterceptor实现 Interceptor接口
* 没有init和destroy。如果不需要就用AbstractInterceptor
*/
@Override
public String intercept(ActionInvocation invocation) throws Exception {
return null;
}
}
Intercept3.java
public class Intercept3 extends MethodFilterInterceptor{
//自定义拦截器方式三:继承MethodFilterInterceptor
/*方法过滤拦截器
* 定制拦截器拦截方法,定制哪些方法需要拦截,哪些方法不需要拦截
*
*/
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//前处理
System.out.println("前处理MethodFilterInterceptor");
//放行
//如果不放行,直接在返回值里返回一个字符串,跳转到结果页面
//如果不放行,直接在返回值里返回invocation.invoke()
invocation.invoke();//进行下一个拦截器
//后处理
System.out.println("后处理MethodFilterInterceptor");
return null;
}
}
DemoAction.java
public class DemoAction extends ActionSupport {
public String add() throws Exception {
System.out.println("add");
return SUCCESS;
}
public String delete() throws Exception {
System.out.println("delete");
return SUCCESS;
}
public String update() throws Exception {
System.out.println("update");
return SUCCESS;
}
public String find() throws Exception {
System.out.println("find");
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>
<!-- 引入其他配置文件 -->
<include file="cjx/intercept/InterceptActionStruts.xml"></include>
</struts>