Problema FileUpload PrimeFaces

3 respostas
A

Quando utilizo o componente de upload do primefaces ao selecionar o arquivo ele não chama o evento no managedbean.
Já pesquisei em vários foruns e no google e tentei de tudo, mas nada solucionou.

Alguém pode me ajudar?

Tenho as duas libs já:
commons-fileupload-1.2.2.jar
commons-io-2.4.jar

pagina
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">

<ui:composition template="../../templates/admin.xhtml">
	<ui:define name="conteudo">
		<h1>Administração</h1>
		<br />
		<h:form id="habilidadeForm"  enctype="multipart/form-data">
			<p:messages autoUpdate="true" />

			<h:panelGrid columns="2">

				<p:outputLabel value="#{msg.nome}" for="nome" />
				<p:inputText id="nome" value="#{habilidadeMB.habilidade.nome}"
					required="true" requiredMessage="#{msg.nomeRequired}"
					maxlength="20" />

				<p:outputLabel value="#{msg.descricao}" for="descricao" />
				<p:inputTextarea id="descricao"
					value="#{habilidadeMB.habilidade.descricao}" required="true"
					requiredMessage="#{msg.descricaoRequired}" />
					
				<p:outputLabel value="#{msg.imagem}" for="imagem" />
				<p:fileUpload id="imagem" fileUploadListener="#{habilidadeMB.uploadAction}"
					auto="true" />	

				<p:commandButton id="btnSalvarHabilidade" value="#{msg.salvar_habilidade}"
					update=":habilidadeForm :formHabilidade"
					oncomplete="handleCompleteEvent(args)"
					action="#{habilidadeMB.salvar}" />

				<p:commandButton id="btnFecharHabilidade" value="#{msg.fechar}"
					update=":habilidadeForm :formHabilidade"
					onclick="registroHabilidadeDlg.hide()" icon="ui-icon-circle-close"
					action="#{habilidadeMB.limpar}" />

			</h:panelGrid>
		</h:form>
	
		<script type="text/javascript">
			function handleCompleteEvent(args){if(args.success)registroHabilidadeDlg.hide();}
		</script>
	</ui:define>
</ui:composition>
</html>
método no managedbean
public void uploadAction (FileUploadEvent event) throws IOException {
		System.out.println("FileUploadEvent event");
		this.arquivo.fileUpload(event, ".jpg", "/images/habilidades/");
		this.habilidade.setImagem(this.arquivo.getNome());
	}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Jogo</display-name>
  
	<context-param>  
		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>  
		<param-value>server</param-value>  
	 </context-param> 
	 
	<welcome-file-list>
	    <welcome-file>index.jsf</welcome-file>
	</welcome-file-list>
    
  	<!-- File Upload -->
 	<filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>uploadDirectory</param-name>
            <param-value>/tmp</param-value>
        </init-param>
    </filter>
    <filter-mapping>  
        <filter-name>PrimeFaces FileUpload Filter</filter-name>  
        <servlet-name>Faces Servlet</servlet-name>  
    </filter-mapping> 

    <!-- Faces 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>/faces/*</url-pattern>
		<url-pattern>*.jsf</url-pattern>
		<url-pattern>*.xhtml</url-pattern>
	</servlet-mapping>
classe de upload (http://meninajava.blogspot.com.br/2012/01/fileupload-com-jsf-primefaces-hibernate.html)
public class UploadArquivo {
	private String diretorio;
	private String caminho;
	private byte[] arquivo;
	private String nome;

	public UploadArquivo() {
	}

	public String getNome() {
		return nome;
	}

	public String getRealPath() {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		HttpServletResponse response = (HttpServletResponse) externalContext
				.getResponse();

		FacesContext aFacesContext = FacesContext.getCurrentInstance();
		ServletContext context = (ServletContext) aFacesContext
				.getExternalContext().getContext();

		return context.getRealPath("/");
	}

	public void fileUpload(FileUploadEvent event, String type, String diretorio) {
		try {
			this.nome = new java.util.Date().getTime() + type;
			this.caminho = getRealPath() + diretorio + getNome();
			this.arquivo = event.getFile().getContents();

			File file = new File(getRealPath() + diretorio);
			file.mkdirs();

		} catch (Exception ex) {
			System.out.println("Erro no upload do arquivo" + ex);
		}
	}

	public void gravar() {

		try {

			FileOutputStream fos;
			fos = new FileOutputStream(this.caminho);
			fos.write(this.arquivo);
			fos.close();

		} catch (Exception ex) {
			System.out.println(ex);
		}

	}

	public String getDiretorio() {
		return diretorio;
	}

	public void setDiretorio(String diretorio) {
		this.diretorio = diretorio;
	}

	public String getCaminho() {
		return caminho;
	}

	public void setCaminho(String caminho) {
		this.caminho = caminho;
	}

	public byte[] getArquivo() {
		return arquivo;
	}

	public void setArquivo(byte[] arquivo) {
		this.arquivo = arquivo;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}
}

3 Respostas

kilate

Olá amigo, adicione o URL PATTERN no filter-mapping do filtro de upload do primefaces:

<url-pattern>*.jsf</url-pattern>

Abraços!

A

Fiz isso, mas não adiantou ! :frowning:

<filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> <init-param> <param-name>uploadDirectory</param-name> <param-value>/tmp</param-value> </init-param> </filter> <filter-mapping> <filter-name>PrimeFaces FileUpload Filter</filter-name> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </filter-mapping>

A

Alguém?? :confused:

Criado 11 de setembro de 2012
Ultima resposta 12 de set. de 2012
Respostas 3
Participantes 2