java.lang.InstantiationException: my.package.CustomActionListener 使用非默认构造函数
java.lang.InstantiationException: my.package.CustomActionListener with non-default constructor
我尝试在 primefaces 5.1 上生成命令按钮,但出现错误:
这里是 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>
这里是豆子class:
@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。
我该如何解决?
您需要在 CustomActionListener 中有一个默认构造函数(自定义构造函数除外)。
忽略奇怪的设计方法(请随意提出有关如何正确实现您 possibly incorrectly thought that this would be the right solution), you can solve it by letting it implement 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();
}
}
我尝试在 primefaces 5.1 上生成命令按钮,但出现错误:
这里是 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>
这里是豆子class:
@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。 我该如何解决?
您需要在 CustomActionListener 中有一个默认构造函数(自定义构造函数除外)。
忽略奇怪的设计方法(请随意提出有关如何正确实现您 possibly incorrectly thought that this would be the right solution), you can solve it by letting it implement 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();
}
}