嗨,我需要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)
我在這裏看到的'menuAction'是一個帶有getter/setter的字段。你不能在'action'屬性中使用它 – kolossus
看看:http://stackoverflow.com/questions/12159777/dynamic-choice-of-bean-in-el – andremonteiro