For example, I have class Article with methods getTitle ()
and getContent ()
.
I also have ArticlesService with method getAllArticles ()
. How to create a list of links with meaningful names (formed with #{article.title}
)? Like:
http://mysiteaddress.com/article/first-article-title
http://mysiteaddress.com/article/how-to-make-links-in-jsf
..or something similar.
I can create links with all necessary functionality with <h:commandLink>
, but I don't know how to make nice 'href' for it: it always has href '#'.
I can create nice links with <h:outputLink>
but I don't know how to add necessary functionality to it.
In jsp I created my own front-controller, which parsed urls from requests and then performed redirection to correspondent jsp-page.
How to achieve the same functionality in JSF?
What are SEO friendly URLs? SEO friendly URLs are URLs that are designed to meet the needs of users and searchers. Specifically, URLs optimized for SEO tend to be short and keyword-rich.
The generateSeoURL() function automatically creates SEO friendly URL slug from the string using PHP. A title string needs to be passed as input and it returns a human-friendly URL string with a hyphen ( - ) as the word separator. $string – Required. The string which you want to convert to the SEO friendly 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?
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:
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.
Did you tried PrettyFaces?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With