سؤال

Hi i need to dyamically set h:commandLink action as a string value from bean side. Here explains my problem with code 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>

Here what i need is i need to dynamically change commandlink action value with respect to bean string value (here it was 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. The JEE7 tutorial states:

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.

نصائح أخرى

Maybe you can try something like Command Pattern. This is only an idea, I did not tested it.

In the 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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top