Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

jsf - Executing the ActionListener of a (Primefaces) menu item leads to an IllegalStateException

In JSF backed bean I got an IllegalStateException when the programmatically added action listener of a programmatically added Primefaces menu item is called. I tried both request and session scope but both are leading to the same error. Obviously there's need -- according to the stack trace -- to restore the view when an action listener is executed and I let my ToolbarBean implement Serializable with no different effect. What should I consider in order to get this to work?

User interface definition

<?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>

Backed bean providing the menu

@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;
    }
}

Exception when clicking one of the menu buttons

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EL (read: reflection) cannot access/construct anonymous classes. Refactor them into fullworthy classes.

So, replace

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

by

    item.addActionListener(new FooActionListener());

and

public class FooActionListener implements ActionListener {

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

}

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...