struts_3_4

过滤器与拦截器的区别:

*相同点:都是起拦截作用

*不同点:

      *作用范围:

*过滤器:使用范围是J2EE范畴,任何一个web工程都可以使用过滤器

*拦截器:使用范围是struts2范畴,离不开struts2框架的

    *完成的功能:

*过滤器:拦截请求资源

*拦截器:完成其他所有功能

*执行顺序:

过滤器——>拦截器

http客户端-->web容器--web.xml文件--过滤器

        --->struts.xml--->拦截器--->action--service--dao--数据库

   

常见的拦截器有哪些?

        在哪里配置

              struts-default.xml文件

package   name=”struts-default”

        <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="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" />

            <!-- Basic stack -->

            <interceptor-stack name="basicStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="multiselect"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>

                </interceptor-ref>

                <interceptor-ref name="conversionError"/>

            </interceptor-stack>

            <!-- Sample validation and workflow stack -->

            <interceptor-stack name="validationWorkflowStack">

                <interceptor-ref name="basicStack"/>

                <interceptor-ref name="validation"/>

                <interceptor-ref name="workflow"/>

            </interceptor-stack>

            <!-- Sample file upload stack -->

            <interceptor-stack name="fileUploadStack">

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- Sample model-driven stack  -->

            <interceptor-stack name="modelDrivenStack">

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- Sample action chaining stack -->

            <interceptor-stack name="chainStack">

                <interceptor-ref name="chain"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- Sample i18n stack -->

            <interceptor-stack name="i18nStack">

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="basicStack"/>

            </interceptor-stack>

            <!-- An example of the paramsPrepareParams trick. This stack

                 is exactly the same as the defaultStack, except that it

                 includes one extra interceptor before the prepare interceptor:

                 the params interceptor.

                 This is useful for when you wish to apply parameters directly

                 to an object that you wish to load externally (such as a DAO

                 or database or service layer), but can't load that object

                 until at least the ID parameter has been loaded. By loading

                 the parameters twice, you can retrieve the object in the

                 prepare() method, allowing the second params interceptor to

                 apply the values on the object. -->

            <interceptor-stack name="paramsPrepareParamsStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="alias"/>

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="multiselect"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>

                </interceptor-ref>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="chain"/>

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="staticParams"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</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>

            <!-- A complete stack with all the common interceptors in place.

                 Generally, this stack should be the one you use, though it

                 may do more than you need. Also, the ordering can be

                 switched around (ex: if you wish to have your servlet-related

                 objects applied before prepare() is called, you'd need to move

                 servletConfig interceptor up.

                 This stack also excludes from the normal validation and workflow

                 the method names input, back, and cancel. These typically are

                 associated with requests that should not be validated.

                 -->

            <interceptor-stack name="defaultStack">

                <interceptor-ref name="exception"/>

                <interceptor-ref name="alias"/>

                <interceptor-ref name="servletConfig"/>

                <interceptor-ref name="i18n"/>

                <interceptor-ref name="prepare"/>

                <interceptor-ref name="chain"/>

                <interceptor-ref name="scopedModelDriven"/>

                <interceptor-ref name="modelDriven"/>

                <interceptor-ref name="fileUpload"/>

                <interceptor-ref name="checkbox"/>

                <interceptor-ref name="multiselect"/>

                <interceptor-ref name="staticParams"/>

                <interceptor-ref name="actionMappingParams"/>

                <interceptor-ref name="params">

                    <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</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-ref name="debugging"/>

            </interceptor-stack>

            <!-- The completeStack is here for backwards compatibility for

                 applications that still refer to the defaultStack by the

                 old name -->

            <interceptor-stack name="completeStack">

                <interceptor-ref name="defaultStack"/>

            </interceptor-stack>

            <!-- Sample execute and wait stack.

                 Note: execAndWait should always be the *last* interceptor. -->

            <interceptor-stack name="executeAndWaitStack">

                <interceptor-ref name="execAndWait">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

                <interceptor-ref name="defaultStack"/>

                <interceptor-ref name="execAndWait">

                    <param name="excludeMethods">input,back,cancel</param>

                </interceptor-ref>

            </interceptor-stack>

       </interceptors>

拦截器 

拦截器栈   -->就是拦截器的集合

默认使用的拦截器栈  defualtStack

包含哪些拦截器:

拦截器都完成什么功能?

下面通过一个案例来理解struts2的拦截器    自定义的拦截器

操作:

在action的一个方法中,要进行权限的控制。如果是admin用户登入,就执行该方法,如果不是admin用户登入,就不能执行该方法

假设 你只有完成登陆之后才能完成其它的操作

1、 intercept方法中的参数invocation是执行action的上下文,可以从

这里得到正在访问的actionOgnl值栈、请求路径、方法名称、命名空间

等信息。以帮助程序员在拦截器中做相应的处理工作

分析:

http://localhost:8080/struts_interceptor/csdn/UserAction_delete.action?msg=%E5%88%A0%E9%99%A4%E7%9A%84%E9%99%84%E5%8A%A0%E7%9A%84%E5%8F%82%E6%95%B0

先执行拦截器  再执行action

拦截器  /csdn/UserAction_delete.action

/csdn/UserAction_query.action

判断用户是否存在的拦截器 :

要自定义拦截器需要实现com.opensymphony.xwork2.interceptor.Interceptor接口:

public class PermissionInterceptor implements Interceptor {

   private static final long serialVersionUID = -5178310397732210602L;

   public void destroy() {

   }

   public void init() {

   }

   public String intercept(ActionInvocation invocation) throws Exception {

  System.out.println("进入拦截器");

if(session里存在用户){

String result = invocation.invoke();

}else{

return logon;

}

//System.out.println("返回值:"+ result);

//return result;

    }

}

说明:

<package name="csdn" namespace="/test" extends="struts-default">

<interceptors>

           <interceptor name=permission" class="cn.csdn.aop.PermissionInterceptor" />

           <interceptor-stack name="permissionStack">

   <interceptor-ref name="defaultStack" />

  <interceptor-ref name=" permission " />

            </interceptor-stack>

  </interceptors>

<action name="helloworld_*" class="cn.csdn.action.HelloWorldAction" method="{1}">

<result name="success">/WEB-INF/page/hello.jsp</result>

<interceptor-ref name="permissionStack"/>

</action>

</package>

因为struts2中如文件上传,数据验证,封装请求参数到action等功能都是由系统默认的defaultStack中的拦截器实现的,所以我们定义的拦截器需要引用系统默认的defaultStack,这样应用才可以使用struts2框架提供的众多功能。

如果希望包下的所有action都使用自定义的拦截器,可以通过<default-interceptor-ref name=permissionStack/>把拦截器定义为默认拦截器。注意:每个包只能指定一个默认拦截器。另外,一旦我们为该包中的某个action显式指定了某个拦截器,则默认拦截器不会起作用。

怎么样获取 作用域中复合对象的属性值:

Action

    提供 次属性的 get方法

在页面中:

    <h4>欢迎${sessionScope.user.name}登陆后台管理</h4>

           

                    采用的是EL的写法:

           <br/>

            ${user.name} //page---request--session--application<br/>

            

            ${user["name"]}:::${user["name"]}<br/>

            

           ${sessionScope['user'].name }:: ${sessionScope['user'].name }<br/>

           ${sessionScope.user["name"] }:: ${sessionScope.user["name"] }<br/>

            

            

            

          <br/>

          利用struts2标签取值:page request  session application 

                 #作用域:

          <br/>

          <s:property  value="#session['user'].name"/>

          <br/>

          <s:property  value="#session.user.name"/>

          <s:property  value="#session.user['name']"/>

文件上传的时候,

   进行过滤 文件上传的类型

   文件上传的大小

   

   <action name="FileAction_*" class="www.csdn.struts_upload_interceptor.action.FileAction" method="{1}">

            <interceptor-ref name="fileUpload">

                <!-- 设置文件上传的类型:Tomcat/conf/web.xml文件 就有文件的类型的名称 -->

                <param name="allowedTypes">image/jpeg,image/pjpeg,application/octet-stream,application/x-zip-compressed</param>

                <param name="maximumSize">5242880</param>

            </interceptor-ref>

              <!-- 默认的拦截器栈 -->

            <interceptor-ref name="defaultStack"/>

            <result name="success">/index.jsp</result>

       </action>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值