我有一个简单的JSF表单,我提交用户名和密码并保存到数据库。
仅在第一次提交表单时,我遇到UTF-8字符问题。当我在第一篇文章中提交ğğüüçç时,我会遇到错误的字符。在第二次尝试中,没关系。
这是我的web.xml和index.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<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>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
和我的index.xhtml:
<!DOCTYPE html>
<html lang="tr"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<div class="container">
<h:form styleClass="form-signin" prependId="false" id="registirationForm">
<h2 class="form-signin-heading">Please Register!</h2>
<h:inputText styleClass="input-block-level" value="#{registirationFormBean.nickname}" />
<h:inputSecret styleClass="input-block-level" value="#{registirationFormBean.password}"/>
<h:commandButton styleClass="btn btn-large btn-primary" value="Register Me!" action="#{registirationFormBean.registerUser}"/>
</h:form>
</div>
</h:body>
</html>
编辑:这是来自Tomcat中的server.xml
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="UTF-8"
/>
当我调试我的应用程序时,在第一篇文章中我看到了发布的值:
但是第二次:
答案 0 :(得分:5)
使用
<f:view>
和您的语言区域。
<f:view locale="#{registirationFormBean.locale}" encoding="UTF-8" contentType="text/html">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<div class="container">
<h:form styleClass="form-signin" prependId="false" id="registirationForm">
<h2 class="form-signin-heading">Please Register!</h2>
<h:inputText styleClass="input-block-level" value="#{registirationFormBean.nickname}" />
<h:inputSecret styleClass="input-block-level" value="#{registirationFormBean.password}"/>
<h:commandButton styleClass="btn btn-large btn-primary" value="Register Me!" action="#{registirationFormBean.registerUser}"/>
</h:form>
</div>
</h:body>
</f:view>
从您的bean中提供区域设置对象,如。
public Locale getLocale(){
return new Locale("tr", "TR");
}