I'm try to generate a commandButton on primefaces 5.1, but I have an error:
Here the xhtml file:
<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>
Here the bean 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);
}
}
Here the 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);
}
}
The error that I receive when I press the button is:
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
If I remove arguments from constructor i doesn't receive this error. But I need to pass an argument to my CustomerActionListener. How can I fix?
Ignoring the strange design approach (feel free to ask a question on how to properly achieve the concrete functional requirement for which you possibly incorrectly thought that this would be the right solution), you can solve it by letting it implement Externalizable
. This is an extension on Serializable
which allows you to take over the responsibility to serialize properties.
Here's a kickoff example:
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();
}
}