且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在jsf中创建用户友好和seo友好的URL?

更新时间: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?

此外,您还需要自定义 为JSF <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:

  • PrettyFaces ,这是一个完整的URL重写解决方案.它需要一个附加的XML配置文件pretty-config.xml.如果您要完全更改URL和/或配置从旧URL到新URL的重定向,则此库很有用.
  • FacesViews "nofollow noreferrer> OmniFaces 库,该库使现有的URL只能通过单个web.xml上下文参数进行扩展.它还支持"MultiViews",从而可以声明性地将路径参数注入到托管bean中.例如. /foo/bar/baz可以指向/foo.xhtml,值barbaz可以由@Param(pathIndex)注入.
  • PrettyFaces, which is a complete URL rewrite solution. It requires an additional XML configuration file 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.
  • FacesViews of OmniFaces library, which makes existing URLs just extensionless by a single 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).

还有 PrettyUrlPhaseListener kenai.com/projects/scales/pages/Home"rel =" nofollow noreferrer> Mojarra Scales 库,但是它是一个旧库,PrettyFaces很大程度上基于它,因此不值得付出努力.

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.