Wednesday, January 21, 2009

First Seam Example

I tried a lot, to execute one simple application in SEAM. But again and again I got fail. I visited a lot of link but didnt get any clear running simple example. Even one of my friend is having some year experience in Seam and JSF, but she also didnt help me. From that day I was struggling to execute even a simple and first Seam example. Today I got success. :)

My previous post might be not run, but this post is fully tested by me and it is working. You can go throught stet by step.

Software Required.
1. jboss-seam-2.1.1.GA
2. jboss-4.2.0.GA
3. Eclipse Europa
4. PostgreSQL 8.1.0

Suppose you have all these software in C drive. (I am assuming.)
Please copy database drive jar file into jboss-seam lib. (Copy C:\PostgreSQL\8.1\jdbc\postgresql-8.1dev-403.jdbc2ee.jar INTO C:\jboss-seam-2.1.1.GA\lib)
Step1 : Create one workspace like C:\Workspace_Seam
Step2: Open dos prompt
c:\jboss-seam-2.1.1.GA>seam setup
It will ask many configuration, put value as below
Enter you java project workspace : C:\Workspace_Seam
Enter your JBoss AS home directory: c:\jboss-4.2.0.GA
Enter the Project name : Binod_SEAM_Proj
Then put three time enter key (Accepting default values)
Enter Java package name for your session bean : com.suman.session
Enter Java package name for your entity bean : com.suman.entity
Enter Java package name for your test cases : com.suman.test
What kind of database are you using : postgres
Enter the Hibernate dialect for your database : org.hibernate.dialect.PostgreSQLDialect
Enter the filesystem path to JDBC driver jar : C:\PostgreSQL\8.1\jdbc\postgresql-8.1dev-403.jdbc2ee.jar
Enter JDBC drive class for your database : org.postgresql.Driver
Enter JDBC URL for your database : jdbc:postgresql://localhost:5432/postgres
Enter database user name: postgres
Enter database password: suman
Put enter key 5 times to set default value.

Step3:c:\jboss-seam-2.1.1.GA> seam create-project
It will create one folder Binod_SEAM_Proj (Your project name) in C:\Workspace_Seam (Your workspace). And copy a lot of files including one project in Binod_SEAM_Proj folder.

Step4:
c:\jboss-seam-2.1.1.GA> seam generate
It will build the application and generate ear file i.e. Binod_SEAM_Proj.ear inside the C:\Workspace_Seam\Binod_SEAM_Proj\exploded-archives folder.

Step5:
c:\jboss-seam-2.1.1.GA> seam deploy
It will deploy the application and copy Binod_SEAM_Proj.ear to C:\jboss-4.2.0.GA\server\default\deploy

Step6: Start the jboss server
C:\jboss-4.2.0.GA\bin>run.bat
You will get message in server console like this
00:40:10,406 INFO [EARDeployer] Started J2EE application: file:/C:/jboss-4.2.0.GA/server/default/deploy/Binod_SEAM_Proj.ear/

Step7:
Open internet explorer and type
http://localhost:8080/Binod_SEAM_Proj
You will get one page having written
Welcome to Seam!
Your seam-get project is deployed! Here are some ...........

Step8:
Open Eclipse and put the workspace C:\Workspace_Seam
File -> Import -> General -> Existing Projects into Workspace -> Browse -> choose Binod_SEAM_Proj
Goto C:\jboss-seam-2.1.1.GA\examples\registration\src\org\jboss\seam\example\registration
and copy Register.java and RegisterAction.java and paste into Eclipse Binod_SEAM_Proj->src/hot-> suman.session
It will show some exception. So change the package in both java file from
package org.jboss.seam.example.registration; to package suman.session;
It will show one more exception, that would be solve in next step.

Step9:
Copy User.java from C:\jboss-seam-2.1.1.GA\examples\registration\src\org\jboss\seam\example\registration into Eclipse Binod_SEAM_Proj -> src/main -> suman.entity. Change the package name from
package org.jboss.seam.example.registration; to package suman.entity;
Now there woule not be any exception.

Step10: Go to E:\SEAM\jboss-seam-2.1.1.GA\jboss-seam-2.1.1.GA\examples\registration\view and copy all files. (index.html, register.xhtml, registered.xhtml) into Eclipse Binod_SEAM_Proj -> View. (Overwrite all files).

Step11:
Stop the jboss server
Delete Binod_SEAM_Proj.ear from C:\jboss-4.2.0.GA\server\default\deploy

Step12: Right click on build.xml -> Run As -> Ant Build
Again it will build the application and deploy the application.
Start the jboss server C:\jboss-4.2.0.GA\bin>run.bat
Open internet explorer and put
http://localhost:8080/Binod_SEAM_Proj2
You will get User Entry form having Username, Real Name, Password
Put data in all these text field and click on Register button.
You will get one confirmation message.
Now go to database, here one table is created with name users. That will have all the above given information.

Thats it .................. :)

Thursday, January 15, 2009

HOW TO SETUP SEAM PROJECT SEAM TUTORIAL

I visited many links and go through many books, but didnt any easy to setup the SEAM project.

After a lot of R&D on this set up, now sharing with you.

Software Required

1. jems-installer-1.2.0.GA (jems-installer-1.2.0.GA.jar) [ Download Link ]

2. Eclipse Europa [ Download Link ]

3. HSQL DB [ Download Link ]


Supposer you have kept all these software in E:\SEAM
A. Install jboss-4.0.5.GA using jems-installer-1.2.0.GA

1. E:\seam\java -jar jems-installer-1.2.0.GA.jar (Press Enter)

2. You will get language choose pop up, select English

3. Next -> Next -> Accept terms and conditions (after confirm)Change select the installation path to D:\seam_work\jboss-4.0.5.GA

4. Select ejb3 during installation

5. let it standard -> Next

6. Set JBOSS_HOME=D:\seam_work\jboss-4.0.5.GA in environment variable

7. Check JAVA_HOME and JBOSS_HOME setup or not

8. C:\>set jJAVA_HOME=C:\jdk1.5.0_12JBOSS_HOME=D:\seam_work\jboss-4.0.5.GA
9. Start the jboss server
D:\seam_work\jboss-4.0.5.GA\bin> run.batafter start server, just confirm with http://localhost:8080/
Now configure the seam project
E:\SEAM\jboss-seam-2.1.1.GA>seam setupEnter your java project workspace C:\jsf_workspaceEnter your Jboss home directoryD:\seam_work\jboss-4.0.5.GAEnter the project namemySeamProjThen just press enter, enter ........
You will get message
BUILD SUCCESSFULL
E:\SEAM\jboss-seam-2.1.1.GA\jboss-seam-2.1.1.GA>seam new-project
now to check C:\jsf_workspace\mySeamProj, many file and folder copied to this folder.
Now open Eclisp and set workspace C:\jsf_workspaceFile -> Import -> General -> Existing Projects into Workspace -> Browse -> c:\jsf_workspace\mySeamProj -> Finish
A build file exceutes automatically and create the ear file.




Wednesday, January 14, 2009

How to add Validation in JSF

Continue from previous blog ..........
Validation in JSF

Now see that how to put the validation in input values (using previous blog example).

1. Code to check for no input for a name to make sure that no empty name is submitted. To do that have to use required attribute of inputText tag.

<h:inputText value="#{StudentInfoBean.name}" required="true">

2. Code to check for reasonable length of input for a name. To do that have to use f:validateLength tag.
<f:validateLength minimum="2" maximum="10"/>

Now complete code for name input
<h:inputText value="#{StudentInfoBean.name}" required="true">
<f:validateLength minimum="2" maximum="10"/>
</h:inputText>

3. Still error message will not show on the screen, to show error message have to add this below code
<h:messages style="color:darkred"/>

Now complete 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>

<p>
<h:messages style="color:darkred"/>
</p>

<h:form>
<h1><h:outputText value="#{message.input_header}"/></h1> <br>

<h:outputText value="#{message.prompt_student_name}"/>
<h:inputText value="#{StudentInfoBean.name}" required="true">
<f:validateLength minimum="2" maximum="10"/>
</h:inputText>
<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>

There are two type of validations are used in above code for student name
1. Student name can not be empty.
2. Student name can have of minimum 2 or maximum 2 characters.
If any criterial does fail then you will get error on the top of screen, because we have used <h:mesage ...... /> at top of screen.
One problem is there in above code that every time you will get the any type of error on top your screen, but error should show just infront of field. To do that, we will change the location of <h:messages style="color:darkred"/> and have to use of input id.
<h:inputText id="studentName" value="#{StudentInfoBean.name}" required="true"> <f:validateLength minimum="2" maximum="10"/> </h:inputText>
<font color="#FF0000"><h:message for="studentName"/></font>
I used the id for input component and used same id with message for tag.
We can also put all the component in tabular format for good look.
Again complete code of 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>
<table >
<tr> <td align="center"><h2><h:outputText value="#{message.input_header}"/></h2></td> </tr><td></td><tr> </tr>
<tr> <td><h:outputText value="#{message.prompt_student_name}"/></td>
<td>

<h:inputText id="studentName" value="#{StudentInfoBean.name}" required="true"> <f:validateLength minimum="2" maximum="10"/>
</h:inputText>
<font ><h:message for="studentName"/></font>
</td> </tr><tr> <td>
<h:outputText value="#{message.prompt_roll_number}"/></td> <td> <h:inputText id="roll" value="#{StudentInfoBean.roll}" > <f:validateLongRange minimum="1" maximum="500"/> </h:inputText>
<font color="#FF0000"><h:message for="roll"/></font> </td> </tr><tr> <td><h:commandButton action="result" value="#{message.button_text}" /></td> </tr> </table> </h:form> </body></html></f:view>

Now there are three validations in the above code
1. Name can not be empty
2. Name can have of minimum 2 characters and maximum 10 characters.
3. Roll must be between 1 to 500 includings 1 and 500.
Now we will get error in front of respective input box. Means if error is related to name, it will come in front of name input box.

How to change the default error message
1. Make one packase say (com.suman.message.validation) in src folder and make one properties file (Mymessage.properties) in that package.

Mymessage.properties

javax.faces.component.UIInput.REQUIRED=Please enter a value for this field.

2. Add this code inside faces-config.xml
<application> <message-bundle>com.suman.message.validation.Mymessage</message-bundle> </application>

Restart the server, now you will get the difference in error message ealier it was Validation error : Value is required now it is Please enter a value for this field.


Custom Validation is coming soon ................ :)

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 .................... :)