Struts2学习记录

Posted 亦真亦假,何必当真

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2学习记录相关的知识,希望对你有一定的参考价值。

一、interceptor拦截器的使用

第一种情况(指定action使用该拦截器):struts.xml文件的配置:

<interceptors>
  <interceptor name="myinterceptor" class="loginInterceptor"/>
  <interceptor-stack name="loginStack">
    <interceptor-ref name="myinterceptor"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>

<action name="go" class="testAction">
  <result name="success">index.jsp</result>
  <interceptor-ref name="loginStack"></interceptor-ref>
</action>

第二种情况(所有action使用该拦截器):struts.xml文件的配置:

<interceptors>
  <interceptor name="myinterceptor" class="loginInterceptor"/>
  <interceptor-stack name="loginStack">
    <interceptor-ref name="myinterceptor"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>
<default-interceptor-ref name="loginStack"/>

PS:必须要添加defaultStack的默认拦截器,不然依赖注入参数受到影响

二、struts.xml配置文件各节点先后顺序问题

package节点的子节点一定要按照如下的顺序配置,不然启动时会报错。

      result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-exception-mappings?,action*

例如:global-results节点一定放要在action节点前面,interceptor节点后面(也就是按照上面的节点顺序排列)

三、防止页面重复提交,使用token拦截器

拦截器配置:

<interceptors>
  <interceptor-stack name="tokenStack">
    <interceptor-ref name="token"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>

定向结果配置:

<action name="serch" class="getUser">
  <result name="success">showUser.jsp</result>
  <result name="invalid.token">error.html</result>
</action>

表单页面:

1、引入Struts2标签库:<%@ taglib prefix="s" uri="/struts-tags" %>

2、表单包含该标签:<s:token></s:token>。

注意事项:上面的action一定要继承ActionSupport类,不然运行抛出空指针异常



























以上是关于Struts2学习记录的主要内容,如果未能解决你的问题,请参考以下文章

JavaEE企业应用实战学习记录struts2登录

Struts2学习记录-Value Stack(值栈)和OGNL表达式

学习记录

Struts2学习————Struts2入门

Struts2源码学习——Struts2中的XWork容器

Struts2表达式封装Action获取不到表单数据的一次记录