简体   繁体   English

GlassFish + MySQL + XHTML + Java Beans + UTF-8问题

[英]GlassFish + MySQL + XHTML + Java Beans + UTF-8 issue

Hello, 你好,

I developed a JSF2.2 web app in NetBeans 7.4. 我在NetBeans 7.4中开发了一个JSF2.2 Web应用程序。 It worked great on Apache Tomcat 7.0.50. 它在Apache Tomcat 7.0.50上运行良好。 But now I switched to GlassFish 4 and I have a problem with some Serbian characters ( Ć, Č, Š, Đ, Ž ...) 但是现在我切换到了GlassFish 4,并且遇到了一些塞尔维亚字符( Ć,Č,Š,Đ,Ž ...)的问题

MySQL database has utf8_general_ci collation, as well as all the tables and every varchar column. MySQL数据库具有utf8_general_ci归类,以及所有表和每个varchar列。

Connection URL: 连接网址:

jdbc:mysql://localhost:3306/sportal?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8

web.xml has the following part: web.xml具有以下部分:

<context-param>
<param-name>PARAMETER_ENCODING</param-name>
<param-value>UTF-8</param-value>
</context-param>

supporters.xhtml file where I display the table content: 我在其中显示表格内容的supporters.xhtml文件:

<h:dataTable id="listSupportersTable" value="#{supportersBean.list}"...>
...
<h:outputText value="#{row.name}"/>
...
</h:dataTable>

In SupportersBean.java I generate a list and for debugging purposes I print the result: SupportersBean.java中,我生成一个列表,出于调试目的,我将打印结果:

...
while (resultSet.next()) {
supporter.setName(resultSet.getString("name"));
System.out.println(resultSet.getString("name") + ", " + supporter.getName());
list.add(supporter)
}
...

For testing purposes, I made one entry in the table, name=Ć (and it displays correctly in MySQL). 为了进行测试,我在表中输入了一个名称name =Ć (在MySQL中正确显示)。 On the supporters.xhtml page, the name is also Ć , but the println gives: Ć, Ć 在supporters.xhtml页面上,名称也为Ć ,但println给出: Ć,Ć

For editing the entry, I created a commandLink on the supporters.xhtml page, with action: 为了编辑条目,我在supporters.xhtml页面上创建了一个commandLink,并执行以下操作:

action="#{supportersBean.editSelected(event)}"

...and I gave it a parameter: ...然后我给了它一个参数:

<f:param name="supportersName" value="#{row.name}"/>

Here's the editSelected procedure: 这是editSelected过程:

public String editSelected(AjaxBehaviorEvent event) {
newName=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("supportersName");
System.out.println("newName="+newName); // for debugging
isEditing = true;
return "editSupporters.xhtml?faces-redirect=true";
}

The println gives me: newName= Ć println给我:newName = Ć

The editSupporters.xhtml part: editSupporters.xhtml部分:

<h:inputText value="#{supportersBean.newName}"/>

In this field I see the character Ä . 在此字段中,我看到字符Ä I don't change anything here, I only click the Save button, which does the following: 我在这里没有任何更改,仅单击“保存”按钮,该按钮将执行以下操作:

supporter.setName(newName);
System.out.println("Updated: "+newName);
dao.update(supporter);

The println gives: Updated: Ć println提供:更新: ÄÃ,†

After this, the outputField on my supporters.xhtml page has the value ÃÂ . 之后,我的supporters.xhtml页面上的outputField的值为Â If I click Edit again, the inputText field will have the value: ÃÂÃÂ , and when I click Save, the outputField will be ÃÂÃÂÃÂÃÂ ... 如果我再次点击编辑,该inputText字段的值将为:AAAA,当我点击保存,该outputField将AAAAAAAA ...

Finally, after those two edit/saves, I check my table via phpMyAdmin. 最后,在进行了两次编辑/保存之后,我通过phpMyAdmin检查了我的表。 The value in the column is: ÃÂ 列中的值为: Ã

Does anyone has any idea what the problem could be??? 有谁知道这可能是什么问题???

I found the answer here: https://stackoverflow.com/a/22614044/1168874 我在这里找到了答案: https : //stackoverflow.com/a/22614044/1168874

I should add this to the glassfish-web.xml file: 我应该将其添加到glassfish-web.xml文件中:

<parameter-encoding default-charset="UTF-8"/>

The println's in Java still give me some weird characters, but it doesn't matter as long as the database content is correct and the web page display is fine. Java中的println仍然给我一些怪异的字符,但是只要数据库内容正确且网页显示正常,都没有关系。

All the credits for this solution go to Pavel 此解决方案的所有功劳归于Pavel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM