【问题标题】:Why URL rewriting is not working in Struts 2 [duplicate]为什么 URL 重写在 Struts 2 中不起作用 [重复]
【发布时间】:2018-06-30 04:40:50
【问题描述】:

我已经配置了 Tuckey URL 重写。我在pom.xml 中包含了依赖项:

 <dependency>
            <groupId>org.tuckey</groupId>
            <artifactId>urlrewritefilter</artifactId>
            <version>4.0.4</version>
        </dependency>

web.xml中添加了过滤器:

    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>commons</param-value>
        </init-param>
        <init-param>
            <param-name>confReloadCheckInterval</param-name>
            <param-value>60</param-value>
        </init-param>
    </filter> 
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

WEB-INF文件夹下创建urlrewriter.xml

  <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">

<urlrewrite default-match-type="wildcard">
    <!-- Struts -->
    <rule match-type="regex">
        <from>^/Profile/([0-9]+)$</from>
        <to>/Profile?id=$1</to>
    </rule>   
 
    <!-- Remove JSESSIONID from URLs when cookies disabled -->
    <!-- http://stackoverflow.com/questions/962729/is-it-possible-to-disable-jsessionid-in-tomcat-servlet -->
    <outbound-rule encodefirst="true" match-type="regex">
        <name>Strip URL Session ID's</name>
        <from>^(.*?)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$</from>
        <to>$1$2$3</to>
    </outbound-rule>
</urlrewrite>

现在我从浏览器请求http://localhost:8080/Test/Profile/123

这不会重定向到http://localhost:8080/Test/Profile?id=123

如何使它工作?

编辑:

在调试时,我在 Tomcat 日志中得到以下结果

24-Jan-2018 17:06:45.588 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: starting conf reload check
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: conf is not modified
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.utils.ServerNameMatcher DEBUG: looking for hostname match on current server name localhost
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: checking for status path on /test/Profile/2345
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriter DEBUG: processing request for /Profile/2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: Rule 0 run called with /Profile/2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: matched "from"
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.substitution.MatcherReplacer DEBUG: found 1
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.substitution.MatcherReplacer DEBUG: replaced sb is /Profile?id=2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleExecutionOutput DEBUG: needs to be forwarded to /Profile?id=2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriter DEBUG: rule is last
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: starting conf reload check
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: conf is not modified
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.utils.ServerNameMatcher DEBUG: looking for hostname match on current server name localhost
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: checking for status path on /test/Profile
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriter DEBUG: processing request for /Profile
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: Rule 0 run called with /Profile

Tomcat 控制台正在显示:

 MonitorFilter::WARNING: the monitor filter must be the first filter in the chain.

【问题讨论】:

  • 日志显示它有效。

标签: java jsp servlets struts2 tuckey-urlrewrite-filter


【解决方案1】:

如果在 Web 应用程序部署描述符中将 Tuckey URL rewrite 过滤器放在 Struts2 过滤器之前,则可以重写任何 URL。使用本指南:Tuckey URLRewrite How-To


您还可以在 servlet 容器前使用 Apache Web Server 的 URL 重写模块。使用本指南:URL Rewriting for Beginners

【讨论】:

  • tuckey 过滤器在我更新了相关 web.xml 的 struts2 过滤器之前
  • 阅读指南,调试,然后告诉我们问题。
  • 请求 url 匹配,它被转发 url 替换,但我的操作类没有被调用
  • 这是因为请求被转发到错误的 URL 并且操作映射器无法找到该 URL 的映射。如果您希望过滤器使用正确的正则表达式转发到操作,您应该阅读指南。
  • 我正在关注文档 tuckey.org/urlrewrite/manual/3.0/guide.html 从那里我得到了这个例子 &lt;rule&gt; &lt;from&gt;^/products/([0-9]+)$&lt;/from&gt; &lt;to&gt;/products/index.jsp?product_id=$1&lt;/to&gt; &lt;/rule&gt; /products/1234 将被传递给 /products/index.jsp?product_id=1234 。我找不到错误的地方
猜你喜欢
  • 2012-03-09
  • 2010-09-24
  • 1970-01-01
  • 2011-05-12
  • 2012-04-28
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 2022-07-11
相关资源
最近更新 更多