jqp*_*jqp 5 spring tomcat servlets url-rewriting tuckey-urlrewrite-filter
我希望能够使用 Tuckey URLRewrite 过滤器在 Tomcat 中进行跨上下文请求转发。例如,我希望能够使用 SEO-/user-friendly URL 路由传入请求,如http://example.com/group-elements/300245/some-descriptive-text,其中“group-elements " 不是已部署应用程序的名称,不是映射到应用程序 'foo' 的 Java Spring 控制器方法的 URL,例如http://example.com/foo/app/group/300245/elements。我正在使用 Tomcat 7.0.27 和 URLRewrite 3.2.0;我正在使用 Java Spring 3.1 Web 应用程序。
所述URLRewrite 3.20文档注意到一个可选的“上下文”用于“到”滤波器参数元素属性:
If your application server is configured to allow "cross context" communication then this attribute can be used to forward (and only forward, not redirect or other "to" types) requests to a named servlet context.
On Tomcat, for instance, the application contexts in the server configuration (server.xml or context.xml) need the option crossContext="true". For instance, the two applications mentioned before ("app" and "forum") have to be defined as:
<Context docBase="app" path="/app" reloadable="true" crossContext="true"/>
<Context docBase="forum" path="/forum" reloadable="true" crossContext="true"/>
Run Code Online (Sandbox Code Playgroud)
鉴于这一点以及关于该功能的原始讨论,“上下文”属性似乎是我正在寻找的。但是,我无法正确启用跨上下文请求转发。
这是我在 conf/server.xml 中的“上下文”条目应用程序“foo”:
<Context docBase="foo" path="/foo" reloadable="true" crossContext="true"/>
Run Code Online (Sandbox Code Playgroud)
我在 webapps/ROOT/WEB-INF/ 中有我的 urlrewrite.xml 文件和 web.xml 文件。这是它们的样子:
urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite>
<rule>
<from>baz</from>
<!-- Note: this 'to' element's value has an error. See the edit at bottom of this post for corrected version. -->
<to context="foo">/foo/app/group/300245/elements</to>
</rule>
</urlrewrite>
Run Code Online (Sandbox Code Playgroud)
网页.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">
<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>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
上面在 urlrewrite.xml 中定义的规则是有意为基本和硬编码的。在这种情况下,我只是尝试在“to”和“from”中开发正则表达式之前,让规则的跨上下文方面起作用。
当我使用该规则请求http://example.com/baz时,Tomcat 返回 404 错误,指出“请求的资源 (/baz) 不可用”。我在“to”过滤器参数中尝试了一些变体,但还没有奏效。而且我还没有找到任何关于如何使用“上下文”的例子。
关于如何使这种跨上下文请求过滤起作用的任何想法?甚至有可能吗?我想我可以通过将 foo.war 重命名为 ROOT.war 或更改此处提到的根应用程序来实现我想要做的事情,但我想尝试通过 URLRewrite 执行此操作,除非这样做不可行或表面上是个坏主意。
如果显示更多我的配置会有所帮助,请告诉我。提前感谢您的任何意见。
编辑:
感谢克里斯托弗舒尔茨的有用回答。就我而言,问题是由两件事引起的:1)在 webapps/ROOT/META-INF 中没有 context.xml 文件,以及 2)在 webapps/ 中的 URL 重写规则中的“to”元素中有错误ROOT/WEB-INF/urlrewrite.xml。
修复包括在 webapps/ROOT/META-INF 中放置一个适当的 context.xml 文件。作为遇到此问题的其他人的参考,该文件最终如下所示:
webapps/ROOT/META-INF/context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="ROOT" path="/" reloadable="true" crossContext="true" />
Run Code Online (Sandbox Code Playgroud)
正如舒尔茨所提到的,只需要为给定 URL 重写规则(这里是 ROOT)中的“from”元素中隐含的上下文定义具有 crossContext="true" 的上下文。没有必要在“to”URL 重写规则中为应用程序明确定义上下文。换句话说,您不需要为该应用程序手动创建 context.xml 文件——因此继续上面的示例,您不需要手动定义 context.xml 文件并将其放入 webapps/foo/META-INF /.
Schultz 的回答反映了官方 Tomcat 文档中定义上下文的建议:http : //tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context。
这个问题也是由于我最初的帖子中的 URL 重写规则有错误造成的。正确的版本应该是:
urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite>
<rule>
<from>baz</from>
<!-- Note: the use of '/app' instead of '/foo/app/' below -->
<to context="foo">/app/group/300245/elements</to>
</rule>
</urlrewrite>
Run Code Online (Sandbox Code Playgroud)
如果您的(真实)Web 应用程序部署到,/foo并且您想要动态重写 URL,例如/group-elements/baz转发(而不是重定向)到/foo/app/group/300245/elements,那么您必须将重写过滤器部署到以下两个位置之一:/group-elements或/。
上面的配置似乎部署到 ROOT(实际上是 /),但随后将 URL 映射/baz到/foo/app/group/300245/elements. 相反,您可能想要这个:
<rule>
<from>/group-elements/baz</from>
<to context="foo">/foo/app/group/300245/elements</to>
</rule>
Run Code Online (Sandbox Code Playgroud)
看起来你正试图达到http://example.com/baz我期望的效果。最后一点神奇之处在于使ROOT上下文跨上下文(请注意,您的 web 应用程序不需要跨上下文:只有 urlrewrite 才需要)。您可以通过添加到 来将ROOTweb 应用程序更改为跨上下文。crossContext="true"webapps/ROOT/META-INF/context.xml
最后,您真的应该停止将<Context>元素放入 server.xml 中:将它们留在那里基本上意味着您需要重新启动 Tomcat 才能更改您的 Web 应用程序部署。
| 归档时间: |
|
| 查看次数: |
9609 次 |
| 最近记录: |