天天看點

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 标簽