JSF2:如何将字符串值绑定到h:commandLink操作

时间:2013-10-14 17:16:58

标签: jsf jsf-2 action commandlink

您好我需要将h:commandLink操作动态设置为bean端的字符串值。这里解释了我的代码问题 MenuObject.java


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>

这里我需要的是我需要动态地改变关于bean字符串值的commandlink动作值(这里是menuAction)。但在这种情况下,我得到了以下异常


javax.el.MethodNotFoundException: /WEB-INF/layouts/main.xhtml @137,85 action="#{menuCreator.menu}": Method not found: com.util.MenuObject@30c96021.menuAction()
    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)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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();
    }
}