Tuckey UrlRewriteFilter和Struts2在第一次请求时提供404

时间:2013-10-30 04:36:50

标签: struts2 http-status-code-404 tuckey-urlrewrite-filter

我的大约30个重写链接工作正常,但我有一些在第一次被调用时无法工作,但他们第二次工作!

这是web.xml片段:

   
<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
        <param-name>actionPackages</param-name>
        <param-value>uk.co.prodia.prosoc.struts2.action</param-value>
    </init-param>
</filter>
<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

这是urlrewrite.xml,它第一次调用时会给出404:

   
<rule>
    <from>^/forgotten-password$</from>
    <to>/unsecured/forgotten-password!input.action</to>
</rule>

和struts2配置该操作:

   
<package name="unsecured" extends="struts-default" namespace="/unsecured">
    <action name="forgotten-password" class="actionForgottenPassword">
        <result name="input">/WEB-INF/view/unsecured/forgotten-password.jsp</result>
        <result name="passwordNotFound">/WEB-INF/view/unsecured/forgotten-password.jsp</result>
    </action>
</package>

但是,如果我让urlrewrite.xml通过包含unsecured操作的forgotten-password包,那么它可以正常工作:

   
<rule>
    <from>^/unsecured/forgotten-password$</from><!--works through /unsecured -->
    <to>/unsecured/forgotten-password!input.action</to>
</rule>

似乎从urlrewrite中缺少Struts2包名会在第一次使用URI时导致404。

这对任何人都有意义,还是我只能留在疯狂的城镇?

1 个答案:

答案 0 :(得分:1)

问题似乎是由上下文第一页加载中的URL中的jsessionid引起的。我对regexp有点无用,但这似乎有效:

<rule>
    <from>^/forgotten-password(;jsessionid=.*)?$</from>
    <to>/unsecured/forgotten-password!input.action</to>
</rule>

我将(;jsessionid=.*)?添加到每个规则的末尾,似乎有效。