This is a simple hello world example for jsf beginners.I a going to use NetBeans 7.4.
Java Server Faces (JSF) 2.0 is an MVC java based web framework which simplifies building user interfaces for Java application.It has 100+ ready UI tags.
Just Create a simple Web application in Netbeans selecting JSF framework.
This type of folder structure will appear.We will be using the highlighted files.Just ignore other files.
Firstly Just create a simple index.xhtml which will have a text box and a submit button.
Here as you see,We have used a value tag which assigns a value entered into the
name field of HelloBean class by calling setter method.
On click on submit button,we have given action as "welcome".
welcome.xhtml is our another page where we are redirecting user on click of that button.
The code snippet of HelloBean and welcome.xhtml is as follows-
In HelloBean.java,we have used two annotations @ManagedBean and @SessionScoped.
ManagedBean is a simply a java bean managed by jsf framework.It contains getter setter methods.It may contain business logic or even a backing bean.(what is backing bean,will explore in later part)
SessionScoped means that bean will live as long as the http session lives.It will be created upon the first request and will remain till the session is present.
In welcome.xhtml,we are simply accessing the name field value from the HelloBean.
When you deploy this simple application,if nothing goes wrong ,you will see output like this.
Here on click on submit,we go to welcome.xhtml and sees the output.If you notice,in url still it is showing index.html.Now we will fix this.
JSF by default uses page forward while navigating to another change,so url does not change.
If you want to change the url also,just write action="welcome?faces-redirect=true" in place of action="welcome".This will serve the purpose.
In case of any doubt,write comments.
Java Server Faces (JSF) 2.0 is an MVC java based web framework which simplifies building user interfaces for Java application.It has 100+ ready UI tags.
Just Create a simple Web application in Netbeans selecting JSF framework.
This type of folder structure will appear.We will be using the highlighted files.Just ignore other files.
Firstly Just create a simple index.xhtml which will have a text box and a submit button.
Here as you see,We have used a value tag which assigns a value entered into the
name field of HelloBean class by calling setter method.
On click on submit button,we have given action as "welcome".
welcome.xhtml is our another page where we are redirecting user on click of that button.
The code snippet of HelloBean and welcome.xhtml is as follows-
In HelloBean.java,we have used two annotations @ManagedBean and @SessionScoped.
ManagedBean is a simply a java bean managed by jsf framework.It contains getter setter methods.It may contain business logic or even a backing bean.(what is backing bean,will explore in later part)
SessionScoped means that bean will live as long as the http session lives.It will be created upon the first request and will remain till the session is present.
In welcome.xhtml,we are simply accessing the name field value from the HelloBean.
When you deploy this simple application,if nothing goes wrong ,you will see output like this.
Here on click on submit,we go to welcome.xhtml and sees the output.If you notice,in url still it is showing index.html.Now we will fix this.
JSF by default uses page forward while navigating to another change,so url does not change.
If you want to change the url also,just write action="welcome?faces-redirect=true" in place of action="welcome".This will serve the purpose.
In case of any doubt,write comments.