Bean
[code]package tutorial;
import java.io.Serializable;
import java.util.Random;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
@SuppressWarnings(“serial”)
public class NumberBean implements Serializable {
protected final static Random rand = new Random();
protected int min;
protected int max;
protected int guess;
protected int actual;
// Default Constructor
public NumberBean() {
this.min = 1;
this.max = 10;
}
// called by JSF to validate user input
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
// coerce the value to an int
try {
int param = Integer.parseInt(value.toString());
// validate param
if (param > this.max || param < this.min) {
FacesMessage msg = new FacesMessage("Guess must be between "+this.min+" and "+this.max);
throw new ValidatorException(msg);
}
} catch (NumberFormatException e) {
FacesMessage msg = new FacesMessage("Must be a number");
throw new ValidatorException(msg);
}
}
// lazy generate our actual value
public synchronized int getActual() {
if (this.actual == 0) {
this.actual = rand.nextInt(this.max - this.min);
this.actual += this.min;
}
return this.actual;
}
// our message for the response
public String getMessage() {
if (this.guess == this.getActual()) {
return "Sweet, you got it right!";
} else if (this.guess < this.getActual()) {
return "Sorry, try something higher";
} else {
return "Too bad, go lower";
}
}
// other bean properties
public int getMin() { return this.min; }
public int getMax() { return this.max; }
public int getGuess() { return this.guess; }
public void setMin(int min) { this.min = min; }
public void setMax(int max) { this.max = max; }
public void setGuess(int guess) { this.guess = guess; }
}[/code]
web.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_2_5.xsd"
id="WebApp_ID" version="2.4">
<display-name>carona-web</display-name>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</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>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
Faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.2//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
<managed-bean>
<managed-bean-name>NumberBean</managed-bean-name>
<managed-bean-class>tutorial.NumberBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>min</property-name>
<value>1</value>
</managed-property>
<managed-property>
<property-name>max</property-name>
<value>10</value>
</managed-property>
</managed-bean>
</faces-config>
template.xhtml
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Facelets: Number Guess Tutorial</title>
<style type="text/css">
<!--
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: small;
}
-->
</style>
</head>
<body>
<h1>
<ui:insert name="title">Default Title</ui:insert>
</h1>
<p>
<ui:insert name="body">Default Body</ui:insert>
</p>
</body>
</html>
guess.xhtml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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">
<body>
<!-- This text above will not be displayed. -->
<ui:composition template="/template.xhtml">
<!-- This text above will not be displayed. -->
<ui:define name="title">
I'm thinking of a number from #{NumberBean.min} to #{NumberBean.max}. Can you guess it?
</ui:define>
<!-- This text above will not be displayed. -->
<ui:define name="body">
<h:form id="helloForm">
<h:inputText type="text" id="userNo" value="#{NumberBean.guess}" validator="#{NumberBean.validate}"/>
<br/>
<h:commandButton type="submit" id="submit" action="success" value="Submit" />
<br/>
<h:message showSummary="true" showDetail="false" style="color: red; font-weight: bold;" id="errors1" for="userNo"/>
</h:form>
</ui:define>
<!-- This text above will not be displayed. -->
</ui:composition>
<!-- This text above will not be displayed. -->
</body>
</html>
response.xhtml
<!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">
<body>
<ui:composition template="/template.xhtml">
<ui:define name="title">
#{NumberBean.message}
</ui:define>
<ui:define name="body">
<form jsfc="h:form">
<input jsfc="h:commandButton" type="submit" id="back" value="Back" action="success"/>
</form>
</ui:define>
</ui:composition>
</body>
</html>
Jars instalados na imagem em anexo
