19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The getResponse method converts guessString from the user input <strong>to</strong> an<br />

integer (line 28) and determines if the guess is <strong>to</strong>o low (line 30), <strong>to</strong>o<br />

high (line 34), and just right (line 32).<br />

Listing 44.10 GuessNumber.xhtml<br />

<br />

<br />

<br />

<br />

<br />

<br />

Guess a number<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

The bean property guessString is bound <strong>to</strong> the text input (line 14). The<br />

CSS style text-align: right (line 13) specifies that the text is rightaligned<br />

in the input box.<br />

The CSS style margin-left: 60px (line 15) specifies that the <strong>com</strong>mand<br />

but<strong>to</strong>n has a left margin of 60 pixels.<br />

The bean property response is bound <strong>to</strong> the text output (line 18). The<br />

CSS style color: red (line 17) specifies that the text is displayed in<br />

red in the output box.<br />

<br />

The project uses the view scope. What happens if the scope is changed <strong>to</strong><br />

the request scope? Every time the page is refreshed, JSF creates a new<br />

bean with a new random number. What happens if the scope is changed <strong>to</strong><br />

the session scope? The bean will be alive as long as the browser is<br />

alive. What happens if the scope is changed <strong>to</strong> the application scope?<br />

The bean will be created once when the application is launched from the<br />

server.<br />

44.7 Validating Input<br />

In the preceding GuessNumber page, an error would occur if you entered a<br />

non-integer in the input box before clicking the Guess but<strong>to</strong>n. A simple<br />

way <strong>to</strong> fix the problem is <strong>to</strong> check the text field before processing any<br />

event. JSF provides several convenient and powerful ways for input<br />

validation. You can use the standard valida<strong>to</strong>r tags in the JSF Core Tag<br />

Library or create cus<strong>to</strong>m valida<strong>to</strong>rs. Table 44.2 lists some JSF input<br />

valida<strong>to</strong>r tags.<br />

25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!