更新时间:2023-11-07 22:54:22
如果这是对现有应用程序的改进,则基本上需要如何在Java中使用servlet过滤器来更改传入的servlet请求url?
If this is intended as an improvement of an existing application, then you basically need a Filter
which detects "dirty" and "friendly" URLs. When it detects a "dirty" URL, then it should redirect the request to a "friendly" URL by HttpServletResponse#sendRedirect()
. When it detects a "friendly" URL, then it should forward the request to the "dirty" URL by RequestDispatcher#forward()
. An example can be found in this related question: How to use a servlet filter in Java to change an incoming servlet request url?
此外,您还需要自定义 <h:form>
,<h:link>
等生成所需的友好" URL.可以在此处找到示例:
Further, you also need a custom ViewHandler
to produce the desired "friendly" URL for JSF <h:form>
, <h:link>
, etc. An example can be found here: Dynamic Directory in Java EE Web Application.
如果这是一个新应用程序或可以更改的应用程序,则可以考虑使用任何现有的漂亮URL库,而无需重新发明***:
If this is a new application or an application which is open to changes, you could consider any of the existing pretty URL libraries instead of reinventing the wheel:
pretty-config.xml
.如果您要完全更改URL和/或配置从旧URL到新URL的重定向,则此库很有用.web.xml
上下文参数进行扩展.它还支持"MultiViews",从而可以声明性地将路径参数注入到托管bean中.例如. /foo/bar/baz
可以指向/foo.xhtml
,值bar
和baz
可以由@Param(pathIndex)
注入.pretty-config.xml
. This library is useful if you want to completely change URLs and/or want to configure redirects from old to new URLs.web.xml
context param. It also supports "MultiViews" whereby path parameters can declaratively be injected in managed beans. E.g. /foo/bar/baz
can point to /foo.xhtml
and the values bar
and baz
can be injected by @Param(pathIndex)
.还有
There's also the experimental PrettyUrlPhaseListener of Mojarra Scales library, but it's an old library and PrettyFaces is largely based on it, so it's not worth the effort.