您的位置:首页 > 编程语言 > Java开发

struts2学习(13)——拦截器的配置

2013-08-19 10:55 375 查看
今天学习了struts2的拦截器功能,我们都知道struts2是基于weblogic和Filter拦截器为基础的。

struts2本身为我们提供了大量的拦截器,例如当我们设置的属性,request,application中的属性,这些属性的添加都是在拦截器的作用下完成的。

struts2的defaultStack中定义了框架本身自带的拦截器,我们可以通过打开struts-core-2.23.2(版本可能不同),在其中就有struts-default.xml文件,中间定义了struts的默认拦截器。





打开文件我们可以看到: <interceptors>
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
<interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
<interceptor name="cookieProvider" class="org.apache.struts2.interceptor.CookieProviderInterceptor"/>
<interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
<interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
<interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
<interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
<interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
<interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
<interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
<interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
<interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
<interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
<interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
<interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
<interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
<interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
<interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
<interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
<interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
<interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
<interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />

这就是struts给我们已经定义的interceptor,帮我们完成了我们大部分的功能。

当然在struts中自定义一个拦截器也是相当的简单的。

首先我们需要创建一个类,让它来完成拦截功能

import com.opensymphony.xwork2.interceptor.Interceptor;

public class permission implements Interceptor{

public void destroy() {

}

public void init() {

}

public String intercept(ActionInvocation invocation) throws Exception {
Object user = ActionContext.getContext().getSession().get("user");
if(user != null){
System.out.println("用户已经登录,放行");
return invocation.invoke();//说明用户已经登录,放行
}else{
ActionContext.getContext().put("message", "用户尚未登录,请先登录!");
System.out.println("用户尚未登录,请先登录");
return "message";
}
}

}

注意一个拦截器一定要实现import com.opensymphony.xwork2.interceptor.Interceptor
然后我们要注册这个拦截器

<interceptors>
<interceptor name="permission" class="cn.itcast.Interceptor.permission"></interceptor>
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="permission"></interceptor-ref>
</interceptor-stack>
</interceptors>

在这里我们

<interceptor name="permission" class="cn.itcast.Interceptor.permission"></interceptor>

注册了这个拦截器

之后我们有定义了一个拦截器栈,在这个拦截器栈中我们定义了struts2的默认拦截器和我们自定义的拦截器,这是因为当我们给一个action绑定拦截器的时候,倘若我们只将我们自定义的拦截器绑定上,那么这个action将失去struts2默认的拦截器功能,这是不可想象的。所以当我们给我们的action绑定拦截器的时候一定要加上defaultStack,这样既可以保证自定义的拦截器可以使用,也不会影响默认拦截器。

<action name="hello_*" class="cn.itcast.action.helloAction" method="{1}">
<result name="success">/WEB-INF/page/hello.jsp</result>
<interceptor-ref name="permissionStack"></interceptor-ref>
</action>

我们在这里绑定了拦截器。
这样我们的自定义拦截器就ok了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Struts 拦截器