javablogspot

Just another WordPress.com weblog

Tutorial: Creating JavaServer Faces JSF application in Eclipse

Posted by damuchinni on March 2, 2009

Let us see how to create a simple application using JavaServer Faces or JSF framework in Eclipse IDE. First let us see what are the tools required to create our hello world JSF application.

1. JDK 1.5 above (download)
2. Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
3. Eclipse 3.2.x above (download)
4. Sun Reference Implementation of JSF: (download). Following are the list of JAR files required for this application.
* jsf-impl.jar
* jsf-api.jar
* jstl.jar
* common-logging.jar
* common-beanutils.jar
* common-collections.jar
* common-chain.jar
* common-digester.jar

We will implement a JSF application with an Add User screen with two fields, ID and User Name. Once user enter these values and press submit, she will be redirected to a welcome page displaying the user name.

Let us start with our first JSF based web application.
Step 1: Create Dynamic Web project

Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.
dynamic web project eclipse

Select Dynamic Web application and click Next.
create dynamic web project jsf

Write the name of the project HelloWorldJSF. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. In configuration select JavaServer Faces v1.2 Project and press Next.

jsf-new-project

On Project Facets window, select Java 5 and JSF 1.2 and press Next.

Skip Web module window and press Next.

jsf-capabilities-face-servlet

Select JSF component library. Click New in Component Libraries and add jstl.jar, jsf-api.jar and jsf-impl.jar. In URL Mapping Patterns add /faces/* and then click Finish.

Once the project is created, you can see its structure in Project Explorer.
jsf-project-explorer-view
Step 2: Create Package and Managed bean

Create a package net.viralpatel.jsf.helloworld in the source folder and create a Java file UserBean.java. Copy following content into UserBean.java.
view plaincopy to clipboardprint?

1. package net.viralpatel.jsf.helloworld;
2.
3. public class UserBean {
4. private int id;
5. private String name;
6.
7. //Action method to add user
8. public String addUser() {
9.
10. return “success”;
11. }
12. public int getId() {
13. return id;
14. }
15. public void setId(int id) {
16. this.id = id;
17. }
18. public String getName() {
19. return name;
20. }
21. public void setName(String name) {
22. this.name = name;
23. }
24. }

package net.viralpatel.jsf.helloworld;

public class UserBean {
private int id;
private String name;

//Action method to add user
public String addUser() {

return “success”;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Above Java class is a User bean that we will use to store our user’s information. This class acts like a form bean and action class. The addUser() method will get called when we click Add button on our Add User page.
Step 3: Create JSP files

Create two JSP files: AddUser.jsp and ListUser.jsp in WebContent folder. Copy following content in each of these files.
AddUser.jsp

view plaincopy to clipboardprint?

1.
2.
3.
4.
5.
6. Add New User Form
7.
8.
9.
10.

11.
12.

13.
14.
15.
16.
17.
18.
19.
20.
21.
23.
24.
25.
26.
27.

Add New User Form

We have added a validation rule for ID using f:validateLongRange tag and required=”true” attribute. The ID must be in between 1 and 500.
ListUser.jsp
view plaincopy to clipboardprint?

1.
2.
3.
4.
5.
6. List of Users
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.

List of Users

Step 4: Modify faces-config.xml file

Open faces-config.xml from WebContent -> WEB-INF folder and copy following content into it.
view plaincopy to clipboardprint?

1.
2.
3.
7.
8. userBean
9.
10. net.viralpatel.jsf.helloworld.UserBean
11.
12. session
13.
14.
15. AddUser
16. /AddUser.jsp
17.
18. success
19. /ListUser.jsp
20.
21.
22.

userBean

net.viralpatel.jsf.helloworld.UserBean

session

AddUser
/AddUser.jsp

success
/ListUser.jsp

In faces config we have defined a managed bean UserBean with scope session and mapping from AddUser.jsp to ListUser.jsp.
Step 5: Execute and run the project

Final step is to execute the project and view it in browser.
For this, right click on Project Name in Project Explorer -> Run As -> Run on Server (Shortcut Alt+Shift+X, R).

add-new-user-jsf-project-eclipse

Once you enter ID and Username and press Add User, following success screen will appear.

add-user-success-jsf-project-eclipse
Download complete WAR file with source
http://viralpatel.net/blogs/download/jsf/HelloWorldJSF.war

Advertisements

One Response to “Tutorial: Creating JavaServer Faces JSF application in Eclipse”

  1. Your code is not showing up correctly. you might want to look into the following link… http://numberformat.wordpress.com/2009/05/10/syntax-highlighting-in-blogs/

    Also please look into maven:
    http://numberformat.wordpress.com/2009/10/28/maven-for-spring-framework-developers/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: