我在尝试通过 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//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 "";
}
}
控制台输出是:set input ???è??è???????è??è?????? 显示输入 ???è??è?????è??è??????