May 17

Nowadays having SEO friedly links is very important if you want to list higher on the search engines. And with JSF this can be a big pain in the a**. There is an extension available called prettyfaces. For my applicaiton i thought of first using prettyfaces rather then developing my own enhancement to save me some time. But then i ended up developing my own prettyfaces:) as i wanted some extra functionality specific to my application which wasn’t possible using prettyfaces.

So i have my own custom tag called this tag along with some other params takes in the href paramter and rewrites it to a SEO friendly link and outputs it.

Then when this link is clicked you need to convert it back to the normal link for example with parameters. For that you need to have a servlet defined in your web.xml which gets the SEO friendly URL rewrites it to a normal url and then dispatches it to your faces servlet.

If you want to have a look at my application using this enhancement to convert the links to SEO friendly links visit, its running on JSF!

Tagged with:
Feb 05

I spent almost 2 days putting these three together to generate a test application. The beauty about this app is it dose not have any xml configuration file except of course the applicationcontext.xml for spring. And in that file also there is just the datasource defined. I thought of putting it down here in order to save somebody stumbling with the same some time.

What Does the app do?

well its just a test application. It shows how to wire everything together just using the annotations. and it also shows the Ajax functionality and the templating functionality of JSF 2.0. and it also inserts some test entity into the Database

Lets get directly dirty with the code

Lets start with the applicationContext.xml file ..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"
        p:driverClassName="${jdbc.driverClass}" p:password="${jdbc.password}" p:url="${jdbc.url}"
        p:username="${jdbc.username}"> </bean>
    <bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        id="sessionFactory" p:dataSource-ref="dataSource">

        <property name="annotatedClasses">
        <property name="hibernateProperties">
            <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.show_sql=${jdbc.showSql} </value>

    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        id="transactionManager" p:sessionFactory-ref="sessionFactory"/>
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory" lazy-init="false" p:dataSource-ref="dataSource">
        <property name="persistenceUnitName" value="testsource"/>

    <!-- Activates annotation-based bean configuration -->

    <!-- needed is for @Configurable -->
    <context:component-scan base-package="com.test"/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value=""/>

As you see all you have done is defined your datasource and wired it with entityManagerFactory which will be used by hibernate. All the connection properties are comming from the file which is looked up from the classpath so it has to be placed inside the classes directory within WEB-INF.  The next thing to do is to add the persistance.xml in META-INF folder which looks like this

<persistence version="1.0" xmlns=""
    <persistence-unit name="testsource" transaction-type="RESOURCE_LOCAL">
             | The SQL dialect to use for this data source, use MySQL InnoDB as
             | we only use InnoDB tables for the entity beans.
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
             | Specify the second level query cache for hibernate.
             | Enable query caching availability.
            <!-- <property name="hibernate.cache.use_query_cache" value="true"/> -->


As you must have noticed here we only declare the persistence-unit name which is used by spring.
And the last xml file is the faces-config.xml. i know i said just one configuration :P . here we dont define any beans or navaigation rules here we just have our resource bundle and the reslover for faces beans.

<?xml version="1.0"?>
<faces-config xmlns=""

so now its all configured. Lets start with the entity class. It looks like this

public class Car implements Serializable{

 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;
 private String carName;

next is the DAOimpl class

public class CarDaoImpl implements CarDao{

 private EntityManager entityManagerFactory;

 void setEntityManager(EntityManager entityManager) {
 this.entityManagerFactory = entityManager;

 public void addCar(Car car){



The @Repository annotation is yet another stereotype that was introduced in Spring 2.0 itself. This annotation is used to indicate that a class functions as a repository.

then we have the spring service classes

public class CarServiceImpl implements CarService{

 private final CarDao carDao;

 CarServiceImpl(CarDao carDao) {
 this.carDao = carDao;
 } ...

and finally the backing bean

public class CarSession {
 private Car car;

 public Car getCar() {
 return car;
 } ..

we use annotations for defining managed beans which otherwise would have been declared in ur config file so this significantly reduces your XML, but you can get rid of virtually all of it with JSF 2 through either annotations, as I’m doing for managed beans, or with convention, as is the case for JSF 2’s simplified navigation handling.
so now we have everything in palce .. the only files left are the xhtml files

<ui:composition xmlns=""

  <ui:define name="menuLeft">
    <!-- <ui:include src="/sections/login/menuLeft.xhtml"/>-->

  <ui:define name="content">
     <ui:include src="/sections/welcome.xhtml"/>


and the file sections/welcome.xhtml looks like this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns=""

	<h:form id="form" prependId="false" >
    <h:outputText value="#{i18n.prefs_country}"/>
            <h:selectOneListbox id="company" value="#{}" size="1">
              <f:selectItems value="#{facesSupport.carModels}"/>
              <f:ajax render="model"/>
            <h:outputText value="#{i18n.prefs_region}"/>
            <h:selectOneListbox id="model" value="#{car.model}" style="width: 180px;" size="1">
              <f:selectItems value="#{car.modelOptions}"/>
     <h:commandButton id="loginButton" value="save" action="#{car.saveCar}"/>

as you see how ajax is used to render the model drop box when ever the company box is changed.

the last thing left is the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""


      <param-value>/WEB-INF/web-application-config.xml </param-value>


    <servlet-name>Faces Servlet</servlet-name>

    <servlet-name>Faces Servlet</servlet-name>




Thats it .. everything is in place. soon i will zip my test application and upload it so that its easier to go through the code..

Well if there are any questions or suggestions feel free to leave a comment

Finally got some time to create a sample application .. you can download it from here have also included spring security 3.0 into it .. so enjoy ;) ..u can just change from .zip to .war and deploy the application ..but before that dont forget to change the properties file inside /WEB-INF/config add your database connection details there... if there are any questions just write a comment ... and ya .. the source is included inside the war file ... the war file has been tested with tomcat 6 just place it on webapps .. and point ur browser to http://localhost:8080/sample/index.html ..

Tagged with:
preload preload preload