我在使用复合组件时遇到问题。当组件需要重新呈现时(因为我有基于用户操作显示/隐藏的面板)我得到了以下错误。 (他希望通过使用默认构造函数创建一个新的实例来恢复一个类,但他无法找到/执行该构造函数,因为它不存在?但我不明白为什么他我想这样做以及它失败的原因......):
我尝试使用 wlp (websphere liberty profile)运行完全相同的代码,然后工作作为魅力,所以我怀疑在下载时遇到问题!
=>当我看到implementation of that class时,我找不到类中的默认构造函数(PartImpl代码)所以它的逻辑是他无法创建它,但他为什么要这样做恢复/创建该服务器特定类的实例??
** java.lang.InstantiationException:io.undertow.servlet.spec.PartImpl io.undertow.servlet.spec.PartImpl。()**
这个类是Wildfly类(我想是Wildfly使用的)
调试时我发现了这段代码:
结果:null(抛出以下异常)
try {
toRestoreClass = loadClass(className, this);
}
catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
if (null != toRestoreClass) {
try {
result = toRestoreClass.newInstance();
}
catch (InstantiationException e) {
throw new IllegalStateException(e);
}
这是错误:
ERROR javax.enterprise.resource.webcontainer.jsf.context - java.lang.IllegalStateException: java.lang.InstantiationException: io.undertow.servlet.spec.PartImpl
at javax.faces.component.StateHolderSaver.restore(StateHolderSaver.java:153)
at javax.faces.component.ComponentStateHelper.restoreState(ComponentStateHelper.java:303)
at javax.faces.component.UIComponentBase.restoreState(UIComponentBase.java:1629)
at javax.faces.component.UIOutput.restoreState(UIOutput.java:286)
at javax.faces.component.UIInput.restoreState(UIInput.java:1420)
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy$2.visit(FaceletPartialStateManagementStrategy.java:379)
at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:151)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1689)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIForm.visitTree(UIForm.java:371)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700)
at com.sun.faces.application.view.FaceletPartialStateManagementStrategy.restoreView(FaceletPartialStateManagementStrategy.java:366)
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:591)
at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:151)
at org.ocpsoft.rewrite.faces.RewriteViewHandler.restoreView(RewriteViewHandler.java:102)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at org.omnifaces.viewhandler.OmniViewHandler.restoreView(OmniViewHandler.java:104)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:353)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:123)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:226)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at be.raes.web.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:22)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at be.raes.web.filter.SecurityFilter.doFilter(SecurityFilter.java:129)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at be.raes.web.filter.LanguageResolverFilter.doFilter(LanguageResolverFilter.java:49)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.jrHandle(ServletInitialHandler.java)
at sun.reflect.GeneratedMethodAccessor245.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
一些重要的最后部分:
Caused by: java.lang.InstantiationException: io.undertow.servlet.spec.PartImpl
at java.lang.Class.newInstance(Class.java:427)
at javax.faces.component.StateHolderSaver.restore(StateHolderSaver.java:150)
... 86 more
Caused by: java.lang.NoSuchMethodException: io.undertow.servlet.spec.PartImpl.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 87 more
这是我的组件代码:
<cc:interface componentType="fileComponent">
<cc:attribute name="label" type="java.lang.String"/>
<cc:attribute name="value" type="java.lang.Object"/>
<cc:attribute name="action" method-signature="void listener()"/></cc:interface>
<cc:implementation>
<uc:component id="#{cc.clientId}"
composite="true">
<uc:fileUpload id="fileUpload"
label="#{cc.attrs.label}"
value="#{cc.file}"
action="#{cc.uploadFile}"
render="#{cc.clientId}"
displayed="#{empty cc.fileContainer}"
accept="image/png,
image/jpeg"/>
<uc:component id="fileContainer"
label="#{cc.attrs.label}"
displayed="#{not empty cc.fileContainer}">
<h:commandLink ajax="true">
<p:fileDownload value="#{cc.downloadFileContainer}"/>
</h:commandLink>
<o:commandScript name="action"
actionListener="#{cc.attrs.action}"
render=":#{cc.clientId}"
execute=":#{cc.clientId}"/>
</uc:component>
</uc:component>
组件类:
@FacesComponent("fileComponent")
public class FileComponent extends UIInput implements NamingContainer {
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
@Override
public Object getSubmittedValue() {
return getFileContainer();
}
public FileContainer createFileContainer() {
return new FileContainer();
}
public void uploadFile() throws IOException {
FileContainer fileContainer = createFileContainer();
fileContainer.setFileName(FilenameUtils.getName(getFile().getSubmittedFileName()));
fileContainer.setContent(Utils.toByteArray(getFile().getInputStream()));
setFileContainer(fileContainer);
}
public void downloadFileContainer() throws IOException {
Faces.sendFile(getFileContainer().getContent(), getFileContainer().getFileName(), true);
}
public Part getFile() {
return (Part) getStateHelper().eval("file");
}
public void setFile(Part file) {
getStateHelper().put("file", file);
}
public FileContainer getFileContainer() {
return (FileContainer) getStateHelper().eval("filecontainer", getValue());
}
public void setFileContainer(FileContainer fileContainer) {
getStateHelper().put("filecontainer", fileContainer);
}
}
JSF文件(组件的使用):
... <mc:fileComponent value="#{bean.fileContainer}" /> ...
支持bean
@ViewScoped
@Named("bean")
public class Bean implements Serializable {
private FileContainer fileContainer;
//getters & setters etc
}
components.taglib.xml:
...<tag>
<tag-name>fileComponent</tag-name>
<component>
<resource-id>components/fileComponent.xhtml</resource-id>
</component>
<attribute>
<name>id</name>
</attribute>
<attribute>
<name>label</name>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
</attribute>
<attribute>
<name>action</name>
</attribute>
</tag>...
感谢您的帮助