http://swexplorations.blogspot.com/2008/09/using-constants-in-facelets.html
Says
The idea here is to use a backing bean implementing 'Map' interface and implementing its 'get' method to return appropriate value for the given constant. This is achieved by using introspection.
Here is the snippet of xhtml that uses constants defined in backing beans, use of constants is highlighted using red color:
<rich:tabPanel switchType="ajax" id="tabPanel1"
selectedTab="#{bean.selectedTab}">
<rich:tab label="First" rendered="true" name="first">
<h:outputText value="First Test component" />
</rich:tab>
<rich:tab label="Second" rendered="true" name="second">
<h:outputText value="Second Test component" />
</rich:tab>
</rich:tabPanel>
<a4j:commandLink reRender="tabPanel1" action="#{bean.changeSelectedTab}"
value="first">
<f:setPropertyActionListener
value="#{StaticAccessor['demo.Bean.FIRST_TAB']}"
target="#{bean.selectedTabInt}" />
</a4j:commandLink>
<rich:spacer width="10"></rich:spacer>
<a4j:commandLink reRender="tabPanel1" action="#{bean.changeSelectedTab}"
value="second">
<f:setPropertyActionListener
value="#{StaticAccessor['demo.Bean.SECOND_TAB']}"
target="#{bean.selectedTabInt}" />
</a4j:commandLink>
Here is the backing bean implementation that provides map interface (only get implementation is shown here):
public class StaticAccessor implements Map{
public Object get(Object arg0) {
if ((arg0 != null) && (arg0 instanceof String)) {
String input = (String) arg0;
int index = input.lastIndexOf('.');
if (index == -1) {
return null;
}
String className = input.substring(0, index);
String constant = input.substring(index + 1);
try {
Class forName = Class.forName(className);
Field declaredField = forName.getDeclaredField(constant);
int value = declaredField.getInt(forName);
return value;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
Posted by Atul Kshirsagar
Scriptless JSP Pages: The Constant Constants Consternation
http://www.javaranch.com/journal/200601/ccc.html
Advanced Facelets programming
http://www.ibm.com/developerworks/web/library/j-facelets2.html
No comments:
Post a Comment