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

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

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

Web Client<br />

1064<br />

}<br />

public Object action() {<br />

return this;<br />

}<br />

If you are not familiar with enums, see http://java.sun.com/javase/6/docs/technotes/<br />

guides/language/enums.html.<br />

A managed bean is needed to expose the enum to the expression language so that a page can<br />

access its logical outcomes. In this case, the Navigation enum class is accessed through the<br />

NavigationEnumBean:<br />

public class NavigationEnumBean extends EnumManagedBean {<br />

public NavigationEnumBean() {<br />

super(Util.Navigation.class);<br />

}<br />

}<br />

NavigationEnumBean extends a special bean class that includes a method to return an enum<br />

constant, which represents a logical outcome:<br />

public Enum getEnum(String enumName) {<br />

return Enum.valueOf(e, enumName);<br />

}<br />

<strong>The</strong> application also includes a custom EL resolver, EnumResolver, which resolves expressions<br />

that reference an instance of this bean class. You create a resolver if you want expressions to<br />

particular kinds of objects resolved in a special way that is not already supported by the EL<br />

mechanism. See “Resolving Expressions” on page 159 for more information on EL resolvers.<br />

<strong>The</strong> resolver calls the bean’s getEnum method from its getValue method to return the enum<br />

constant:<br />

public Object getValue(ELContext elContext, Object base, Object property) {<br />

if ((base != null && property != null)<br />

&& base instanceof EnumManagedBean) {<br />

elContext.setPropertyResolved(true);<br />

return<br />

((EnumManagedBean)base)<br />

.getEnum(property.toString());<br />

}<br />

return null;<br />

}<br />

A tag’s action attribute references a particular constant of the enum to specify a logical<br />

outcome. <strong>The</strong> following commandLink tag appears on the links.jsp page:<br />

<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

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

Saved successfully!

Ooh no, something went wrong!