繁体   English   中英

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

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

你好,

我在NetBeans 7.4中开发了一个JSF2.2 Web应用程序。 它在Apache Tomcat 7.0.50上运行良好。 但是现在我切换到了GlassFish 4,并且遇到了一些塞尔维亚字符( Ć,Č,Š,Đ,Ž ...)的问题

MySQL数据库具有utf8_general_ci归类,以及所有表和每个varchar列。

连接网址:

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

web.xml具有以下部分:

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

我在其中显示表格内容的supporters.xhtml文件:

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

SupportersBean.java中,我生成一个列表,出于调试目的,我将打印结果:

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

为了进行测试,我在表中输入了一个名称name =Ć (在MySQL中正确显示)。 在supporters.xhtml页面上,名称也为Ć ,但println给出: Ć,Ć

为了编辑条目,我在supporters.xhtml页面上创建了一个commandLink,并执行以下操作:

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

...然后我给了它一个参数:

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

这是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";
}

println给我:newName = Ć

editSupporters.xhtml部分:

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

在此字段中,我看到字符Ä 我在这里没有任何更改,仅单击“保存”按钮,该按钮将执行以下操作:

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

println提供:更新: ÄÃ,†

之后,我的supporters.xhtml页面上的outputField的值为Â 如果我再次点击编辑,该inputText字段的值将为:AAAA,当我点击保存,该outputField将AAAAAAAA ...

最后,在进行了两次编辑/保存之后,我通过phpMyAdmin检查了我的表。 列中的值为: Ã

有谁知道这可能是什么问题???

我在这里找到了答案: https : //stackoverflow.com/a/22614044/1168874

我应该将其添加到glassfish-web.xml文件中:

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

Java中的println仍然给我一些怪异的字符,但是只要数据库内容正确且网页显示正常,都没有关系。

此解决方案的所有功劳归于Pavel

暂无
暂无

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

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