10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

After setting the components’ rendered properties to true, this method returns the logical<br />

outcome null. This causes the <strong>Java</strong>Server Faces implementation to re-render the<br />

bookcashier.jsp page without creating a new view of the page. If this method were to return<br />

purchase (which is the logical outcome to use to advance to bookcashier.jsp, as defined by<br />

the application configuration resource file), the bookcashier.jsp page would re-render<br />

without retaining the customer’s input. In this case, you want to re-render the page without<br />

clearing the data.<br />

If the user does not purchase more than $100 (or 100 euros) worth of books or the thankYou<br />

component has already been rendered, the method returns receipt.<br />

<strong>The</strong> default NavigationHandler provided by the <strong>Java</strong>Server Faces implementation matches the<br />

logical outcome, as well as the starting page (bookcashier.jsp) against the navigation rules in<br />

the application configuration resource file to determine which page to access next. In this case,<br />

the <strong>Java</strong>Server Faces implementation loads the bookreceipt.jsp page after this method<br />

returns.<br />

public String submit() {<br />

...<br />

if(cart().getTotal() > 100.00 &&<br />

!specialOffer.isRendered())<br />

{<br />

specialOfferText.setRendered(true);<br />

specialOffer.setRendered(true);<br />

return null;<br />

} else if (specialOffer.isRendered() &&<br />

!thankYou.isRendered()){<br />

thankYou.setRendered(true);<br />

return null;<br />

} else {<br />

clear();<br />

return ("receipt");<br />

}<br />

}<br />

Typically, an action method will return a String outcome, as shown in the previous example.<br />

Alternatively, you can define an Enum class that encapsulates all possible outcome strings and<br />

then make an action method return an enum constant, which represents a particular String<br />

outcome defined by the Enum class. In this case, the value returned by a call to the Enum class’s<br />

toString method must match that specified by the from-outcome element in the appropriate<br />

navigation rule configuration defined in the application configuration file.<br />

<strong>The</strong> Duke’s Bank example uses an Enum class to encapsulate all logical outcomes:<br />

public enum Navigation {<br />

main, accountHist, accountList, atm, atmAck, transferFunds,<br />

transferAck, error<br />

}<br />

Writing Backing Bean Methods<br />

Chapter 12 • Developing with <strong>Java</strong>Server FacesTechnology 403

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

Saved successfully!

Ooh no, something went wrong!