简体   繁体   English

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

[英]JSF2:How to bind string value to h:commandLink action

Hi i need to dyamically set h:commandLink action as a string value from bean side. 嗨,我需要动态地将h:commandLink操作设置为来自bean端的字符串值。 Here explains my problem with code MenuObject.java 这里说明代码CodeObject.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 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 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>

Here what i need is i need to dynamically change commandlink action value with respect to bean string value (here it was menuAction). 在这里我需要的是我需要相对于bean字符串值动态更改Commandlink动作值(这里是menuAction)。 But in this situation i got following exception 但是在这种情况下,我得到以下例外


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)

You are trying to use the EL to return a value expression to be used as a method expression in a single expression. 您正在尝试使用EL返回一个值表达式,以将其用作单个表达式中的方法表达式。 The JEE7 tutorial states: 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.

You can achive this behaviour using javascript or use a library that offers you a dynamic menu component, like primefaces . 您可以使用javascript来实现此行为,也可以使用为您提供动态菜单组件(例如primefaces)的库

Maybe you can try something like Command Pattern . 也许您可以尝试使用Command Pattern之类的方法 This is only an idea, I did not tested it. 这只是一个想法,我没有测试过。

In the xhtml: 在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 pattern: 命令模式:

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

The menu item: 菜单项:

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

The invoker: 调用者:

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

    public String callAction(){
        return action.execute();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM