Search code examples
jsffile-uploadprimefacescharacter-encodingtomahawk

Tomahawk t:inputFileUpload results in validation error


I have a problem using the Tomahawk t:inputFileUpload component together with Primefaces input components.

I'm using this Tomahawk component because Primefaces converts "multipart/form-data" to another charset.

When i have special chars (like ã é ó..) in my form the p:message component is displaying ***Validation Error : the value is not valid***

Here is the test project

web.xml

<display-name>TESTE_ANEXO</display-name>
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

<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>*.xhtml</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
    <init-param>
        <param-name>uploadMaxFileSize</param-name>
        <param-value>20m</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

index.xhtml

<h:head>
    <title><h:outputText value="#{msg.welcomeTitle}" /></title>
</h:head>

<body>
<h:form enctype="multipart/form-data" acceptcharset="UTF-8">
    <p:messages autoUpdate="true"/>

    <p:selectOneMenu immediate="true" label="teste" id="teste">
        <f:selectItem itemLabel="ããã" itemValue="ããã" />
    </p:selectOneMenu>

    <t:inputFileUpload id="fileupload" value="#{meuBean.arquivo}" /> 
    <h:commandButton value="Enviar"  action="#{meuBean.enviar}" />
</h:form>

MeuBean.java

@ManagedBean
public class MeuBean {

private UploadedFile arquivo;

public UploadedFile getArquivo() {
    return arquivo;
}

public void setArquivo(UploadedFile arquivo) {
    this.arquivo = arquivo;
}

public String enviar() {
    System.out.println("AA");
    System.out.println("Nome do arquivo enviado: " + arquivo.getName());
    System.out.println("Tipo do arquivo enviado: "
            + arquivo.getContentType());
    System.out.println("Tamanho do arquivo enviado: " + arquivo.getSize());

    return "ok";
}
}

Dependency Versions

<dependency>
    <groupId>org.apache.myfaces.tomahawk</groupId>
    <artifactId>tomahawk21</artifactId>
    <version>1.1.14</version>
</dependency>

<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.2.5</version>
</dependency>

<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>5.1</version>
</dependency>

Solution

  • The validation error is coming from your dropdown menu containing "special characters". This is clearly explained in the answer of this related Q&A: Validation Error: Value is not valid. Most relevant part is cited below:

    In case you're already using String, make sure that the request character encoding is configured right. If it contains special characters and JSF is configured to render the output as UTF-8 but interpret the input as e.g. ISO-8859-1, then it will fail. See also a.o. Unicode input retrieved via PrimeFaces input components become corrupted.

    If you think logically, Tomahawk has still not solved your encoding problem. Get rid of it, you don't need it as PrimeFaces already offers the same functionality with <p:fileUpload>.

    If you still face the character encoding problem after configuring the character encoding filter as described in the aforementioned link, then you're most likely using a server which has a bug or requires additional configuration for its multipart/form-data parser.

    Depending on whether the server specific bug/configuration is manageable, you may need to switch back to Apache Commons FileUpload. It doesn't expose this encoding problem. You can find installation instructions in How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null. Basically, you need to add two JARs, install a servlet filter and set context param primefaces.UPLOADER to commons.

    See also: