执行(Primefaces)菜单项的ActionListener会导致IllegalStateException

Lar*_*erg 6 jsf el javabeans actionlistener primefaces

在JSF支持的bean中,我得到了一个IllegalStateException以编程方式添加的动作监听器,它以编程方式添加了Primefaces菜单项.我尝试了两个requestsession范围,但两者都导致了同样的错误.显然需要 - 根据堆栈跟踪 - 在执行动作侦听器时恢复视图,并且让我的ToolbarBean工具Serializable没有任何不同的效果.为了让这个工作,我应该考虑什么?

用户界面定义

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
    <title>TITLE</title>
</h:head>

<h:body>
    <h:form>
        <p:menu model="#{toolbarBean.model}" type="tiered" />
    </h:form>
</h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

支持菜单的支持豆

@Named
@Scope("request")
public class ToolbarBean implements Serializable {

    private static final long serialVersionUID = -8556751897482662530L;

    public ToolbarBean() {
        model = new DefaultMenuModel();

        MenuItem item;

        // Direct menu item
        item = new MenuItem();
        item.setValue("Menuitem 1");
        item.addActionListener(new ActionListener() {
            @Override
            public void processAction(ActionEvent event)
                    throws AbortProcessingException {
                System.out.println(event.toString());
            }
        });

        model.addMenuItem(item);

        item = new MenuItem();
        item.setValue("Menuitem 2");
        item.addActionListener(new ActionListener() {
            @Override
            public void processAction(ActionEvent event)
                    throws AbortProcessingException {
                System.out.println(event.toString());
            }
        });

        model.addMenuItem(item);
    }

    private MenuModel model;

    public MenuModel getModel() {
        return model;
    }
}
Run Code Online (Sandbox Code Playgroud)

单击其中一个菜单按钮时出现异常

javax.faces.FacesException: java.lang.IllegalStateException: java.lang.InstantiationException: id.co.sofcograha.baseui.ToolbarBean$1
    at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1284)
    at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
    at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1290)
    at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
    at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1290)
    at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
    at javax.faces.component.UIComponent.invokeOnComponent(UIComponent.java:1290)
    at javax.faces.component.UIComponentBase.invokeOnComponent(UIComponentBase.java:673)
    at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStrategyImpl.java:297)
    at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177)
    at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:119)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:438)
    at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:144)
    at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:284)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:182)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:107)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 8

EL(读取:反射)无法访问/构造匿名类.将它们重构为完整的课程.

所以,替换

    item.addActionListener(new ActionListener() {
        @Override
        public void processAction(ActionEvent event)
                throws AbortProcessingException {
            System.out.println(event.toString());
        }
    });
Run Code Online (Sandbox Code Playgroud)

通过

    item.addActionListener(new FooActionListener());
Run Code Online (Sandbox Code Playgroud)

public class FooActionListener implements ActionListener {

    @Override
    public void processAction(ActionEvent event)
            throws AbortProcessingException {
        System.out.println(event.toString());
    }

}
Run Code Online (Sandbox Code Playgroud)

也可以看看: