JSF :: Hello World Application :: Tutorial :: Example : Tech Guru India JSF :: Hello World Application :: Tutorial :: Example :: Tech Guru India

JSF :: Hello World Application :: Tutorial :: Example




So You want to create a simple Hello World Application in JSF, huh? I will show you some basic steps that will create an Application.

Lets start with requirements! You need to have NetBeans 6.0 or higher Version of IDE installed in your machine.

We will make application that will simply accept a name and on button click displays it on same page.

Now i suppose you already have it, lets move further.
1. Go to file menu-> New Project-> Java Web -> Enter Your Project name, lets say "HelloWorld2", press next
2 Now select a server, It can be Tomcat or it can be GlassFish (come with NetBeans), press next
3. Now in Next step you will be asked for FrameWork. select "Java Server Faces" as framework and press "finish".

This will create very basic application. You can run it by pressing F6.



Explained:

"taglib" will be used to load library. In this application we are using base and html library.
"uri" will specify the path for the library.
"prefix" will specify the prefix that will be used to distinguish between html tags and base tags.
"f:loadBundle" is used to load the property file. (Which we are going to use in next section)
"h:form" is just like that html form tag. Whenever the submit button is pressed all the data with in <form></form> will be sent.
"h:outputText" is used simply to display data on the page.
"h:inputText" will display simple textbox on screen. "h:commandButton" will display a button on the screen.
"#{msgs.greet}" now, this is used to take value from the messages.properties file. msgs is the name we have given to messages.properties file. 'greet' is one variable stored in this file. The structure of file 'messages.properties' will make it clear.



Data is store as param=value which is separate by new line. Now this file should be stored in C:\Users\maxxx\HelloJSF2\web\WEB-INF\classes path.
Here we need to create "classes" directory and then place 'messages.properties' file in it.

Use of Bean


Bean is the component in java which can be used many times. In other words its a 'reusable' component in java application.
We will create a bean class to getname from the user and display it back on same page.
create 'mybean.java' file in "classes" directory.

Bean class must have setter and getter methods implemented in it!
Now as we have created bean class, we need to compile it and store 'mybean.class' file in "classes" directory.


"#{GetNameBean.name}" that we have used in over file will use bean to store the name. and Similarly the same is used in next line to print the stored name.
Now we need to register this bean class in "faces-config.xml" file


"managed-bean" is used to register managed bean.
"managed-bean-class" is the name of the class we have created.
"managed-bean-scope" specifies the scope of the bean class.

So all the necessary thing needed to run simple application are done now!
Any Query can be mentioned as comment!


We will send you more information, Enter your email address:


Related Posts with Thumbnails

Share/Save/Bookmark

0 comments:

Post a Comment