JSF输入的中文字符编码 [英] Chinese Character Encoding of JSF input
本文介绍了JSF输入的中文字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
index.xhtml
<?xml version ='1.0'encoding =' UTF-8'?>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://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>
< h:头>
< title> Facelet标题< / title>
< / h:头>
< h:body>
< h:form>
< h:panelGrid columns =2>
< h:outputText value =Input/>
< h:inputText value =#{showBean.input}/>
< / h:panelGrid>
< / h:表格>
< / h:body>
< / html>
ShowBean.java
import javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
公共类showBean {
字符串输入;
public showBean(){
input = null;
}
public String getInput(){
return input;
}
public void setInput(String input){
System.out.println(set input+ input);
this.input = input;
public String show(){
System.out.println(show input+ input);
return;
控制台输出为:
set input ???è??è?????è??è?????
show input ??? ???????? ??????????????????????????????????????????????? p>如果您使用glassfish,只需将以下代码添加到glassfish-web.xml中即可。
< glassfish-web-app>
< parameter-encoding default-charset =UTF-8/>
< / glassfish-web-app>
I am having a problem when trying to input and save a Chinese string into MySQL via JSF and Hibernate.
Actually, I used "System.out.print" and detected garbled words happened, after typing 我(me) in JSF input field but before saving to database. Here is part of the code:
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!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">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Input"/>
<h:inputText value="#{showBean.input}" />
</h:panelGrid>
<h:commandLink action="#{showBean.show()}" value="Show"/>
</h:form>
</h:body>
</html>
ShowBean.java
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class showBean {
String input;
public showBean() {
input = null;
}
public String getInput() {
return input;
}
public void setInput(String input) {
System.out.println("set input " + input);
this.input = input;
}
public String show(){
System.out.println("show input " + input);
return "";
}
}
The console output is :
set input ???è??è?????è??è?????
show input ???è??è?????è??è?????
解决方案 If you use glassfish just add the following code to glassfish-web.xml
<glassfish-web-app>
<parameter-encoding default-charset="UTF-8" />
</glassfish-web-app>
这篇关于JSF输入的中文字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文