Hello World Jsf Example steps
Requirements
- Eclipse IDE for Java EE Developers (Download).
- Apache Tomcat 7 (Download).
- JSF Library file(2.0.10 binary) (javax.faces-2.0.10.jar).
Step 1: Create a Dynamic web project in eclipse,
Step 2: Add the above downloaded jar file,
Right click on the project -> Build path -> Configure build path -> Libraries (tab) -> Add External Jars (browse and add the downloaded jsf jar file).
[plain gutter=”false”]Note:Sometime you may need to paste the library file(which we downloaded -jsf jar file) to your lib folder(WebContent/WEB-INF/lib) manually.[/plain]
Step 3: Edit the web.xml file,
[xml]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>HelloWorldJsf</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<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>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
[/xml]
Step 4: Create a Bean class (HelloWorld) and paste the below code,
[java]
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "hw")
@SessionScoped
public class HelloWorld {
private String helloWorld = "JSF HelloWorld by Javadomain.in";
public String getHelloWorld() {
return helloWorld;
}
}
[/java]
Step 5: Create a index.xhtml file and paste the below code,
[html]
<?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:body>
<h:form>
<h:outputText value="#{hw.helloWorld}"></h:outputText>
</h:form>
</h:body>
</html>
[/html]
Step 6: Run the index.xhtml (right click on this fle -> Run as -> Run On server).
[plain gutter=”false”] Note:Ensure you have created the server already [/plain]
.
Output:
Download the Source code:
Recommended Jsf Books: