java.lang.InstantiationException:my.package.CustomActionListener,带有非默认构造函数

时间:2015-01-28 16:31:01

标签: jsf serialization actionlistener commandbutton

我尝试在primefaces 5.1上生成一个commandButton,但是我有一个错误:

这里是xhtml文件:     

<h:head>
</h:head>

<h:body styleClass="appDefaultBg">

    <h:form>
        <p:remoteCommand autoRun="true" action="#{bean.pageRender()}" update=":pageElement" />
    </h:form>

    <h:panelGroup id="pageElement">
    </h:panelGroup>

</h:body>

这里是bean类:

@ManagedBean(name = "bean")
@SessionScoped
public class Bean implements Serializable {

private static final String PAGE_ELEMENT = "pageElement";

private UIComponent page;

@PostConstruct
public void init() {
    findComponent();
}

public void findComponent() {
    FacesContext.getCurrentInstance().getViewRoot().invokeOnComponent(FacesContext.getCurrentInstance(), PAGE_ELEMENT, new ContextCallback() {
        @Override
        public void invokeContextCallback(FacesContext context, UIComponent component) {
            page = component;
        }
    });
}

public void pageRender() {
    HtmlForm form = new HtmlForm();

    Panel panel = new Panel();
    panel.setId("filter");
    panel.setHeader("Header");

    CommandButton customButton = new CommandButton();
    customButton.setValue("Click me!");
    customButton.addActionListener(new CustomActionListener(5));

    panel.getChildren().add(customButton);
    form.getChildren().add(panel);
    page.getChildren().add(form);

}
}

这里是CustomActionListener:

public class CustomActionListener implements ActionListener {

    private int u;

    public CustomActionListener(int u) {
        this.u = u;
    }

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

按下按钮时收到的错误是:

GRAVE: java.lang.InstantiationException: my.package.CustomActionListener
java.lang.IllegalStateException: java.lang.InstantiationException: my.package.CustomActionListener
    at javax.faces.component.StateHolderSaver.restore(StateHolderSaver.java:153)
    at javax.faces.component.AttachedObjectListHolder.restoreState(AttachedObjectListHolder.java:158)
    at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1611)
    at com.sun.faces.application.view.StateHolderSaver.restore(StateHolderSaver.java:165)
    at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.restoreDynamicAdd(FaceletPartialStateManagementStrategy.java:274)
    at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.restoreDynamicActions(FaceletPartialStateManagementStrategy.java:227)
    at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.restoreView(FaceletPartialStateManagementStrategy.java:395)
    at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:138)
    at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java:123)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlingStrategy.java:585)
    at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:150)
    at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:197)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:121)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.synapsis.catalina.realm.filter.AlertChangePWDBefore.doFilter(AlertChangePWDBefore.java:106)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.synapsis.catalina.realm.filter.SingleLogOnFilter.doFilter(SingleLogOnFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.synapsis.catalina.realm.filter.DefaultUserFilter.doFilter(DefaultUserFilter.java:118)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at com.synapsis.sio.ward.usercontext.controller.filter.UserContextFilter.doFilter(UserContextFilter.java:168)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:610)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:339)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException: my.package.CustomActionListener
    at java.lang.Class.newInstance(Class.java:359)
    at javax.faces.component.StateHolderSaver.restore(StateHolderSaver.java:150)
    ... 51 more

如果我从构造函数中删除参数,我没有收到此错误。但是我需要将一个参数传递给我的CustomerActionListener。 我该如何解决?

2 个答案:

答案 0 :(得分:0)

您需要在CustomActionListener中使用默认构造函数(除了自定义构造函数)。

答案 1 :(得分:0)

忽略奇怪的设计方法(随意问一个关于如何正确实现您possibly incorrectly认为这是正确解决方案的具体功能要求的问题),您可以通过让它实现来解决它Externalizable。这是Serializable的扩展,允许您接管序列化属性的责任。

这是一个启动示例:

public class CustomActionListener implements ActionListener, Externalizable {

    private int u;

    public CustomActionListener() {
        // Keep default c'tor alive for serialization.
    }

    public CustomActionListener(int u) {
        this.u = u;
    }

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

    @Override
    public void writeExternal(ObjectOutput output) throws IOException {
        output.writeInt(u);
    }

    @Override
    public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException {
        u = input.readInt();
    }

}