天天看点

struts 设置默认 action

struts 如何设置默认action呢?

我要达到的目的是:访问不存在的action时自动跳转到默认的action

在struts.xml中添加:

struts 设置默认 action

<!-- 404页面 -->  

        <default-action-ref name="notfound" />  

<action name="notfound" class="com.common.action.error.notfounderreraction">  

            <result name="success">/error/not_found.jsp</result>  

        </action>  

 效果如下:

struts 设置默认 action

 action  aaa/xxx.action 不存在,所以自动跳转到了notfound.

但是现在有一个问题,如果我的url是http://localhost:8082/shop_goods/acc 时,界面如下:

struts 设置默认 action

 这是为什么呢?

我检查我的web.xml发现struts 过滤器配置如下:

<filter>

    <filter-name>struts2</filter-name>

    <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>

  </filter>

  <filter-mapping>

    <url-pattern>*.action</url-pattern>

  </filter-mapping>

所以struts只会处理url后缀名为action的,比如xxx.action,abc.action.

解决方法:struts 过滤器配置改为:

struts 设置默认 action

<filter>  

    <filter-name>struts2</filter-name>  

    <filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>  

  </filter>  

<filter-mapping>  

    <url-pattern>/*</url-pattern>  

  </filter-mapping>  

修改之后的效果:

struts 设置默认 action

如果struts过滤器非要使用*.action呢?

那么需要在web.xml中添加:

struts 设置默认 action

<error-page>  

            <error-code>404</error-code>  

            <location>/error/not_found.jsp</location>  

        </error-page>  

 这样,访问http://localhost:8082/shop_goods/acc 也会自动跳转到/error/not_found.jsp

注意:上述页面(/error/not_found.jsp)不能有struts 标签