URL Rewriting Example in JSF 2.0 using PrettyFaces

This is quite easy to add PrettyFaces in your existing JSF projects. By using PrettyFaces we can replace ugly URLs: “/faces/page.jsf” with pretty, SEO-friendly, human URLS: “/optimized/page/”; The client browser never sees your original URLs, even in the source-HTML. PrettyFaces solves several problems elegantly, such as: custom URL-rewriting, page-load actions, seamless integration with JSF navigation and links, dynamic view-id assignment, managed parameter parsing, and configuration-free compatibility with other JSF frameworks.

PrettyFaces is an OpenSource URL-rewriting library with enhanced support for JavaServer Faces – JSF 1.1, 1.2 and 2.0 – enabling creation of bookmark-able, pretty URLs. PrettyFaces solves the “RESTful URL” 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 web frameworks. Take a look at the features

Also see the PrettyFaces Docummentation which provides the sufficient information.

Here is a simple implementation of using PrettyFaces.

Step 01:
Download prettyfaces-jsf2-3.3.3.jar form the link and add the jar to the existing JSF2.0 Project’s class path.

Or if you are using Maven the use this Maven dependencies configuration

<dependency>
    <groupId>com.ocpsoft</groupId>
    <artifactId>prettyfaces-jsf2</artifactId>
    <version>3.3.3</version>
</dependency>

Step 02:

Now add this xml file to the WEB-INF folder.

pretty-config.xml

<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces
                      http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-prettyfaces.xsd">

	<url-mapping id="login">
		<pattern value="/home/" /> <!--url pattern-->
		<view-id value="/index.xhtml" /> <!--name of the page you want to change the url-->
	</url-mapping>
</pretty-config>

Step 03:
Now run your application. You will be able to see the index.xhtml page with the following URL
http://localhost:8080/project_name/home/

If you want to pass any url parameter to managed bean then your url-mapping can be like this-

<url-mapping id="someid">
	<pattern value="/path/#{mbobject.param}" />
	<view-id value="/yourpage.xhtml" />
</url-mapping>

Here param is the property name of your managed bean.

Now you can access yourpage.xhtml using this from any other page.

<a href="#{request.contextPath}/path/#{object.anyValue}">Link content here</a>

Or simply request http://localhost:8080/project_name/path/anyvalue from the browser.

Also you can display your request param value from yourpage.xhtml simply using-

#{mbobject.param}
 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

5,717` Total Views

Leave a Reply

Your email address will not be published. Required fields are marked *


nine × = 72