[英]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.