url重写在struts2中不起作用

时间:2018-01-21 11:59:01

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

我已经配置了tuckey url重写我在pom中包含了依赖

 <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 log

获得了以下结果
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.

1 个答案:

答案 0 :(得分:0)

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

此外,您可以在servlet容器前使用Apache Web Server的URL重写mod。使用本指南:URL Rewriting for Beginners