Looking for a JSF Tutorial or an easy to go  JSF example? Read on…

What you need ?

Tomcat server . You can download from here: http://tomcat.apache.org/download-55.cgi

And  20 minutes of your time…

What we will achieve ?

Your first JSF program. Nothing fancy. No bells and whistles- just a plain vanilla JSF program which will say hello to you.

Note: If you are looking for a JSF example to show you Backing Beans or JSF Navigation- then this is probably not for you. This is just to give you started real quick on JSF. Other good things about JSF will follow later.

Assumptions

That you are running your tomcat server on port 8080.

This example was tested on windows XP platform- but should really work anywhere.

Getting started

Step1 :D ownload the tutoral from here.

Step 2: Unzip the file under your webapps folder under Tomcat installation

Step 3: Copy this URL to your favourite browser

http://localhost:8080/jsftutorial/hellojsf.rn

Voila !!!

Tutorial explained

1) web.xml.

This specified the path for teh Faces-config file. the heart of a JSF application

<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>

This is to load the Faces Servlet before anything else.

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>

This will map any URL ending with *.rn to be routed to the Faces Servlet

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.rn</url-pattern>
</servlet-mapping>

2) Faces-config.xml: Deliberately kept empty. Just a place holder for you to do greater things later on.

3) hellojsf.jsp : A very simple JSP.

4) hellojsf.txt: This is a simple text file. which can be loaded just like any other web resource.

http://localhost:8080/jsftutorial/hellojsf.txt

I have included this as a troubleshooting aid- if you are unable to load the JSP try loading this instead. This bypasses the whole faces servlet so has a better chance of being loaded

What’s Next ?

This JSF tutorial was just to get your feet wet .Next logical step would be to get introduced to JSF Navigation and Backing Beans. I will be posting a tutorial on that soon.

If this example doesnt work for some reason – please leave a note and I will try to assist. Also  see my other posts on troubleshooting  http://www.rajivnarula.com/blog/tags/troubleshooting/

 

Context:

JSF application running on a Tomcat server

Symptoms:

When you try to access a resource in your appplication- you are greeted with a 404 error

Investigation

Check your error log.

If your exception trace is similar to one of the exceptions mentioned below mentioned below- you are missing one or both of these libraries: Commons-Digester or Commons- BeanUtils. (Note: You need both since Digister is dependent upon BeanUtils.)

SEVERE: Error configuring application listener of class com.sun.faces.config.ConfigureListener
java.lang.NoClassDefFoundError: org.apache.commons.digester.RuleSet

 

OR

SEVERE: Exception sending context initialized event to listener instance of class com.sun.faces.config.ConfigureListener
java.lang.NoClassDefFoundError: org.apache.commons.beanutils.MethodUtils

Resolution

Download Digister from here http://commons.apache.org/digester/

and BeanUtils from here http://commons.apache.org/beanutils/

You will need to put these libraries under the WEB-INF\lib folder of your application.

PS: If this fixes your problem  – please leave a note here or  if it doesn’t let me know either way

Good luck !!!

 

The error message

Named query not known: my.simple.query
    [junit] org.hibernate.MappingException: Named query not known: relationship.delete_by_userId
    [junit]  at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
    [junit]  at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1287)

The context

You are using a named query in your application. Named queries are externalized and are coded in the hibernate mapping files rather than the application code

Possible reason(s) for this error

1) The name of the query in the hibernate file doesnt match with what you have in the code. Both should match exactly

2) You have defined the query inside the class definition. A hibernate mapping file can have multiple class definitions (not a recomended or much practised approach though). A named query must exist outside the class definition.

Example(s)

<query name=”my.simple.query”>delete from Users as user where user.id =?</query

Session session = sessionFactory.openSession();
Query query = session.getNamedQuery(“my.simple.query”);
query.setInteger(0,userId);
session.beginTransaction();
query.executeUpdate ();
session.getTransaction().commit();

Did this solve your problem ?

Please let me know. Thanks !!!

© 2011 Technology Cafe Suffusion theme by Sayontan Sinha