Monday, January 12, 2009

How to start JSF

Software required

1. Jboss-4.0.2
2. Eclipse Europa
3. JSF jar file (jsf-api.jar, jsf-impl.jar, jstl.jar, commons-beanutils.jar, commons-collections.jar, commons-digester.jar, commons-logging.jar)

1. Open Eclipse Europa with workspace C:\jsf_workspace
2. New -> project -> web -> Dynamic Web Project
3. Enter Project Name JSFDemo (You can give any name)
4. Click next -> next -> finish.

5. Go to Server Panel and add server, choose JBoss v4.0 -> Next -> Browse -> and choose the location of Jboss 4.0 server in your local driver (in my case C:/jboss4.0.2/jboss-4.0.2).

6. Next - > Next -> add your project JSFDemo -> Finish
7. Right click on server and start.

8. Right click on project -> propertise -> Java build Path -> Libraries -> Add external Jar and all the above metinoed jars.

9. Go to JSFDemo -> WebContent -> WEB-INF -> delete every thing from web.xml and put this below code

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>JSFDemo</display-name>

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

<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
</web-app>

NOTE: Dont forget to add <listener> tag otherwise you will get below exception
javax.servlet.ServletException: Servlet.init() for servlet Faces Servlet threw exception org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39) 08:44:44,453 ERROR [[/JSFDemo]] StandardWrapper.Throwablejava.lang.NullPointerException at javax.faces.webapp.FacesServlet.init(FacesServlet.java:144) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1091)

10. Unser WebContent create one file with name First.jsp and put this below code

First.jsp
<%@ page session="false" contentType="text/html;charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
<html> <title> JSF DEMO BY BINOD </title><body>
<h:form><h:outputText value="Binod Kumar Suman"> </h:outputText></h:form>
</body></html>
</f:view>

NOTE: No need to add any tld file in web.xml

11. If server is not started, please start the server
12. Open internet explorer and type http://localhost:8080/JSFDemo/First.jsf,

you will get output Binod Kumar Suman

Now I am showing that how you will get input from input page then process it and will show in another page. First create one POJO with properties as same as you will have in input form. Second have to use navigation concept means how to navigate one page to other page
For both you have to write one config.xml (faces-config.xml), this file must in the same folder where web.xml reside So, I am writing one application, that will accept the student name and city. After click on save record button, other page should display with insert info.
Create one entry jsp and one result jsp. But before that we should have one POJO class with student properties.
Create StudentVO.java in one new package com.suman

StudentVO.java
package com.suman;
public class StudentVO {
private String name;
private int roll;
public String getName() {return name; }
public void setName(String name) {this.name = name;}
public int getRoll() {return roll;}
public void setRoll(int roll) { this.roll = roll;}
}

Dont forget to put all java class in WEB-INF\classes

studentInfoEntry.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<html> <head><title>JSF DEMO BY BINOD</title></head>
<body> <h:form> <h1>
<h:outputText value="STUDENT ENTRY FORM"/></h1> <br> <h:outputText value="Please Enter Student Name "/>
<h:inputText value="#{StudentInfoBean.name}" /> <br>
<h:outputText value="Please Enter Stdent Roll"/>
<h:inputText value="#{StudentInfoBean.roll}" /> <br>
<h:commandButton action="result" value="Save Record" />
</h:form> </body></html>
</f:view>

NOTE: Here button action ("result" in this case) must same as <from-outcome> tag value in faces-config.xml

result.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<html> <head><title>Result Page</title></head>
<body> <h3>
<h:outputText value="Student Name :: #{StudentInfoBean.name}" /> <h3>
<h:outputText value="Roll Number :: #{StudentInfoBean.roll}" />
</body> </html>
</f:view>

faces-config.xml

<?xml version="1.0"?>
<faces-config> <managed-bean>
<managed-bean-name>StudentInfoBean</managed-bean-name>
<managed-bean-class>com.suman.StudentVO</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean> <navigation-rule>
<from-view-id>/studentInfoEntry.jsp</from-view-id>
<navigation-case>
<from-outcome>result</from-outcome>
<to-view-id>/result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

Done every thing, clean the project and restart the server.
http://localhost:8080/JSFDemo/studentInfoEntry.jsf
You code should work perfectly .................... :)

Properties file concept

Now I am showing that how to use the properties file to avoid all the hard code of button name, input box name.

Create one properties file in src folder (after complition should go into web-inf\classes folder) say messages.properties
input_header=Student Entry Formprompt_student_name=Enter Student Name :: prompt_roll_number=Enter Student roll Number ::greeting_text=Welcome In IIM Bangalore button_text=Save Record
Now studentInfoEntry.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="messages" var="message"/>
<f:view>
<html>
<head>
<title>JSF DEMO BY BINOD</title>
</head>
<body> <h:form> <h1>
<h:outputText value="#{message.input_header}"/></h1> <br> <h:outputText value="#{message.prompt_student_name}"/>
<h:inputText value="#{StudentInfoBean.name}" /> <br>
<h:outputText value="#{message.prompt_roll_number}"/>
<h:inputText value="#{StudentInfoBean.roll}" /> <br>
<h:commandButton action="result" value="#{message.button_text}" /> </h:form> </body>
</html>
</f:view>

Dont forget to add <f:loadBundle basename="messages" var="message"/> in all jsp file, where you are using properties value. Now see that how to put the validation in input values.

Wait for a good tutorial on JBOSS SEAM .................... :)

3 comments:

  1. hi,

    Step5:
    c:\jboss-seam-2.1.1.GA> seam generate

    here is small mistake, not 'seam generate' (thats in step 4) but 'seam deploy'


    great tutorial with all details !

    ReplyDelete
  2. excellent tutorial man...keep it up:)

    ReplyDelete
  3. It is very nice post.It is good post. It is simple and best. It helpful for future use.
    You can test your knowledge in html by attending HTML Quiz .

    ReplyDelete