如何在ace:menuButton组件中绑定ace:menuItem

时间:2014-06-02 12:16:26

标签: java jsf xhtml icefaces-3

在我的Icefaces 3应用程序中,我有一个下拉菜单。我想动态地填充它。在我的ManagedBean中,我有一个定义menuItem的方法。它得到标签和actionMethod,并在MenuItem上对它们进行评估。当我启动我的应用程序时,下拉菜单项始终为空。

ManagedBean:

package com.omb.view;

import java.io.Serializable;

import javax.el.MethodExpression;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icesoft.faces.component.menubar.MenuItem;

@Controller
@Scope("session")
public class MyBean implements Serializable {

    private static final Log logger = LogFactory.getLog(MyBean.class);

    private MenuItem menuItem1;

    public String initMyBean() {

        try {
            initMenuItem();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private void initMenuItem() {

        menuItem1 = new MenuItem();
        menuItem1.setValue("Menu 1");

        MethodExpression actionExpression = FacesUtils.createAction("#{menu1Bean.display}", String.class);
        menuItem1.setActionExpression(actionExpression);

    }

    public MenuItem getMenuItem1() {
        return this.menuItem1;
    }

    public void setMenuItem1(MenuItem menuItem1) {
        this.menuItem1 = menuItem1;
    }

}

FaceUtils

package com.omb.view;

import javax.el.MethodExpression;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.MethodExpressionActionListener;

/**
 * JSF utilities.
 */
public class FacesUtils {

    public static MethodExpression createAction(String actionExpression, Class<?> returnType) {
        FacesContext context = FacesContext.getCurrentInstance();
        return context.getApplication().getExpressionFactory()
                .createMethodExpression(context.getELContext(), actionExpression, returnType, new Class[0]);
    }

    public static MethodExpressionActionListener createActionListener(String actionListenerExpression) {
        FacesContext context = FacesContext.getCurrentInstance();
        return new MethodExpressionActionListener(context
                .getApplication()
                .getExpressionFactory()
                .createMethodExpression(context.getELContext(), actionListenerExpression, null,
                        new Class[] {ActionEvent.class}));
    }
}

screen.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<body>
    <ui:composition>
        <ice:form id="headerForm" xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:ice="http://www.icesoft.com/icefaces/component"
            xmlns:ace="http://www.icefaces.org/icefaces/components"
            xmlns:c="http://java.sun.com/jstl/core"
            xmlns:ui="http://java.sun.com/jsf/facelets">

            <div class="menu">
                <ace:menuButton id="menuButton" effect="slide" effectDuration="200" value="Menu Button">
                    <ace:menuItem binding="#{myBean.menuItem1}"/>
                </ace:menuButton>

            </div>
        </ice:form>
    </ui:composition>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我最终使用标准解决方案:

<ace:menuButton id="menuButton" effect="slide" effectDuration="200" value="Menu Button">
       <ace:menuItem value="Menu 1" action="#{myBean.display}"/>
</ace:menuButton>