我已经配置了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>
在urlrewriter.xml
文件夹下创建WEB-INF
<?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.
发布于 2018-01-23 19:20:36
如果将Tuckey URL重写筛选器放在web应用程序部署描述符中的Struts2筛选器之前,则可以重写任何Struts2。使用本指南:图基URLRewrite如何-到。
此外,您还可以在servlet容器前面为使用URL重写模式。使用本指南:面向初学者的URL重写。
https://stackoverflow.com/questions/48366518
复制