How to create user-friendly and seo-friendly urls in jsf?

依然范特西╮ 提交于 2019-11-29 04:20:09
BalusC

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?

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.

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, 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).

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.

Did you tried PrettyFaces?

Simple, effective, bookmark, JSF

PrettyFaces is an OpenSource extension for JSF1.2 and JSF2.0, which enables creation of bookmark-able, pretty URLs. PrettyFaces solves this problem elegantly, including features such as: page-load actions, seamless integration with faces navigation, dynamic view-id assignment, managed parameter parsing, and configuration-free compatibility with other JSF frameworks.

SEO and Consistency:

Any business knows how important Search Engine Optimization can be for sales. PrettyFaces allows SEO-friendly URLs, and improved customer experience. Give your site a uniform, well understood feeling, from the address bar to the buy button.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!