JSF输入的中文字符编码 [英] Chinese Character Encoding of JSF input

查看:122
本文介绍了JSF输入的中文字符编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过JSF和Hibernate输入并保存中文字符串到MySQL时遇到问题。实际上,我使用System.out.print并在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆