In this tip we will create a <h:commandLink/> programmatically. The code from the previous tips noted, are used to create the component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | @PostConstruct private void init() { commandLink = createCommandLink(FacesContext.getCurrentInstance(), "#{indexBean.outputGenerator()}" , "CLICK ME... Please" ); commandLink.setValueExpression( "style" , createValueExpression( "#{indexBean.linkStyle}" , String. class )); } public HtmlCommandLink getCommandLink() { return commandLink; } public void setCommandLink(HtmlCommandLink commandLink) { this .commandLink = commandLink; } private HtmlCommandLink createCommandLink(FacesContext context, String methodExpression, String value) { Application application = context.getApplication(); Class<?>[] clazz = new Class<?>[]{}; HtmlCommandLink htmlCommandLink = (HtmlCommandLink) application.createComponent(HtmlCommandLink.COMPONENT_TYPE); htmlCommandLink.setValue(value); htmlCommandLink.setActionExpression(createMethodExpression(methodExpression, String. class , clazz)); return htmlCommandLink; } public String getLinkStyle() { return "color:red; font-size: larger; font-weight: bolder;" ; } public String outputGenerator() { output = String.valueOf( new Random().nextInt( 43 )); return null ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <? xml version = '1.0' encoding = 'UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < h:head > < title >< h:commandLink /> Binding</ title > </ h:head > < h:body > < h:form > < h:outputText value = "#{indexBean.output}" /> < h:commandLink binding = "#{indexBean.commandLink}" /> </ h:form > </ h:body > </ html > |
0 comments :
Post a Comment