2013-10-14 46 views
0

嗨,我需要dyamically將h:commandLink動作設置爲來自bean側的字符串值。這裏解釋我的代碼問題 MenuObject.javaJSF2:如何將字符串值綁定到h:commandLink動作

 

public class MenuObject { 
    private String menuName; 
    private String menuAction; 
    public String getMenuName() { 
     return menuName; 
    } 
    public void setMenuName(String menuName) { 
     this.menuName = menuName; 
    } 
    public String getMenuAction() { 
     return menuAction; 
    } 
    public void setMenuAction(String menuAction) { 
     this.menuAction = menuAction; 
    } 



} 

 

MenuCreator.java

 

public class MenuCreator { 
    public List getMenu(){ 
     List menuList = new ArrayList(); 
     MenuObject menu1 = new MenuObject(); 
     menu1.setMenuAction("accountController.beginSearch()"); 
     menu1.setMenuName("Account"); 
     menuList.add(menu1); 
     MenuObject menu2 = new MenuObject(); 
     menu2.setMenuAction("companyController.beginSearch()"); 
     menu2.setMenuName("Company"); 
     menuList.add(menu1); 
     return menuList; 
    } 
 

main.xhtml

 

<ui:repeat value="#{menuCreator.menu}" var="subMenu"> 
    <li class="glyphicons cogwheels"><h:commandLink action="#{subMenu.menuAction}"><i></i><span><h:outputText value="#{subMenu.menuName}"/></span></h:commandLink></li> 
    </ui:repeat> 
 

這裏我需要的是我需要動態改變commandlink動作值關於bean的字符串值(這裏是menuAction)。但在這種情況下,我得到了下面的異常

 

javax.el.MethodNotFoundException: /WEB-INF/layouts/main.xhtml @137,85 action="#{menuCreator.menu}": Method not found: [email protected]() 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:109) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101) 
    at net.bull.javamelody.JsfActionListener.processAction(JsfActionListener.java:65) 
 
+0

我在這裏看到的'menuAction'是一個帶有getter/setter的字段。你不能在'action'屬性中使用它 – kolossus

+1

看看:http://stackoverflow.com/questions/12159777/dynamic-choice-of-bean-in-el – andremonteiro

回答

0

您正在嘗試使用EL返回用作在表達的方法表達一個值表達式。該JEE7教程狀態:

 
9.3 Value and Method Expressions 
The EL defines two kinds of expressions: value expressions and method expressions. 
Value expressions can either yield a value or set a value. Method expressions reference 
methods that can be invoked and can return a value. 

您可以使用JavaScript實現這一目標的行爲或使用您提供了一個動態菜單組件,如primefaces庫。

0

也許你可以試試類似Command Pattern。這只是一個想法,我沒有對它進行測試。

在XHTML:

<ui:repeat value="#{menuCreator.menu}" var="subMenu"> 
    <li class="glyphicons cogwheels"> 
     <h:commandLink action="#{invoker.callAction}" value="#{subMenu.menuName}"> 
      <f:setPropertyActionListener target="#{invoker.action}" value="#{subMenu.action}" /> 
     </h:commandLink> 
    </li> 
</ui:repeat> 

命令模式:

/* The Command interface */ 
public interface Command { 
    String execute(); 
} 

菜單項:

public class MenuObject { 
    private String menuName; 
    private Command action; 
    // Getters and setters... 
} 

調用者:

@Named("invoker") 
public class Invoker { 
    private Command action; 

    public String callAction(){ 
     return action.execute(); 
    } 
} 
相關問題