This is a start of a new project in JSF, Hibernate and Spring.
For new JSF project, i would like to start from the design of beans. There are two beans here, which are shown as follows:
package com.cj.jsf;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="helloMessageBean")
@RequestScoped
public class helloMessageBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String goback() {
return "index.xhtml";
}
}
This is the bean for the incoming page and it has only one attribute "name" here.
package com.cj.jsf;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="welcomeBean")
@RequestScoped
public class welcomeBean {
public String sayHello() {
return "message.xhtml";
}
}
Following is the design of the relevant xhtml pages -- index.xhtml, message.xhtml. They are as follows:
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JSF2FIRSTTRY</title>
</h:head>
<h:body>
<h:form>
<h:outputText value="Please enter into your name:"></h:outputText>
<br />
<h:inputText id="name" value="#{helloMessageBean.name}" required="true"></h:inputText>
<br />
<br />
<h:commandButton value="submit" action="#{welcomeBean.sayHello}"></h:commandButton>
<br />
<h:message for="name"></h:message>
</h:form>
</h:body>
</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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JSF2FIRSTTRY</title>
</h:head>
<h:body>
<h:form>
<h:outputText value="Come on Boy, #{helloMessageBean.name}!"></h:outputText>
<br />
<h:commandButton value="Go Back" action="#{helloMessageBean.goback}"></h:commandButton>
<h:message for=""></h:message>
</h:form>
</h:body>
</html>
Since the flow control here is done by the action string value and there is no need to configure the faces-config.xml.
The web.xml is as follow:
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>FirstJSF</display-name>
<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>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Now when we type "localhost:8080/JSFTest1", we will get into our web application face.