0
Greetings to you all, I was trying to implement upload in my JSF2 project, but in the act of clicking the save button appeared this message below;
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded; charset=UTF-8
at org.apache.catalina.connector.Request.parseParts(Request.java:2794)
at org.apache.catalina.connector.Request.getParts(Request.java:2641)
at org.apache.catalina.connector.RequestFacade.getParts(RequestFacade.java:1083)
at com.sun.faces.renderkit.html_basic.FileRenderer.decode(FileRenderer.java:91)
... 34 more
The error message indicates that the form required the presence of enctype="Multipart/form-date"
I couldn’t understand why in the form I had already put the doctype in the tag form as shown below;
<ui:composition template="/WEB-INF/template/LayoutSystem.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<!-- xmlns:h="http://java.sun.com/jsf/html" -->
<ui:define name="titulo">Novas Notícias</ui:define>
<ui:define name="corpo">
<h:form id="form" enctype="multipart/form-data">
<h1>Novas Notícias</h1>
<p:messages autoUpdate="true" closable="true" />
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:button value="Novo" />
<p:commandButton value="Salvar" id="botaoSalvar"
action="#{cadastroNoticiaBean.salvar}">
<f:ajax execute="@form" render="@all"/>
</p:commandButton>
</p:toolbarGroup>
<p:toolbarGroup align="right">
<p:button value="Pesquisa"
outcome="/noticias/PesquisaNoticias.xhtml" />
</p:toolbarGroup>
</p:toolbar>
<div id="wrap">
<div class="left-sidebar">
<p:panelGrid columns="2" id="painel1"
style="width: 50%; margin-top: 20px" columnClasses="rotulo, campo">
<p:outputLabel value="Titulo" for="titulo" />
<p:inputText id="titulo" size="20" maxlength="50"
value="#{cadastroNoticiaBean.noticia.titulo_noticia}" />
<p:outputLabel value="Data" for="data_noticia" />
<p:calendar id="data_noticia" pattern="dd/MM/yyyy"
value="#{cadastroNoticiaBean.noticia.data_noticia}" />
<p:outputLabel value="Descrição" for="descricao" />
<p:inputText id="descricao" size="20" maxlength="250"
value="#{cadastroNoticiaBean.noticia.desc_noticia}" />
<p:outputLabel value="Foto" />
<h:inputFile value="#{cadastroNoticiaBean.arquivo}" />
</p:panelGrid>
</div>
</div>
</h:form>
</ui:define>
</ui:composition>
I tried several ways to solve this problem, so I found this search reference;
http://blog.triadworks.com.br/upload-de-arquivos-com-jsf-2-2
So I decided to bring the following solution below;
I SWITCHED THAT
<!-- p: é a referencia do PrimeFaces xmlns:p="http://primefaces.org/ui" -->
<p:commandButton value="Salvar" id="botaoSalvar"
action="#{cadastroNoticiaBean.salvar}">
<f:ajax execute="@form" render="@all"/>
</p:commandButton>
FOR THAT
<!-- p: é a referencia do jsf xmlns:h="http://xmlns.jcp.org/jsf/html" -->
<h:commandButton value="Salvar" id="botaoSalvar"
action="#{cadastroNoticiaBean.salvar}">
<f:ajax execute="@form" render="@all"/>
</h:commandButton>
p: is from Primefaces and h: is from JSF, when I switched "p:" with "h:" picked up normally and successfully uploaded to the folder.
The strange thing is that it worked even the XHTML page presents errors as shown in the figure below;
My question is this: There’s a way I can upload using Primefaces Frameworks?
How do I use Primefaces Frameworks to upload and work?
Why did it only work with JSF2 Frameworks?
Using in that way;
<p:fileUpload
fileUploadListener="#{cadastroNoticiaBean.arquivo}"
mode="advanced" dragDropSupport="false" update="messages"
sizeLimit="100000" fileLimit="3"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
makes that mistake;
jul 30, 2015 10:37:41 AM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
GRAVE: Error Rendering View[/noticias/CadastroNoticias.xhtml]
javax.faces.FacesException: Cannot find component for expression "messages" referenced from "form:j_idt41".
at org.primefaces.expression.SearchExpressionFacade.cannotFindComponent(SearchExpressionFacade.java:688)
at org.primefaces.expression.SearchExpressionFacade.resolveComponentById(SearchExpressionFacade.java:506)
at org.primefaces.expression.SearchExpressionFacade.resolveClientIds(SearchExpressionFacade.java:256)
at org.primefaces.expression.SearchExpressionFacade.resolveClientIds(SearchExpressionFacade.java:154)
at org.primefaces.component.fileupload.FileUploadRenderer.encodeScript(FileUploadRenderer.java:84)
at org.primefaces.component.fileupload.FileUploadRenderer.encodeEnd(FileUploadRenderer.java:72)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:131)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeTableBody(PanelGridRenderer.java:102)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeTableLayout(PanelGridRenderer.java:65)
at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:37)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Take a look at these Primefaces Docs that I think they’ll solve your question: Simple file upload and Basic file upload
– Armando K.
I tried that, but it didn’t work out.
– wladyband
Some error appears using these links I gave you?
– Armando K.
I just put in the post.
– wladyband
The error that Mosta is that you have no tag with the id messages. And another thing that I don’t know if it will generate more errors is that instead of using
cadastroNoticiaBean.arquivo
you should do a method that would receive the requisicao, as in the example.– Armando K.