〖 作者:feng_sundy 〗〖 大小:1k 〗〖 发布日期:2007-10-12 〗〖 浏览:3 〗 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="checkbox"> <param name="uncheckedValue">0</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/>(这句是设置所有Action自动调用的拦截器堆栈) </package> struts-action.xml里面配置Action如下: <package name="LogonAdmin" extends="struts-shop">(这里扩展struts.xml里面定义的配置就可以了) <action name="logon" class="logonAction"> <result>/jsp/smeishop/admin/index.jsp</result> <result name="error">/jsp/smeishop/admin/logon.jsp</result> <result name="input">/jsp/smeishop/admin/logon.jsp</result> </action> <action name="logout" class="logoutAction"> <result>/jsp/smeishop/admin/logon.jsp</result> </action> </package> 你的拦截器可以正常工作了!!HOHO 以下是参考资料 struts2自带的配置及其拦截器配置
每一个Action既可以将操作转交给下面的拦截器,Action也可以直接退出操作返回客户既定的画面。
如何自定义一个拦截器? 自定义一个拦截器需要三步: 1 自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。 2 在strutx.xml中注册上一步中定义的拦截器。 3 在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明
Interceptor接口声明了三个方法:
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception; }
Init方法在拦截器类被创建之后,在对Action镜像拦截之前调用,相当于一个post-constructor方法,
Destroy方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源。
Intercept是拦截器的主要拦截方法,如果需要调用后续的Action或者拦截器,只需要在该方法中调用 另外AbstractInterceptor提供了一个简单的Interceptor的实现,这个实现为: public abstract class AbstractInterceptor implements Interceptor {
public void init() { }
public void destroy() { }
public abstract String intercept(ActionInvocation invocation) throws Exception; } 在不需要编写init和destroy方法的时候,只需要从AbstractInterceptor继承而来,实现intercept方法即可。
我们尝试编写一个Session过滤用的拦截器,该拦截器查看用户Session中是否存在特定的属性(LOGIN属性) public class CheckLoginInterceptor extends AbstractInterceptor { public static final String LOGIN_KEY = "LOGIN"; public static final String LOGIN_PAGE = "global.login";
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!"); // 对LoginAction不做该项拦截 Object action = actionInvocation.getAction(); if (action instanceof LoginAction) { System.out.println("exit check login, because this is login action."); return actionInvocation.invoke(); }
// 确认Session中是否存在LOGIN Map session = actionInvocation.getInvocationContext().getSession(); String login = (String) session.get(LOGIN_KEY); if (login != null && login.length() > 0) { // 存在的情况下进行后续操作。 System.out.println("already login!"); return actionInvocation.invoke(); } else { // 否则终止后续操作,返回LOGIN System.out.println("no login, forward login page!"); return LOGIN_PAGE; } } }
注册拦截器 <interceptors> <interceptor name="login" class="com.jpleasure.teamware.util.CheckLoginInterceptor"/> <interceptor-stack name="teamwareStack"> <interceptor-ref name="login"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors>
将上述拦截器设定为默认拦截器: <default-interceptor-ref name="teamwareStack"/> 这样在后续同一个package内部的所有Action执行之前都会被login拦截。
Struts2(XWork)提供的拦截器的功能说明:
注册并引用Interceptor <package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class=".."/> <interceptor name="logger" class=".."/> </interceptors>
<action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <result name="input">login.jsp</result> <result name="success" type="redirect-action">/secure/home</result> </action> </package>
可以将多个拦截器合并在一起作为一个堆栈调用,当一个拦截器堆栈被附加到一个Action的时候, <package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class=".."/> <interceptor name="logger" class=".."/> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> </interceptor-stack> </interceptors>
<action name="login" class="tutuorial.Login"> <interceptor-ref name="myStack"/> <result name="input">login.jsp</result> <result name="success" type="redirect-action">/secure/home</result> </action> </package>
上述说明的拦截器在默认的Struts2应用中,根据惯例配置了若干个拦截器堆栈,详细情参看struts-default.xml 其中有一个拦截器堆栈比较特殊,他会应用在默认的每一个Action上。 <interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="i18n"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>
每一个拦截器都可以配置参数,有两种方式配置参数,一是针对每一个拦截器定义参数,二是针对一个拦截器堆栈 <interceptor-ref name="validation"> <param name="excludeMethods">myValidationExcudeMethod</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>
<interceptor-ref name="defaultStack"> <param name="validation.excludeMethods">myValidationExcludeMethod</param> <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>
每一个拦截器都有两个默认的参数: excludeMethods - 过滤掉不使用拦截器的方法和 includeMethods ?C 使用拦截器的方法。
需要说明的几点: 1 拦截器执行的顺序按照定义的顺序执行,例如: <interceptor-stack name="xaStack"> <interceptor-ref name="thisWillRunFirstInterceptor"/> <interceptor-ref name="thisWillRunNextInterceptor"/> <interceptor-ref name="followedByThisInterceptor"/> <interceptor-ref name="thisWillRunLastInterceptor"/> </interceptor-stack> 的执行顺序为: thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptor thisWillRunFirstInterceptor
2 使用默认拦截器配置每个Action都需要的拦截器堆栈,例如: <action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/>
<result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action> 可以按照如下的方式定义: <interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/> </interceptor-stack> </interceptors>
<default-interceptor-ref name="myStack"/>
<action name="login" class="tutorial.Login"> <result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action>
3 如何访问HttpServletRequest,HttpServletResponse或者HttpSession 有两种方法可以达到效果,使用ActionContext: Map attibutes = ActionContext.getContext().getSession(); 或者实现相应的接口: HttpSession SessionAware HttpServletRequest ServletRequestAware HttpServletResponse ServletResponseAware |
〖 作者:feng_sundy 〗〖 大小:1k 〗〖 发布日期:2007-10-12 〗〖 浏览:3 〗 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="checkbox"> <param name="uncheckedValue">0</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="myStack"/>(这句是设置所有Action自动调用的拦截器堆栈) </package> struts-action.xml里面配置Action如下: <package name="LogonAdmin" extends="struts-shop">(这里扩展struts.xml里面定义的配置就可以了) <action name="logon" class="logonAction"> <result>/jsp/smeishop/admin/index.jsp</result> <result name="error">/jsp/smeishop/admin/logon.jsp</result> <result name="input">/jsp/smeishop/admin/logon.jsp</result> </action> <action name="logout" class="logoutAction"> <result>/jsp/smeishop/admin/logon.jsp</result> </action> </package> 你的拦截器可以正常工作了!!HOHO 以下是参考资料 struts2自带的配置及其拦截器配置
每一个Action既可以将操作转交给下面的拦截器,Action也可以直接退出操作返回客户既定的画面。
如何自定义一个拦截器? 自定义一个拦截器需要三步: 1 自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。 2 在strutx.xml中注册上一步中定义的拦截器。 3 在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明
Interceptor接口声明了三个方法:
public interface Interceptor extends Serializable {
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception; }
Init方法在拦截器类被创建之后,在对Action镜像拦截之前调用,相当于一个post-constructor方法,
Destroy方法在拦截器被垃圾回收之前调用,用来回收init方法初始化的资源。
Intercept是拦截器的主要拦截方法,如果需要调用后续的Action或者拦截器,只需要在该方法中调用 另外AbstractInterceptor提供了一个简单的Interceptor的实现,这个实现为: public abstract class AbstractInterceptor implements Interceptor {
public void init() { }
public void destroy() { }
public abstract String intercept(ActionInvocation invocation) throws Exception; } 在不需要编写init和destroy方法的时候,只需要从AbstractInterceptor继承而来,实现intercept方法即可。
我们尝试编写一个Session过滤用的拦截器,该拦截器查看用户Session中是否存在特定的属性(LOGIN属性) public class CheckLoginInterceptor extends AbstractInterceptor { public static final String LOGIN_KEY = "LOGIN"; public static final String LOGIN_PAGE = "global.login";
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("begin check login interceptor!"); // 对LoginAction不做该项拦截 Object action = actionInvocation.getAction(); if (action instanceof LoginAction) { System.out.println("exit check login, because this is login action."); return actionInvocation.invoke(); }
// 确认Session中是否存在LOGIN Map session = actionInvocation.getInvocationContext().getSession(); String login = (String) session.get(LOGIN_KEY); if (login != null && login.length() > 0) { // 存在的情况下进行后续操作。 System.out.println("already login!"); return actionInvocation.invoke(); } else { // 否则终止后续操作,返回LOGIN System.out.println("no login, forward login page!"); return LOGIN_PAGE; } } }
注册拦截器 <interceptors> <interceptor name="login" class="com.jpleasure.teamware.util.CheckLoginInterceptor"/> <interceptor-stack name="teamwareStack"> <interceptor-ref name="login"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors>
将上述拦截器设定为默认拦截器: <default-interceptor-ref name="teamwareStack"/> 这样在后续同一个package内部的所有Action执行之前都会被login拦截。
Struts2(XWork)提供的拦截器的功能说明:
注册并引用Interceptor <package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class=".."/> <interceptor name="logger" class=".."/> </interceptors>
<action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <result name="input">login.jsp</result> <result name="success" type="redirect-action">/secure/home</result> </action> </package>
可以将多个拦截器合并在一起作为一个堆栈调用,当一个拦截器堆栈被附加到一个Action的时候, <package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class=".."/> <interceptor name="logger" class=".."/> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> </interceptor-stack> </interceptors>
<action name="login" class="tutuorial.Login"> <interceptor-ref name="myStack"/> <result name="input">login.jsp</result> <result name="success" type="redirect-action">/secure/home</result> </action> </package>
上述说明的拦截器在默认的Struts2应用中,根据惯例配置了若干个拦截器堆栈,详细情参看struts-default.xml 其中有一个拦截器堆栈比较特殊,他会应用在默认的每一个Action上。 <interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="prepare"/> <interceptor-ref name="i18n"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>
每一个拦截器都可以配置参数,有两种方式配置参数,一是针对每一个拦截器定义参数,二是针对一个拦截器堆栈 <interceptor-ref name="validation"> <param name="excludeMethods">myValidationExcudeMethod</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>
<interceptor-ref name="defaultStack"> <param name="validation.excludeMethods">myValidationExcludeMethod</param> <param name="workflow.excludeMethods">myWorkflowExcludeMethod</param> </interceptor-ref>
每一个拦截器都有两个默认的参数: excludeMethods - 过滤掉不使用拦截器的方法和 includeMethods ?C 使用拦截器的方法。
需要说明的几点: 1 拦截器执行的顺序按照定义的顺序执行,例如: <interceptor-stack name="xaStack"> <interceptor-ref name="thisWillRunFirstInterceptor"/> <interceptor-ref name="thisWillRunNextInterceptor"/> <interceptor-ref name="followedByThisInterceptor"/> <interceptor-ref name="thisWillRunLastInterceptor"/> </interceptor-stack> 的执行顺序为: thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptor thisWillRunFirstInterceptor
2 使用默认拦截器配置每个Action都需要的拦截器堆栈,例如: <action name="login" class="tutorial.Login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/>
<result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action> 可以按照如下的方式定义: <interceptors> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="default-stack"/> </interceptor-stack> </interceptors>
<default-interceptor-ref name="myStack"/>
<action name="login" class="tutorial.Login"> <result name="input">login.jsp</result> <result type="redirect-action">/secure/home</result> </action>
3 如何访问HttpServletRequest,HttpServletResponse或者HttpSession 有两种方法可以达到效果,使用ActionContext: Map attibutes = ActionContext.getContext().getSession(); 或者实现相应的接口: HttpSession SessionAware HttpServletRequest ServletRequestAware HttpServletResponse ServletResponseAware |