servlets struts hibernate spring maven java
 

A Simple JavaServer Faces Application



To make a JSF application, we are utilizing NetBeans IDE 8.2. You can also refer to other Java IDEs.

Here, we are making a task after that we will raced to test it's setup settings. In this way, we should make another venture fist.

Step 1: Create a New Project

Go to file menu and select new Project.

Select Category Java Web and Project Web Application.

Enter project name.

Select Server and Java EE Version.

Select JSF Framework

Run: Now, you can run your application by selecting run option after right click on the project. It will produce a default message "Hello from Facelets".

We have created JSF project successfully. This project includes following files:

  1. index.xhtml: inside the Web Pages directory
  2. web.xml: inside the WEB-INF directory

1) Create User Interface

  1. // index.xhtml  
  2. <?xml version='1.0' encoding='UTF-8' ?>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml"  
  5. xmlns:h="http://xmlns.jcp.org/jsf/html">  
  6. <h:head>  
  7. <title>User Form</title>  
  8. </h:head>  
  9. <h:body>  
  10. <h:form>  
  11. <h:outputLabel for="username">User Name</h:outputLabel>  
  12. <h:inputText id="username" value="#{user.name}" required="true" requiredMessage="User Name is required" /><br/>  
  13. <h:commandButton id="submit-button" value="Submit" action="response.xhtml"/>  
  14. </h:form>  
  15. </h:body>  
  16. </html>  

// response.xhtml

  1. <?xml version='1.0' encoding='UTF-8' ?>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml"  
  4. xmlns:h="http://xmlns.jcp.org/jsf/html">  
  5. <h:head>  
  6. <title>Welcome Page</title>  
  7. </h:head>  
  8. <h:body>  
  9. <h2>Hello, <h:outputText value="#{user.name}"></h:outputText></h2>  
  10. </h:body>  
  11. </html>  

2) Create a Managed Bean

// User.java

  1. package managedbean;  
  2. Facelet Title  
  3. import javax.faces.bean.ManagedBean;  
  4. import javax.faces.bean.RequestScoped;  
  5. @ManagedBean  
  6. @RequestScoped  
  7. public class User {  
  8. String name;  
  9.   
  10. public String getName() {  
  11. return name;  
  12. }  
  13. public void setName(String name) {  
  14. this.name = name;  
  15. }  
  16. }  

3) Configure Application

// web.xml


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"   
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee   
  4. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">  
  5. <context-param>  
  6. <param-name>javax.faces.PROJECT_STAGE</param-name>  
  7. <param-value>Development</param-value>  
  8. </context-param>  
  9. <servlet>  
  10. <servlet-name>Faces Servlet</servlet-name>  
  11. <servlet-class>javax.faces.webapFacelet Titlep.FacesServlet</servlet-class>  
  12. <load-on-startup>1</load-on-startup>  
  13. </servlet>  
  14. <servlet-mapping>  
  15. <servlet-name>Faces Servlet</servlet-name>  
  16. <url-pattern>/faces/*</url-pattern>  
  17. </servlet-mapping>  
  18. <session-config>  
  19. <session-timeout>  
  20. 30  
  21. </session-timeout>  
  22. </session-config>  
  23. <welcome-file-list>  
  24. <welcome-file>faces/index.xhtml</welcome-file>  
  25. </welcome-file-list>  
  26. </web-app>  






© Javacodegeeks © 2019 - 2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3