javax.validation.ConstraintViolationException - jsf

I am using JSF+JPA iam not fix this error:
javax.validation.ConstraintViolationException: Bean Validation
constraint(s) violated while executing Automatic Bean Validation on
callback event:'prePersist'. Please refer to embedded
ConstraintViolations for details.
#ManagedBean(name = "clientCon")
public class ClientController {
#PostConstruct
public void init() {
System.out.println("Salam");
clients = new Clients();
}
private EntityManagerFactory emf = null;
public ClientController() {
emf = Persistence.createEntityManagerFactory("ClinicProjectPU");
}
private List<Clients> clientList;
private Clients clients;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public Clients getClients() {
return clients;
}
public void setClients(Clients clients) {
this.clients = clients;
}
public List<Clients> getClientList() {
this.clientList = getEntityManager().createNamedQuery("Clients.findAll").getResultList();
return clientList;
}
public void createClient() {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(clients); // the only interesting method
em.flush();
em.getTransaction().commit();
System.out.println("created");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (em != null) {
em.close();
}
}
}
}
<h:form>
<fieldset>
<h:panelGrid columns="2">
<h:outputText value="Ad" style="color: #0099cc"/>
<h:inputText class="form-client" value="#{clientCon.clients.name}" />
<h:outputText value="Soyad" style="color: #0099cc" />
<h:inputText class="form-client" value="#{clientCon.clients.surname}" />
<h:outputText value="Telefon" style="color: #0099cc" />
<h:inputText class="form-client" value="#{clientCon.clients.phone}" />
</h:panelGrid>
<div class="btns">
<center><h:commandButton action="#{clientCon.createClient()}" value="Saxla" style="width: 60%;margin-top: 5%;border-radius: 5px;color: #ffffff;background-color: #0099cc" /></center>
</div>
</fieldset>
</h:form>
javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.
at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.validateOnCallbackEvent(BeanValidationListener.java:90)
at org.eclipse.persistence.internal.jpa.metadata.listeners.BeanValidationListener.prePersist(BeanValidationListener.java:62)
at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyListener(DescriptorEventManager.java:748)
at org.eclipse.persistence.descriptors.DescriptorEventManager.notifyEJB30Listeners(DescriptorEventManager.java:691)
at org.eclipse.persistence.descriptors.DescriptorEventManager.executeEvent(DescriptorEventManager.java:229)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectClone(UnitOfWorkImpl.java:4310)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:4287)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:518)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4229)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:496)
at controller.ClientController.createClient(ClientController.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.ELUtil.invokeMethod(ELUtil.java:326)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:536)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)

To know what caused the constraint violation, you can use the following validator and logger.
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Clients>> constraintViolations = validator.validate(clients);
if (constraintViolations.size() > 0 ) {
System.out.println("Constraint Violations occurred..");
for (ConstraintViolation<Clients> contraints : constraintViolations) {
System.out.println(contraints.getRootBeanClass().getSimpleName()+
"." + contraints.getPropertyPath() + " " + contraints.getMessage());
}
}
Put the logger before persisting the entity. So between
em.getTransaction().begin();
//here goes the validator
em.persist(clients);
Compile and run. The console will show you, just before the exception stack trace, which element(s) caused the violation(s).
You can but should catch your try block containing any persistence method with ConstraintViolationException (to avoid further problems and/or inform the user an error occurred and its reason). However, in a well built system there shouldn't be any constraint violation exception during persistence. In JSF, and other MVC framework, the validation step must be totally or partially done at the client side before submit/persistence. That's a good practice I would say.

This is another way from correct answer on Sujan Sivagurunathan, i wrote not in comment because i dont have 50 reputation.
If you have AbstractFacade.java write this on create method
public void create(T entity) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
javax.validation.Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
if (constraintViolations.size() > 0 ) {
System.out.println("Constraint Violations occurred..");
for (ConstraintViolation<T> contraints : constraintViolations) {
System.out.println(contraints.getRootBeanClass().getSimpleName()+
"." + contraints.getPropertyPath() + " " + contraints.getMessage());
}
getEntityManager().persist(entity);
}
}

Related

java.lang.NullPointerException at org.primefaces.component.filedownload.FileDownloadActionListener.processAction

I am working on a JSF/PrimeFaces project where I am generating some files from a database and placing them in the web/resources folder of my project.I am using a p:datable to load the file names dynamically into a web page. The datatable has a singleMode activated. What I want is to make the files downloadable based on the users choice.when a file is selected and the download button is pushed an exception is thrown;
Here are the codes I used:
JSF page
<div id="content">
<h:form id="resultForm">
<center>
<p:panel id="files" style="width: 80%">
<p:dataTable id="resultDT" var="file" value="#{gen.outFileNames}"
selection="#{downloader.selectedFileName}" selectionMode="single" rowKey="#{file}">
<f:facet name="header">
Generated Files
</f:facet>
<p:column headerText="File names">
<h:outputText value="#{file}" />
</p:column>
<f:facet name="footer">
<p:commandButton value="Download" ajax="false" icon="ui-icon-arrowthick-1-s">
<p:fileDownload value="#{downloader.file}"/>
</p:commandButton>
</f:facet>
</p:dataTable>
</p:panel>
</center>
</h:form>
</div>
Download Controller
#ManagedBean(name = "downloader")
#ViewScoped
public class FilesDownloadController implements Serializable {
private String selectedFileName;
public StreamedContent getFile() {
if (selectedFileName == null) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
"Download warning", "Please click a file and click on the download button."));
return null;
}
System.out.println("Download 1 : selectedFileName = " + selectedFileName);
System.out.println("Download 2");
System.out.println("Download 3");
InputStream stream = this.getClass().getResourceAsStream(selectedFileName);
System.out.println("Download 4");
StreamedContent file = new DefaultStreamedContent(stream, ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getMimeType(selectedFileName),
selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
System.out.println("Download 5 = " + selectedFileName.split("/")[selectedFileName.split("/").length - 1]);
System.out.println(file != null ? "File content OK " + file.getContentType() + " " + file.getName() : "File content NOK");
return file;
}
public String getSelectedFileName() {
return selectedFileName;
}
public void setSelectedFileName(String selectedFileName) {
this.selectedFileName = selectedFileName;
System.out.println("Selected File changed: " + this.selectedFileName );
}
}
Generator bean
#ManagedBean(name = "gen")
#SessionScoped
public class GeneratorController implements Serializable {
private List<String> outFileNames;
public GeneratorController() {
outFileNames = new ArrayList<String>();
outFileNames.add("/resources/images/optimus.jpg");
outFileNames.add("/resources/images/optimus_1.jpg");
outFileNames.add("/resources/images/optimus_2.jpg");
outFileNames.add("/resources/images/optimus_3.jpg");
}
public List<String> getOutFileNames() {
return outFileNames;
}
public void setOutFileNames(List<String> outFileNames) {
this.outFileNames = outFileNames;
}
}
Exceptions thrown
Infos: Selected File changed: /resources/images/optimus_1.jpg
Infos: Download 1 : selectedFileName = /resources/images/optimus_1.jpg
Infos: Download 2
Infos: Download 3
Infos: Download 4
Infos: Download 5 = optimus_1.jpg
Infos: File content OK image/jpeg optimus_1.jpg
FATAL: JSF1073 : javax.faces.FacesException intercepté durant le traitement de INVOKE_APPLICATION 5 : UIComponent-ClientId=, Message=null
FATAL: No associated message
javax.faces.FacesException
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:89)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:81)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIData.broadcast(UIData.java:1108)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
... 31 more
If you have an external file somewhere in the file system, you have to use new FileInputStream(new File(exportFile.getAbsolutePath())) instead of FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(exportFile.getAbsolutePath()) in the showcase. I had the same problem in Primefaces 8 and solved it like this:
file = DefaultStreamedContent.builder().name(exportFile.getName())
.contentType("application/zip").stream(() ->
{
try{
return new FileInputStream(new File(exportFile.getAbsolutePath()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}).build();
I finally got a way to download the files. The folder where the images are located is not on the class path, so
InputStream stream = this.getClass().getResourceAsStream(selectedFileName);
returns null and an NPE occurs. So by replacing the code of the Download controller with
InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream(selectedFileName);
The webapp context is used for locating the files and it works

Redirect a page from a button

Hi i'm trying to develop a login page using oracle jdeveloper 12c and. I'm working with JEE technology and JSF pages.
I want to redirect to the main page after pressing authentification button but it doesn't work and I get this excpetion:
javax.servlet.ServletException: ADF_FACES-60101:Code de statut d''erreur HTTP : 404."
This is my authentification function in the ejb part
public String authentification(String login, String pwd) {
try{
Query query;
query = em.createQuery("select o from UserEntity o where "
+ " o.login = :LOGIN AND o.pwd = :PWD");
query.setParameter("LOGIN",login);
query.setParameter("PWD",pwd);
query.getSingleResult();
return ("success") ;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}
this is my loginaction in the managed bean
public String loginaction() {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request =
(HttpServletRequest)ctx.getExternalContext().getRequest();
String Url =
"/adfAuthentication?success_url=/faces/Main.jsf";
HttpServletResponse response;
response = (HttpServletResponse) ctx.getExternalContext().getResponse();
RequestDispatcher dispatcher =
request.getRequestDispatcher(Url);
try {
dispatcher.forward(request, response);
} catch (IOException e) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Username or Password", "Invalid Username or Password");
ctx.addMessage(null, msg);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
ad this is my jsf
<af:panelFormLayout id="pfl1">
<af:inputText value="#{bindings.login.inputValue}" label="#{bindings.login.hints.label}"
required="#{bindings.login.hints.mandatory}"
columns="#{bindings.login.hints.displayWidth}"
maximumLength="#{bindings.login.hints.precision}"
shortDesc="#{bindings.login.hints.tooltip}" id="it1">
<f:validator binding="#{bindings.login.validator}"/>
</af:inputText>
<af:inputText value="#{bindings.pwd.inputValue}" label="#{bindings.pwd.hints.label}"
required="#{bindings.pwd.hints.mandatory}"
columns="#{bindings.pwd.hints.displayWidth}"
maximumLength="#{bindings.pwd.hints.precision}"
shortDesc="#{bindings.pwd.hints.tooltip}" id="it2">
<f:validator binding="#{bindings.pwd.validator}"/>
</af:inputText>
<af:button actionListener="#{bindings.authentification.execute}" text="authentification"
disabled="#{!bindings.authentification.enabled}" id="b1"
action="#{Login2.loginaction}"/>
</af:panelFormLayout>
this is what i get in the server dashboard
javax.servlet.ServletException: ADF_FACES-60101:Code de statut d''erreur HTTP : 404."
at oracle.adfinternal.view.faces.config.rich.XmlHttpServletResponse._logException(XmlHttpServletResponse.java:141)
at oracle.adfinternal.view.faces.config.rich.XmlHttpServletResponse.sendError(XmlHttpServletResponse.java:107)
at oracle.adfinternal.view.faces.config.rich.XmlHttpServletResponse.sendError(XmlHttpServletResponse.java:101)
at weblogic.servlet.FileServlet.findSource(FileServlet.java:267)
at weblogic.servlet.FileServlet.doGetHeadPost(FileServlet.java:178)
at weblogic.servlet.FileServlet.service(FileServlet.java:160)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:238)
at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:573)
at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:272)
at view.Login2.loginaction(Login2.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:46)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:190)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._invokeApplication(LifecycleImpl.java:1083)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:402)
at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:225)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:341)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:192)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter(RegistrationFilter.java:106)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:478)
at oracle.adfinternal.view.faces.activedata.AdsFilter.doFilter(AdsFilter.java:60)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:478)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:303)
at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:208)
at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:137)
at java.security.AccessController.doPrivileged(Native Method)
at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:315)
at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:460)
at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:120)
at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:217)
at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:81)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:225)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3367)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3333)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2220)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2146)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2124)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1564)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:295)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:254)
From the log i see that it's not just a simple JSF , but an ADF based application. I can think of two ways to achieve the redirection to a page:
1. You could define a navigation rule in the adfc-config.xml (or even faces-config.xml) file and then, simply put the name of this defined rule as the button's action property value.
2. If you wish a programmatically approach, then you could call a code returning this kind of string, "mainPage.jsf?faces-redirect=true" , since adf adds its own context. For example:
public String loginaction() {
........
return "mainPage.jsf" + "?faces-redirect=true";
}
NOTE: You might need to add "/faces/mainPage.jsf" or "/faces/.../mainPage.jsf" instead. It really depends on the location of the two pages participating in the navigation.
As per the dispatcher part, that was used with JSP. It is not needed nor advisable to use it anymore.

Error in streaming dynamic resource. null

Important Notice : This issue has been fixed as of PrimeFaces 5.2 final (Community Release) released on April 8, 2015. As such if you happened to use that version or newer, you would not need to fiddle around with a temporary workaround.
The earlier given example can now safely be modified as follows.
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
String id = context.getExternalContext().getRequestParameterMap().get("id");
byte[] bytes = Utils.isNumber(id) ? service.findImageById(Long.parseLong(id)) : null;
return bytes == null ? null : new DefaultStreamedContent(new ByteArrayInputStream(bytes));
}
}
I have moved images to the database (MySQL) in the form of BLOB (LONGBLOB) after I got tired of manipulating/managing images stored in the disk file system.
Accordingly, I'm displaying images in <p:dataTable> as follows (blatantly copied from here :) ).
<p:column headerText="Header">
<p:graphicImage value="#{bannerBean.image}" height="200" width="200">
<f:param name="id" value="#{row.bannerId}"/>
</p:graphicImage>
<p:column>
The bean that retrieves images is as follows.
#ManagedBean
#ApplicationScoped
public final class BannerBean
{
#EJB
private final BannerBeanLocal service=null;
public BannerBean() {}
public StreamedContent getImage() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
}
else {
String id = context.getExternalContext().getRequestParameterMap().get("id");
byte[] bytes = service.findImageById(Long.parseLong(id));
return bytes==null? new DefaultStreamedContent():new DefaultStreamedContent(new ByteArrayInputStream(bytes));
}
}
}
This works fine as long as there are images in each row of the underlying database table.
The BLOB type column in the database is however, optional in some cases and hence, it can contain null values as well.
If this column in any row/s in the database is null then, the following exception is thrown.
SEVERE: Error in streaming dynamic resource. null
WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:127)
at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:722)
So how to manage null BLOBs so that this exception disappears?
Returning new DefaultStreamedContent(new ByteArrayInputStream(new byte[0])), in case, the byte array in the managed bean is null would suppress the exception but this after all, should not be a solution. Is this a desired solution?
The EJB method that returns a byte array though completely unnecessary, in this case.
public byte[] findImageById(Long id)
{
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<byte[]>criteriaQuery=criteriaBuilder.createQuery(byte[].class);
Root<BannerImages> root = criteriaQuery.from(BannerImages.class);
criteriaQuery.multiselect(root.get(BannerImages_.bannerImage));
ParameterExpression<Long>parameterExpression=criteriaBuilder.parameter(Long.class);
criteriaQuery.where(criteriaBuilder.equal(root.get(BannerImages_.bannerId), parameterExpression));
List<byte[]> list = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, id).getResultList();
return list!=null&&!list.isEmpty()?list.get(0):null;
}
This is a bug (at least, a functional/technical design mistake) in PrimeResourceHandler. It shouldn't have assumed the dynamic resource or its content to be never null. It should have conditionally checked if that was the case and then simply have returned a HTTP 404 "Not Found" response.
In other words, instead of
85 streamedContent = (StreamedContent) ve.getValue(eLContext);
86
87 externalContext.setResponseStatus(200);
88 externalContext.setResponseContentType(streamedContent.getContentType());
they should have done
85 streamedContent = (StreamedContent) ve.getValue(eLContext);
86
87 if (streamedContent == null || streamedContent.getStream() == null) {
88 externalContext.responseSendError(HttpServletResponse.SC_NOT_FOUND, ((HttpServletRequest) externalContext.getRequest()).getRequestURI());
89 return;
90 }
91
92 externalContext.setResponseStatus(200);
93 externalContext.setResponseContentType(streamedContent.getContentType());
This way you can just return null or an empty StreamedContent from the getImage() method in order to generate a decent 404.
Well, what can you do?
Report it to them and hope that they'll fix it. Update They fixed it in version 5.2.
And/or, put a copy of PrimeResourceHandler class in the Java source folder of your webapp project, in exactly its own org.primefaces.application package and then just edit it to include the mentioned change and finally just build/deploy your webapp project as WAR as usual. Classes in /WEB-INF/classes have higher classloading precedence over those in JARs in /WEB-INF/lib, so the modified one will be used instead.
I would suggest to include an attribute in the object the <p:dataTable> is iterating on to signify if an image exists. That way, there are no unnecessary (or null return) calls to BannerBean.getImage().
Example:
<p:column headerText="Header">
<p:graphicImage value="#{bannerBean.image}" height="200" width="200"
rendered="#{row.hasImage}">
<f:param name="id" value="#{row.bannerId}"/>
</p:graphicImage>
<p:column>
Another option is to grab the Primefaces source code, edit PrimeResourceHandler.java, and build it. (see the wiki)
An alternative solution would be to setup your own Serlvet to provide the images.
Main benefit is that browsers can cache the image (you can specify cache timeout if wanted).
Be aware of SQL Injection & other security attacks.
Attach some sort of login/permission check for additional security. (if needed)
Example:
<p:column headerText="Header">
<p:graphicImage value="#{request.contextPath}/images/banner/?id=#{row.bannerId}"
height="200" width="200" />
<p:column>
#WebServlet(name = "Retrieve Banner Images", urlPatterns = "/images/banner/*")
public class BannerImageServlet extends HttpServlet
{
#EJB
private final BannerBeanLocal service;
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String[] ids = req.getParameterValues("id");
if(ids != null && ids.length == 1) {
byte[] bytes = service.findImageById(Long.parseLong(ids[0]));
if(bytes != null) {
// see link #3 below
}
}
}
}
Sources / useful links:
#{request.contextPath}
#WebServlet tutorial
how to send byte stream back to the client

Primefaces fileupload component does not work [duplicate]

Important : The issue which is discussed in this thread has been fixed as of PrimeFaces 5.1 final (community release) released on Monday, October 6, 2014 (just a few minutes ago from now). I attempted on JSF 2.2.8-02 (or api, impl).
As such, if you happened to use that version (or higher, no need to mention), you would not even need to read this question anymore.
I have a web application running on
GlassFish 4.0
Mojarra 2.2.4
PrimeFaces 4.0 final
Everything except file upload with AJAX works well. The following xhtml file sends multipart contents via an AJAX request triggered by a PrimeFaces command button.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:form prependId="true" enctype="multipart/form-data">
<p:fileUpload id="txtCatImage"
value="#{testManagedBean.uploadedFile}"
mode="advanced"
dragDropSupport="true"
fileLimit="1"
sizeLimit="100000"
multiple="false"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
fileUploadListener="#{testManagedBean.fileUploadListener}"/>
<p:message for="txtCatImage" showSummary="false"/>
<p:commandButton id="btnSubmit"
actionListener="#{testManagedBean.insert}"
ajax="true" icon="ui-icon-check" value="Save"/>
</h:form>
</h:body>
</html>
The test managed bean:
#ManagedBean
#ViewScoped
public final class TestManagedBean implements Serializable {
private static final long serialVersionUID = 1L;
private UploadedFile uploadedFile;
public TestManagedBean(){}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void fileUploadListener(FileUploadEvent event){
uploadedFile=event.getFile();
}
public void insert(){
if(uploadedFile!=null){
System.out.println(uploadedFile.getFileName());
}
else{
System.out.println("The file object is null.");
}
}
}
When a file is uploaded from a file browser, it shows the file name in its listener - fileUploadListener().
After uploading a file, when the given command button is pressed (ajax="true"), it causes the following exception to be thrown.
WARNING: javax.servlet.ServletException: The request content-type is not a multipart/form-data
javax.faces.FacesException: javax.servlet.ServletException: The request content-type is not a multipart/form-data
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:44)
at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
at javax.faces.component.UIInput.decode(UIInput.java:771)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1225)
at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.servlet.ServletException: The request content-type is not a multipart/form-data
at org.apache.catalina.fileupload.Multipart.getPart(Multipart.java:187)
at org.apache.catalina.connector.Request.getPart(Request.java:4535)
at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1095)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decodeAdvanced(NativeFileUploadDecoder.java:60)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:37)
... 48 more
SEVERE: javax.servlet.ServletException: The request content-type is not a multipart/form-data
at org.apache.catalina.fileupload.Multipart.getPart(Multipart.java:187)
at org.apache.catalina.connector.Request.getPart(Request.java:4535)
at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1095)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decodeAdvanced(NativeFileUploadDecoder.java:60)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:37)
at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
at javax.faces.component.UIInput.decode(UIInput.java:771)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1225)
at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:722)
It can only work, when the ajax attribute of the command button is set to false - ajax="false".
I have downgraded the Mojarra version to 2.1.9 in Tomcat 7.0.35. It worked with this Mojarra version along with PrimeFaces 4.0 final (and PrimeFaces 4.0 RC1 too) - files uploaded with an AJAX request.
I have alternatively tried on the following Mojarra versions
2.2.0
2.2.1
2.2.2
2.2.3
2.2.4
in GlassFish 4.0 but none of them succeeded to upload files with an AJAX request which is highly required because editing of rows using <p:rowEditor/> (along with images in each row, for example) in PrimeFaces DataTable is always AJAX based.
I want to keep GlassFish 4.0 anyhow. I have also tried to downgrade Mojarra 2.1.9 in GlassFish 4.0 but it failed to create bundles ending with an exception. GlassFish 4.0 doesn't seem to work with Mojarra lower than 2.2.x.
So, what is responsible for causing this exception - PrimeFaces or JSF? Just confused. Is there any workaround to upload files with AJAX requests in this given environment?
EDIT:
Filter mapping in web.xml:
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
The problem remains stationary along with the Mojarra version 2.2.5 (or api, impl) released on January 08, 2014.
Once again tried on the Mojarra version 2.2.6 (or api, impl) released on March 04, 2014. The problem remains intact.
Still does not work on PrimeFaces 5.0 final released on May 05, 2014.
As Kai rightfully pointed out in his answer on the current question, the problem is caused by the NativeFileUploadDecoder as used by FileUploadRenderer not checking whether the request is a multipart/form-data request or not. This will cause trouble when the component is present in a form on which a "regular" ajax request is being submitted. The CommonsFileUploadDecoder checks that correctly and that's why it works correctly in JSF 2.1 which didn't have a native file upload parser yet.
His solution of workarounding this with a custom renderer is in the right direction, however the approach is quite clumsy. There's in this particular case absolutely no need of copypasting the whole class consisting more than 200 lines just to add a few more lines. Instead, just extend exactly that class and override exactly the method with an if check before delegating to the super as follows:
package com.example;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.primefaces.component.fileupload.FileUploadRenderer;
public class MyFileUploadRenderer extends FileUploadRenderer {
#Override
public void decode(FacesContext context, UIComponent component) {
if (context.getExternalContext().getRequestContentType().toLowerCase().startsWith("multipart/")) {
super.decode(context, component);
}
}
}
That's it (keep that <renderer-kit> entry in faces-config.xml though). There's no point of continuing the decode if the request isn't a multipart request. The file parts wouldn't be available anyway (and there's also no point of casting back to javax.servlet.* API when the same functionality is readily available via ExternalContext).
I had the same issue. It seems to be more related to the <p:commandButton> than the <p:fileUpload> component, since it works with an <h:commandButton> (even with ajax).
You could try:
<h:commandButton id="btnSubmit" actionListener="#{testManagedBean.insert}" value="Save">
<f:ajax execute="#all" render="#form"/>
</h:commandButton>
I cannot tell you why or how it works, but it fixed the issue for me.
The downside of course is, that you have to do the styling by yourself, at least until
the Primefaces guys fix this issue.
EDIT:
After digging in the sources and doing some debugging if figured out that there are actually two requests made (I tried in <p:wizard/>). The first one is the multipart/form-data one which actually does the file upload. It fires the fileUploadEvent in the Bean. I the wizards next button is pressed another form with enctype application/www-urlencoded is submitted. This causes the exception. Conclusion is, that unlike what I wrote in the comment suppressing the exception is a valid solution. This can even be done in a way that does not include changing the Primefaces.jar which is handy if the guys fix the issue in a future version.
So here is what needs to be done:
Create a new class com.yourpackage.fileupload.FileUploadRenderer
Copy and paste the following code inside your new class:
package com.yourpackage.fileupload.fileupload;
import java.io.IOException;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.component.fileupload.CommonsFileUploadDecoder;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.component.fileupload.NativeFileUploadDecoder;
import org.primefaces.config.ConfigContainer;
import org.primefaces.context.RequestContext;
import org.primefaces.expression.SearchExpressionFacade;
import org.primefaces.renderkit.CoreRenderer;
import org.primefaces.util.HTML;
import org.primefaces.util.WidgetBuilder;
public class FileUploadRenderer extends CoreRenderer {
#Override
public void decode(FacesContext context, UIComponent component) {
FileUpload fileUpload = (FileUpload) component;
if (!fileUpload.isDisabled()) {
ConfigContainer cc = RequestContext.getCurrentInstance().getApplicationContext().getConfig();
String uploader = cc.getUploader();
boolean isAtLeastJSF22 = cc.isAtLeastJSF22();
if (uploader.equals("auto")) {
if (isAtLeastJSF22) {
if (isMultiPartRequest(context)) {
NativeFileUploadDecoder.decode(context, fileUpload);
}
} else {
CommonsFileUploadDecoder.decode(context, fileUpload);
}
} else if (uploader.equals("native")) {
if (!isAtLeastJSF22) {
throw new FacesException("native uploader requires at least a JSF 2.2 runtime");
}
NativeFileUploadDecoder.decode(context, fileUpload);
} else if (uploader.equals("commons")) {
CommonsFileUploadDecoder.decode(context, fileUpload);
}
}
}
#Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
FileUpload fileUpload = (FileUpload) component;
encodeMarkup(context, fileUpload);
if (fileUpload.getMode().equals("advanced")) {
encodeScript(context, fileUpload);
}
}
protected void encodeScript(FacesContext context, FileUpload fileUpload) throws IOException {
String clientId = fileUpload.getClientId(context);
String update = fileUpload.getUpdate();
String process = fileUpload.getProcess();
WidgetBuilder wb = getWidgetBuilder(context);
wb.initWithDomReady("FileUpload", fileUpload.resolveWidgetVar(), clientId, "fileupload");
wb.attr("auto", fileUpload.isAuto(), false)
.attr("dnd", fileUpload.isDragDropSupport(), true)
.attr("update", SearchExpressionFacade.resolveComponentsForClient(context, fileUpload, update), null)
.attr("process", SearchExpressionFacade.resolveComponentsForClient(context, fileUpload, process), null)
.attr("maxFileSize", fileUpload.getSizeLimit(), Long.MAX_VALUE)
.attr("fileLimit", fileUpload.getFileLimit(), Integer.MAX_VALUE)
.attr("invalidFileMessage", fileUpload.getInvalidFileMessage(), null)
.attr("invalidSizeMessage", fileUpload.getInvalidSizeMessage(), null)
.attr("fileLimitMessage", fileUpload.getFileLimitMessage(), null)
.attr("messageTemplate", fileUpload.getMessageTemplate(), null)
.attr("previewWidth", fileUpload.getPreviewWidth(), 80)
.attr("disabled", fileUpload.isDisabled(), false)
.callback("onstart", "function()", fileUpload.getOnstart())
.callback("onerror", "function()", fileUpload.getOnerror())
.callback("oncomplete", "function()", fileUpload.getOncomplete());
if (fileUpload.getAllowTypes() != null) {
wb.append(",allowTypes:").append(fileUpload.getAllowTypes());
}
wb.finish();
}
protected void encodeMarkup(FacesContext context, FileUpload fileUpload) throws IOException {
if (fileUpload.getMode().equals("simple")) {
encodeSimpleMarkup(context, fileUpload);
} else {
encodeAdvancedMarkup(context, fileUpload);
}
}
protected void encodeAdvancedMarkup(FacesContext context, FileUpload fileUpload) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = fileUpload.getClientId(context);
String style = fileUpload.getStyle();
String styleClass = fileUpload.getStyleClass();
styleClass = styleClass == null ? FileUpload.CONTAINER_CLASS : FileUpload.CONTAINER_CLASS + " " + styleClass;
boolean disabled = fileUpload.isDisabled();
writer.startElement("div", fileUpload);
writer.writeAttribute("id", clientId, "id");
writer.writeAttribute("class", styleClass, styleClass);
if (style != null) {
writer.writeAttribute("style", style, "style");
}
//buttonbar
writer.startElement("div", fileUpload);
writer.writeAttribute("class", FileUpload.BUTTON_BAR_CLASS, null);
//choose button
encodeChooseButton(context, fileUpload, disabled);
if (!fileUpload.isAuto()) {
encodeButton(context, fileUpload.getUploadLabel(), FileUpload.UPLOAD_BUTTON_CLASS, "ui-icon-arrowreturnthick-1-n");
encodeButton(context, fileUpload.getCancelLabel(), FileUpload.CANCEL_BUTTON_CLASS, "ui-icon-cancel");
}
writer.endElement("div");
//content
writer.startElement("div", null);
writer.writeAttribute("class", FileUpload.CONTENT_CLASS, null);
writer.startElement("table", null);
writer.writeAttribute("class", FileUpload.FILES_CLASS, null);
writer.startElement("tbody", null);
writer.endElement("tbody");
writer.endElement("table");
writer.endElement("div");
writer.endElement("div");
}
protected void encodeSimpleMarkup(FacesContext context, FileUpload fileUpload) throws IOException {
encodeInputField(context, fileUpload, fileUpload.getClientId(context));
}
protected void encodeChooseButton(FacesContext context, FileUpload fileUpload, boolean disabled) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = fileUpload.getClientId(context);
String cssClass = HTML.BUTTON_TEXT_ICON_LEFT_BUTTON_CLASS + " " + FileUpload.CHOOSE_BUTTON_CLASS;
if (disabled) {
cssClass += " ui-state-disabled";
}
writer.startElement("span", null);
writer.writeAttribute("class", cssClass, null);
//button icon
writer.startElement("span", null);
writer.writeAttribute("class", HTML.BUTTON_LEFT_ICON_CLASS + " ui-icon-plusthick", null);
writer.endElement("span");
//text
writer.startElement("span", null);
writer.writeAttribute("class", HTML.BUTTON_TEXT_CLASS, null);
writer.writeText(fileUpload.getLabel(), "value");
writer.endElement("span");
if (!disabled) {
encodeInputField(context, fileUpload, clientId + "_input");
}
writer.endElement("span");
}
protected void encodeInputField(FacesContext context, FileUpload fileUpload, String clientId) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("input", null);
writer.writeAttribute("type", "file", null);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("name", clientId, null);
if (fileUpload.isMultiple()) {
writer.writeAttribute("multiple", "multiple", null);
}
if (fileUpload.getStyle() != null) {
writer.writeAttribute("style", fileUpload.getStyle(), "style");
}
if (fileUpload.getStyleClass() != null) {
writer.writeAttribute("class", fileUpload.getStyleClass(), "styleClass");
}
if (fileUpload.isDisabled()) {
writer.writeAttribute("disabled", "disabled", "disabled");
}
writer.endElement("input");
}
protected void encodeButton(FacesContext context, String label, String styleClass, String icon) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String cssClass = HTML.BUTTON_TEXT_ICON_LEFT_BUTTON_CLASS + " ui-state-disabled " + styleClass;
writer.startElement("button", null);
writer.writeAttribute("type", "button", null);
writer.writeAttribute("class", cssClass, null);
writer.writeAttribute("disabled", "disabled", null);
//button icon
String iconClass = HTML.BUTTON_LEFT_ICON_CLASS;
writer.startElement("span", null);
writer.writeAttribute("class", iconClass + " " + icon, null);
writer.endElement("span");
//text
writer.startElement("span", null);
writer.writeAttribute("class", HTML.BUTTON_TEXT_CLASS, null);
writer.writeText(label, "value");
writer.endElement("span");
writer.endElement("button");
}
private boolean isMultiPartRequest(FacesContext context) {
if (context == null) {
return false;
}
return ((HttpServletRequest) context.getExternalContext().getRequest()).getContentType().startsWith("multipart");
}
}
Add the following lines at the bottom of your faces-config.xml:
<render-kit>
<renderer>
<component-family>org.primefaces.component</component-family>
<renderer-type>org.primefaces.component.FileUploadRenderer</renderer-type>
<renderer-class>com.yourpackage.fileupload.FileUploadRenderer</renderer-class>
</renderer>
</render-kit>
You're ready to go!
What have we done? We created our own FileUploadRenderer which checks if the contentType is really multipart/form-data using the method isMultiPartRequest(). Only if this returns true the rest of the code is executed. In any other case nothing will happen which means that no exception will be thrown. If Primefaces fixes this issue you just need to remove the lines from your faces-config.xml to use their class.
Let me know if that works for you!
EDIT
This code checks wether or not the given request is of type multipart/form-data. If it is not the execution is stopped. The original Primefaces code would continue anyway. As I mentioned above if you upload a file inside a Primefaces component there are actually two requests made:
The Ajax-FileUpload using the <p:fileUpload/> (enctype: multipart/form-data)
The Ajax action in the <p:editRow/> or <p:wizard/> (enctype: application/www-form-urlencoded)
The first one is handled by the renderer while the second one causes the exception in the original code since the renderer tries to handle something which it is not capable of. With the changes made in the code only multipart/form-data forms are handled by the renderer so no exceptions occur. IMO it is clearly a bug in the sources of Primefaces. The code differences is just the method private boolean isMultiPartRequest(FacesContext context) and its one occurrence in the code. Glad I could help you!
Although this is old and already answered, i wanted to share something, just in case you missed it:
PrimeFaces 4+ now has a context paramter, which you can use (in web.xml) to manually choose which uploader should be used (native-servlet3 or commons). You can use this to force commons-uploader like this:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
(Of course you still need FileUploadFilter as described above and in the guide).
See PrimeFaces User's Guide for more information.
#BalusC - Your suggestion for extending the existing FileUploadRenderer is very clean. Thanks!
Depending on the version of JSF you'r using you may see a random iFrame pop up. This is a bug noted here: JAVASERVERFACES-2843
In my first attempt to fix this (without having to upgrade to 2.2.1) I just hid the iFrame with CSS.
#JSFFrameId {
visibility:hidden;
}
This worked but for some reason additional AJAX submits would not fire. I then called a little script to remove the iFrame and that fixed the problem.
<h:commandButton id="btnSubmit" action="#{fileUploadController.upload}" value="Save" >
<f:ajax execute="#all" render="frmMain" onevent="removeIFrame()" />
</h:commandButton>
JavaScript:
function removeIFrame()
{
document.getElementById("JSFFrameId").removeNode();
}
I had the same issue,
In my case i have used primefaces file uploader in a data table, tried to modify the existing image using onRowEdit, which was end up with the same error mentioned above.
Then i changed the primefaces jar to 5.1 version. Now its working fine.
The custom class work for me:
#Override
public void decode(FacesContext context, UIComponent component) {
if (!context.getExternalContext().getRequestContentType().toLowerCase().startsWith("multipart/")) {
return;
}
FileUpload fileUpload = (FileUpload) component;
if (!fileUpload.isDisabled()) {
PrimeConfiguration cc = RequestContext.getCurrentInstance().getApplicationContext().getConfig();
String uploader = cc.getUploader();
boolean isAtLeastJSF22 = cc.isAtLeastJSF22();
String inputToDecodeId = getSimpleInputDecodeId(fileUpload, context);
if (uploader.equals("auto")) {
if (isAtLeastJSF22)
NativeFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
else
CommonsFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
}
else if (uploader.equals("native")) {
NativeFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
}
else if (uploader.equals("commons")) {
CommonsFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
}
}
}

File upload doesn't work with AJAX in PrimeFaces 4.0/JSF 2.2.x - javax.servlet.ServletException: The request content-type is not a multipart/form-data

Important : The issue which is discussed in this thread has been fixed as of PrimeFaces 5.1 final (community release) released on Monday, October 6, 2014 (just a few minutes ago from now). I attempted on JSF 2.2.8-02 (or api, impl).
As such, if you happened to use that version (or higher, no need to mention), you would not even need to read this question anymore.
I have a web application running on
GlassFish 4.0
Mojarra 2.2.4
PrimeFaces 4.0 final
Everything except file upload with AJAX works well. The following xhtml file sends multipart contents via an AJAX request triggered by a PrimeFaces command button.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Test</title>
</h:head>
<h:body>
<h:form prependId="true" enctype="multipart/form-data">
<p:fileUpload id="txtCatImage"
value="#{testManagedBean.uploadedFile}"
mode="advanced"
dragDropSupport="true"
fileLimit="1"
sizeLimit="100000"
multiple="false"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
fileUploadListener="#{testManagedBean.fileUploadListener}"/>
<p:message for="txtCatImage" showSummary="false"/>
<p:commandButton id="btnSubmit"
actionListener="#{testManagedBean.insert}"
ajax="true" icon="ui-icon-check" value="Save"/>
</h:form>
</h:body>
</html>
The test managed bean:
#ManagedBean
#ViewScoped
public final class TestManagedBean implements Serializable {
private static final long serialVersionUID = 1L;
private UploadedFile uploadedFile;
public TestManagedBean(){}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public void fileUploadListener(FileUploadEvent event){
uploadedFile=event.getFile();
}
public void insert(){
if(uploadedFile!=null){
System.out.println(uploadedFile.getFileName());
}
else{
System.out.println("The file object is null.");
}
}
}
When a file is uploaded from a file browser, it shows the file name in its listener - fileUploadListener().
After uploading a file, when the given command button is pressed (ajax="true"), it causes the following exception to be thrown.
WARNING: javax.servlet.ServletException: The request content-type is not a multipart/form-data
javax.faces.FacesException: javax.servlet.ServletException: The request content-type is not a multipart/form-data
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:44)
at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
at javax.faces.component.UIInput.decode(UIInput.java:771)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1225)
at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.servlet.ServletException: The request content-type is not a multipart/form-data
at org.apache.catalina.fileupload.Multipart.getPart(Multipart.java:187)
at org.apache.catalina.connector.Request.getPart(Request.java:4535)
at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1095)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decodeAdvanced(NativeFileUploadDecoder.java:60)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:37)
... 48 more
SEVERE: javax.servlet.ServletException: The request content-type is not a multipart/form-data
at org.apache.catalina.fileupload.Multipart.getPart(Multipart.java:187)
at org.apache.catalina.connector.Request.getPart(Request.java:4535)
at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1095)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decodeAdvanced(NativeFileUploadDecoder.java:60)
at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:37)
at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
at javax.faces.component.UIInput.decode(UIInput.java:771)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1225)
at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:722)
It can only work, when the ajax attribute of the command button is set to false - ajax="false".
I have downgraded the Mojarra version to 2.1.9 in Tomcat 7.0.35. It worked with this Mojarra version along with PrimeFaces 4.0 final (and PrimeFaces 4.0 RC1 too) - files uploaded with an AJAX request.
I have alternatively tried on the following Mojarra versions
2.2.0
2.2.1
2.2.2
2.2.3
2.2.4
in GlassFish 4.0 but none of them succeeded to upload files with an AJAX request which is highly required because editing of rows using <p:rowEditor/> (along with images in each row, for example) in PrimeFaces DataTable is always AJAX based.
I want to keep GlassFish 4.0 anyhow. I have also tried to downgrade Mojarra 2.1.9 in GlassFish 4.0 but it failed to create bundles ending with an exception. GlassFish 4.0 doesn't seem to work with Mojarra lower than 2.2.x.
So, what is responsible for causing this exception - PrimeFaces or JSF? Just confused. Is there any workaround to upload files with AJAX requests in this given environment?
EDIT:
Filter mapping in web.xml:
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
The problem remains stationary along with the Mojarra version 2.2.5 (or api, impl) released on January 08, 2014.
Once again tried on the Mojarra version 2.2.6 (or api, impl) released on March 04, 2014. The problem remains intact.
Still does not work on PrimeFaces 5.0 final released on May 05, 2014.
As Kai rightfully pointed out in his answer on the current question, the problem is caused by the NativeFileUploadDecoder as used by FileUploadRenderer not checking whether the request is a multipart/form-data request or not. This will cause trouble when the component is present in a form on which a "regular" ajax request is being submitted. The CommonsFileUploadDecoder checks that correctly and that's why it works correctly in JSF 2.1 which didn't have a native file upload parser yet.
His solution of workarounding this with a custom renderer is in the right direction, however the approach is quite clumsy. There's in this particular case absolutely no need of copypasting the whole class consisting more than 200 lines just to add a few more lines. Instead, just extend exactly that class and override exactly the method with an if check before delegating to the super as follows:
package com.example;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.primefaces.component.fileupload.FileUploadRenderer;
public class MyFileUploadRenderer extends FileUploadRenderer {
#Override
public void decode(FacesContext context, UIComponent component) {
if (context.getExternalContext().getRequestContentType().toLowerCase().startsWith("multipart/")) {
super.decode(context, component);
}
}
}
That's it (keep that <renderer-kit> entry in faces-config.xml though). There's no point of continuing the decode if the request isn't a multipart request. The file parts wouldn't be available anyway (and there's also no point of casting back to javax.servlet.* API when the same functionality is readily available via ExternalContext).
I had the same issue. It seems to be more related to the <p:commandButton> than the <p:fileUpload> component, since it works with an <h:commandButton> (even with ajax).
You could try:
<h:commandButton id="btnSubmit" actionListener="#{testManagedBean.insert}" value="Save">
<f:ajax execute="#all" render="#form"/>
</h:commandButton>
I cannot tell you why or how it works, but it fixed the issue for me.
The downside of course is, that you have to do the styling by yourself, at least until
the Primefaces guys fix this issue.
EDIT:
After digging in the sources and doing some debugging if figured out that there are actually two requests made (I tried in <p:wizard/>). The first one is the multipart/form-data one which actually does the file upload. It fires the fileUploadEvent in the Bean. I the wizards next button is pressed another form with enctype application/www-urlencoded is submitted. This causes the exception. Conclusion is, that unlike what I wrote in the comment suppressing the exception is a valid solution. This can even be done in a way that does not include changing the Primefaces.jar which is handy if the guys fix the issue in a future version.
So here is what needs to be done:
Create a new class com.yourpackage.fileupload.FileUploadRenderer
Copy and paste the following code inside your new class:
package com.yourpackage.fileupload.fileupload;
import java.io.IOException;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.component.fileupload.CommonsFileUploadDecoder;
import org.primefaces.component.fileupload.FileUpload;
import org.primefaces.component.fileupload.NativeFileUploadDecoder;
import org.primefaces.config.ConfigContainer;
import org.primefaces.context.RequestContext;
import org.primefaces.expression.SearchExpressionFacade;
import org.primefaces.renderkit.CoreRenderer;
import org.primefaces.util.HTML;
import org.primefaces.util.WidgetBuilder;
public class FileUploadRenderer extends CoreRenderer {
#Override
public void decode(FacesContext context, UIComponent component) {
FileUpload fileUpload = (FileUpload) component;
if (!fileUpload.isDisabled()) {
ConfigContainer cc = RequestContext.getCurrentInstance().getApplicationContext().getConfig();
String uploader = cc.getUploader();
boolean isAtLeastJSF22 = cc.isAtLeastJSF22();
if (uploader.equals("auto")) {
if (isAtLeastJSF22) {
if (isMultiPartRequest(context)) {
NativeFileUploadDecoder.decode(context, fileUpload);
}
} else {
CommonsFileUploadDecoder.decode(context, fileUpload);
}
} else if (uploader.equals("native")) {
if (!isAtLeastJSF22) {
throw new FacesException("native uploader requires at least a JSF 2.2 runtime");
}
NativeFileUploadDecoder.decode(context, fileUpload);
} else if (uploader.equals("commons")) {
CommonsFileUploadDecoder.decode(context, fileUpload);
}
}
}
#Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
FileUpload fileUpload = (FileUpload) component;
encodeMarkup(context, fileUpload);
if (fileUpload.getMode().equals("advanced")) {
encodeScript(context, fileUpload);
}
}
protected void encodeScript(FacesContext context, FileUpload fileUpload) throws IOException {
String clientId = fileUpload.getClientId(context);
String update = fileUpload.getUpdate();
String process = fileUpload.getProcess();
WidgetBuilder wb = getWidgetBuilder(context);
wb.initWithDomReady("FileUpload", fileUpload.resolveWidgetVar(), clientId, "fileupload");
wb.attr("auto", fileUpload.isAuto(), false)
.attr("dnd", fileUpload.isDragDropSupport(), true)
.attr("update", SearchExpressionFacade.resolveComponentsForClient(context, fileUpload, update), null)
.attr("process", SearchExpressionFacade.resolveComponentsForClient(context, fileUpload, process), null)
.attr("maxFileSize", fileUpload.getSizeLimit(), Long.MAX_VALUE)
.attr("fileLimit", fileUpload.getFileLimit(), Integer.MAX_VALUE)
.attr("invalidFileMessage", fileUpload.getInvalidFileMessage(), null)
.attr("invalidSizeMessage", fileUpload.getInvalidSizeMessage(), null)
.attr("fileLimitMessage", fileUpload.getFileLimitMessage(), null)
.attr("messageTemplate", fileUpload.getMessageTemplate(), null)
.attr("previewWidth", fileUpload.getPreviewWidth(), 80)
.attr("disabled", fileUpload.isDisabled(), false)
.callback("onstart", "function()", fileUpload.getOnstart())
.callback("onerror", "function()", fileUpload.getOnerror())
.callback("oncomplete", "function()", fileUpload.getOncomplete());
if (fileUpload.getAllowTypes() != null) {
wb.append(",allowTypes:").append(fileUpload.getAllowTypes());
}
wb.finish();
}
protected void encodeMarkup(FacesContext context, FileUpload fileUpload) throws IOException {
if (fileUpload.getMode().equals("simple")) {
encodeSimpleMarkup(context, fileUpload);
} else {
encodeAdvancedMarkup(context, fileUpload);
}
}
protected void encodeAdvancedMarkup(FacesContext context, FileUpload fileUpload) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = fileUpload.getClientId(context);
String style = fileUpload.getStyle();
String styleClass = fileUpload.getStyleClass();
styleClass = styleClass == null ? FileUpload.CONTAINER_CLASS : FileUpload.CONTAINER_CLASS + " " + styleClass;
boolean disabled = fileUpload.isDisabled();
writer.startElement("div", fileUpload);
writer.writeAttribute("id", clientId, "id");
writer.writeAttribute("class", styleClass, styleClass);
if (style != null) {
writer.writeAttribute("style", style, "style");
}
//buttonbar
writer.startElement("div", fileUpload);
writer.writeAttribute("class", FileUpload.BUTTON_BAR_CLASS, null);
//choose button
encodeChooseButton(context, fileUpload, disabled);
if (!fileUpload.isAuto()) {
encodeButton(context, fileUpload.getUploadLabel(), FileUpload.UPLOAD_BUTTON_CLASS, "ui-icon-arrowreturnthick-1-n");
encodeButton(context, fileUpload.getCancelLabel(), FileUpload.CANCEL_BUTTON_CLASS, "ui-icon-cancel");
}
writer.endElement("div");
//content
writer.startElement("div", null);
writer.writeAttribute("class", FileUpload.CONTENT_CLASS, null);
writer.startElement("table", null);
writer.writeAttribute("class", FileUpload.FILES_CLASS, null);
writer.startElement("tbody", null);
writer.endElement("tbody");
writer.endElement("table");
writer.endElement("div");
writer.endElement("div");
}
protected void encodeSimpleMarkup(FacesContext context, FileUpload fileUpload) throws IOException {
encodeInputField(context, fileUpload, fileUpload.getClientId(context));
}
protected void encodeChooseButton(FacesContext context, FileUpload fileUpload, boolean disabled) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String clientId = fileUpload.getClientId(context);
String cssClass = HTML.BUTTON_TEXT_ICON_LEFT_BUTTON_CLASS + " " + FileUpload.CHOOSE_BUTTON_CLASS;
if (disabled) {
cssClass += " ui-state-disabled";
}
writer.startElement("span", null);
writer.writeAttribute("class", cssClass, null);
//button icon
writer.startElement("span", null);
writer.writeAttribute("class", HTML.BUTTON_LEFT_ICON_CLASS + " ui-icon-plusthick", null);
writer.endElement("span");
//text
writer.startElement("span", null);
writer.writeAttribute("class", HTML.BUTTON_TEXT_CLASS, null);
writer.writeText(fileUpload.getLabel(), "value");
writer.endElement("span");
if (!disabled) {
encodeInputField(context, fileUpload, clientId + "_input");
}
writer.endElement("span");
}
protected void encodeInputField(FacesContext context, FileUpload fileUpload, String clientId) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.startElement("input", null);
writer.writeAttribute("type", "file", null);
writer.writeAttribute("id", clientId, null);
writer.writeAttribute("name", clientId, null);
if (fileUpload.isMultiple()) {
writer.writeAttribute("multiple", "multiple", null);
}
if (fileUpload.getStyle() != null) {
writer.writeAttribute("style", fileUpload.getStyle(), "style");
}
if (fileUpload.getStyleClass() != null) {
writer.writeAttribute("class", fileUpload.getStyleClass(), "styleClass");
}
if (fileUpload.isDisabled()) {
writer.writeAttribute("disabled", "disabled", "disabled");
}
writer.endElement("input");
}
protected void encodeButton(FacesContext context, String label, String styleClass, String icon) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String cssClass = HTML.BUTTON_TEXT_ICON_LEFT_BUTTON_CLASS + " ui-state-disabled " + styleClass;
writer.startElement("button", null);
writer.writeAttribute("type", "button", null);
writer.writeAttribute("class", cssClass, null);
writer.writeAttribute("disabled", "disabled", null);
//button icon
String iconClass = HTML.BUTTON_LEFT_ICON_CLASS;
writer.startElement("span", null);
writer.writeAttribute("class", iconClass + " " + icon, null);
writer.endElement("span");
//text
writer.startElement("span", null);
writer.writeAttribute("class", HTML.BUTTON_TEXT_CLASS, null);
writer.writeText(label, "value");
writer.endElement("span");
writer.endElement("button");
}
private boolean isMultiPartRequest(FacesContext context) {
if (context == null) {
return false;
}
return ((HttpServletRequest) context.getExternalContext().getRequest()).getContentType().startsWith("multipart");
}
}
Add the following lines at the bottom of your faces-config.xml:
<render-kit>
<renderer>
<component-family>org.primefaces.component</component-family>
<renderer-type>org.primefaces.component.FileUploadRenderer</renderer-type>
<renderer-class>com.yourpackage.fileupload.FileUploadRenderer</renderer-class>
</renderer>
</render-kit>
You're ready to go!
What have we done? We created our own FileUploadRenderer which checks if the contentType is really multipart/form-data using the method isMultiPartRequest(). Only if this returns true the rest of the code is executed. In any other case nothing will happen which means that no exception will be thrown. If Primefaces fixes this issue you just need to remove the lines from your faces-config.xml to use their class.
Let me know if that works for you!
EDIT
This code checks wether or not the given request is of type multipart/form-data. If it is not the execution is stopped. The original Primefaces code would continue anyway. As I mentioned above if you upload a file inside a Primefaces component there are actually two requests made:
The Ajax-FileUpload using the <p:fileUpload/> (enctype: multipart/form-data)
The Ajax action in the <p:editRow/> or <p:wizard/> (enctype: application/www-form-urlencoded)
The first one is handled by the renderer while the second one causes the exception in the original code since the renderer tries to handle something which it is not capable of. With the changes made in the code only multipart/form-data forms are handled by the renderer so no exceptions occur. IMO it is clearly a bug in the sources of Primefaces. The code differences is just the method private boolean isMultiPartRequest(FacesContext context) and its one occurrence in the code. Glad I could help you!
Although this is old and already answered, i wanted to share something, just in case you missed it:
PrimeFaces 4+ now has a context paramter, which you can use (in web.xml) to manually choose which uploader should be used (native-servlet3 or commons). You can use this to force commons-uploader like this:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
(Of course you still need FileUploadFilter as described above and in the guide).
See PrimeFaces User's Guide for more information.
#BalusC - Your suggestion for extending the existing FileUploadRenderer is very clean. Thanks!
Depending on the version of JSF you'r using you may see a random iFrame pop up. This is a bug noted here: JAVASERVERFACES-2843
In my first attempt to fix this (without having to upgrade to 2.2.1) I just hid the iFrame with CSS.
#JSFFrameId {
visibility:hidden;
}
This worked but for some reason additional AJAX submits would not fire. I then called a little script to remove the iFrame and that fixed the problem.
<h:commandButton id="btnSubmit" action="#{fileUploadController.upload}" value="Save" >
<f:ajax execute="#all" render="frmMain" onevent="removeIFrame()" />
</h:commandButton>
JavaScript:
function removeIFrame()
{
document.getElementById("JSFFrameId").removeNode();
}
I had the same issue,
In my case i have used primefaces file uploader in a data table, tried to modify the existing image using onRowEdit, which was end up with the same error mentioned above.
Then i changed the primefaces jar to 5.1 version. Now its working fine.
The custom class work for me:
#Override
public void decode(FacesContext context, UIComponent component) {
if (!context.getExternalContext().getRequestContentType().toLowerCase().startsWith("multipart/")) {
return;
}
FileUpload fileUpload = (FileUpload) component;
if (!fileUpload.isDisabled()) {
PrimeConfiguration cc = RequestContext.getCurrentInstance().getApplicationContext().getConfig();
String uploader = cc.getUploader();
boolean isAtLeastJSF22 = cc.isAtLeastJSF22();
String inputToDecodeId = getSimpleInputDecodeId(fileUpload, context);
if (uploader.equals("auto")) {
if (isAtLeastJSF22)
NativeFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
else
CommonsFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
}
else if (uploader.equals("native")) {
NativeFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
}
else if (uploader.equals("commons")) {
CommonsFileUploadDecoder.decode(context, fileUpload, inputToDecodeId);
}
}
}

Resources