我正在使用带有 Java/Jersey REST API 的 AngularJS 1.3 webapp。我终于让 html5mode 与 Tuckey UrlRewriteFilter 一起工作了这样一些简单的事情就像 本地主机:8080/#/页现在只是 本地主机:8080/页 .但是,我似乎无法得到更复杂的东西,例如 本地主机:8080/mypage/category/subcategory/item 好好工作。当在单个浏览器选项卡中时,此 url 将在 html5mode 中加载,但如果我在新选项卡中打开链接,则会收到 404。
这是我的 pom 和 web.xml 文件:
pom.xml
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confPath</param-name>
<param-value>/WEB-INF/urlrewrite.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
web.xml:
<urlrewrite>
<rule match-type="wildcard">
<from>*/page</from>
<to>index.html</to>
</rule>
// what should this rule look like in order to recognize a url like:
// localhost:8080/mypage/category/subcategory/item
<rule match-type="wildcard">
<from>?</from>
<to>index.html</to>
</rule>
</urlrewrite>
我通读了 UrlRewriteFilter 文档 here并尝试使用通配符匹配和正则表达式语法的不同变体,但我总是得到 404。
我还要补充一点,我使用的是 angular-ui-router 版本 0.2.13,我的相关页面/状态的 url 看起来像 /mypage/:category/:subcategory/:item .
所以我的问题是如何匹配这个模式,以便重定向到 index.html 可以正确地用于 html5mode 还是我应该以不同的方式配置路由以更好地与 UrlRewriteFilter 一起工作?
最佳答案
这是我最终使用的
<urlrewrite>
<rule match-type="regex" enabled="true">
<condition type="request-filename" operator="notfile" />
<condition type="request-filename" operator="notdir" />
<condition type="request-uri" operator="notequal">(\.html|\.js)</condition>
<from>^/html/(.*rootpage1.*|.*rootpage2.*)$</from>
<to last="true">/html/index.html</to>
</rule>
因为我的应用程序正在使用
<base href="/myapp/html/">
我还必须将 index.html 上的引用从相对更改为
href="/myapp/html/otherpage.html"
因为例如。当在/myapp/html/rootpage1/page1/something 上使用相对引用并且重写将 url 传递给 index.html 时,它在 rootpage1/范围内,而不是 html/范围。
关于angularjs - 无法让 Angular html5mode 和 UrlRewriteFilter 与某些 URL 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27712957/