Java Reference
In-Depth Information
The
getResponse
method converts
guessString
from the user input to an integer
(line 28) and determines if the guess is too low (line 30), too high (line 34), and just right (line 32).
L
ISTING
33.10
GuessNumber.xhtml
1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "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>Guess a number</title>
8 </h:head>
9 <h:body>
10 <h:form>
11 <h:outputLabel value="Enter you guess: "/>
12 <h:inputText style="text-align: right; width: 50px"
13 id="guessInputText"
14 value="#{guessNumber.guessString}"/>
15 <h:commandButton style="margin-left: 60px" value="Guess" />
16 <br />
17 <h:outputText style="color: red"
18
bind text input
value="#{guessNumber.response}" />
bind text output
19 </h:form>
20 </h:body>
21 </html>
The bean property
guessString
is bound to the text input (line 14). The CSS style
text-align: right
(line 13) specifies that the text is right-aligned in the input box.
The CSS style
margin-left: 60px
(line 15) specifies that the command button has a
left margin of 60 pixels.
The bean property
response
is bound to the text output (line 18). The CSS style
color:
red
(line 17) specifies that the text is displayed in red in the output box.
The project uses the
view
scope. What happens if the scope is changed to the request
scope? Every time the page is refreshed, JSF creates a new bean with a new random number.
What happens if the scope is changed to the
session
scope? The bean will be alive as long as
the browser is alive. What happens if the scope is changed to the
application
scope? The bean
will be created once when the application is launched from the server. So every client will use
the same random number.
scope
33.13
✓
✓
What is a scope? What are the available scopes in JSF? Explain request scope, view
scope, session scope, and application scope. How do you set a request scope, view
scope, session scope, and application scope in a managed bean?
Check
Point
33.14
What happens if the bean scope in Listing 33.9 GuessNumberJSFBean.java is
changed to request?
33.15
What happens if the bean scope in Listing 33.9 GuessNumberJSFBean.java is
changed to session?
33.16
What happens if the bean scope in Listing 33.9 GuessNumberJSFBean.java is
changed to application?
JSF provides tools for validating user input.
Key
Point
In the preceding
GuessNumber
page, an error would occur if you entered a noninteger in the
input box before clicking the
Guess
button. One way to fix the problem is to check the text
field before processing any event. But a better way is to use the validators. You can use the
Search WWH ::
Custom Search