Tomahawk t:inputFileUpload 导致验证错误
Tomahawk t:inputFileUpload results in validation error
我在使用 Tomahawk t:inputFileUpload
组件和 Primefaces 输入组件时遇到问题。
我正在使用这个 Tomahawk 组件,因为 Primefaces 将 "multipart/form-data" 转换为另一个字符集。
当我的表单中有特殊字符(如 ã é ó..)时,p:message 组件正在显示
***Validation Error : the value is not valid***
这里是测试项目
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>
<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>
验证错误来自包含 "special characters" 的下拉菜单。这在相关问答的回答中解释得很清楚:Validation Error: Value is not valid。最相关的部分引用如下:
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.
如果你按逻辑思考,Tomahawk 还是没有解决你的编码问题。去掉它,你不需要它,因为 PrimeFaces 已经提供了与 <p:fileUpload>
.
相同的功能
如果按照上述 link 所述配置字符编码过滤器后,您仍然遇到字符编码问题,那么您很可能使用的服务器存在错误或需要对其进行额外配置 multipart/form-data解析器。
根据特定服务器 bug/configuration 是否可管理,您可能需要切换回 Apache Commons FileUpload。它不会暴露此编码问题。您可以在 How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null 中找到安装说明。基本上,您需要添加两个 JAR,安装一个 servlet 过滤器并将上下文参数 primefaces.UPLOADER
设置为 commons
.
另请参阅:
我在使用 Tomahawk t:inputFileUpload
组件和 Primefaces 输入组件时遇到问题。
我正在使用这个 Tomahawk 组件,因为 Primefaces 将 "multipart/form-data" 转换为另一个字符集。
当我的表单中有特殊字符(如 ã é ó..)时,p:message 组件正在显示
***Validation Error : the value is not valid***
这里是测试项目
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>
<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>
验证错误来自包含 "special characters" 的下拉菜单。这在相关问答的回答中解释得很清楚:Validation Error: Value is not valid。最相关的部分引用如下:
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.
如果你按逻辑思考,Tomahawk 还是没有解决你的编码问题。去掉它,你不需要它,因为 PrimeFaces 已经提供了与 <p:fileUpload>
.
如果按照上述 link 所述配置字符编码过滤器后,您仍然遇到字符编码问题,那么您很可能使用的服务器存在错误或需要对其进行额外配置 multipart/form-data解析器。
根据特定服务器 bug/configuration 是否可管理,您可能需要切换回 Apache Commons FileUpload。它不会暴露此编码问题。您可以在 How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null 中找到安装说明。基本上,您需要添加两个 JAR,安装一个 servlet 过滤器并将上下文参数 primefaces.UPLOADER
设置为 commons
.