Tuesday, May 8, 2012

Java Server Faces(JSF) with Netbeans

Java Server Faces (JSF) is a user interface framework for Java web applications. It is designed to significantly ease the burden of writing and maintaining applications that run on a Java application server and render their UIs back to a target client.

Create a simple Hellworld program :

Create a JSF page:

Create a maven web app in netbeans. (File >New Project > maven >web app >Helloworld ).
Then right click the project, go to properties >frameworks >add.
Select Java Server Faces.

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
        <title>Hello World</title>
    </h:head>
    <h:body>
     <h3>Hello World Example/h3>
     <h:form>
        <h:inputText value="#{helloBean.name}"></h:inputText>
        <h:commandButton value="Welcome Me" action="welcome"></h:commandButton>
     </h:form>
    </h:body>
</html>


JSF 2.0 dependencies:

<dependencies> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.0.0-b13</version> <scope>compile</scope> </dependency>
<dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.0.0-b13</version> <scope>compile</scope> </dependency>
<dependencies>

You must add these dependencies into pom.xml

Create Managed Bean:
Managed bean can be accessed from a JSF page.
Use @ManagedBean annotation to indicate this is a managed bean.


Righi click the project, >New > JSF Managed Bean > helloBean

helloBean.java

package org.yit.yschool.bean;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;
 
@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 private String name;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}


welcome.xhtml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"    
      xmlns:h="http://java.sun.com/jsf/html">
 
    <h:head>
     <title>Hello World</title>
    </h:head>
    <h:body>
     <h3>Hello World Example</h3>
     <h4>Welcome #{helloBean.name}</h4>
    </h:body>
</html>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
   <web-app>
<display-name>Archetype Created Web Application</display-name> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> </web-app>


No comments:

Post a Comment