将 UTF-8 消息发送到 JSF 页面
Sending UTF-8 message to JSF page
我想向 UTF-8
格式的 JSF 页面发送消息,但是 <p:message>
没有显示正确的字符。
我正在使用 Tomcat 8.0.28、JSF 2 和 Spring.
JSF 页面:
<h:form id="loginForm">
<p:messages id="messages" style="color:red;margin:8px;" autoUpdate="true"/>
<p:outputLabel value="Username:"/>
<p:inputText id="usernameInput" value="#{userMgmtBean.username}"/>
<p:outputLabel value="Password:"/>
<h:inputSecret id="passwordInput" value="#{userMgmtBean.password}"/>
<p:commandButton value="Login" action="#{userMgmtBean.authenticate}"/>
</h:form>
java豆类方法:
public String authenticate(){
String username = this.username;
String password = Hash.createHash(this.password);
FacesContext context = FacesContext.getCurrentInstance();
if(username != null && password != null && !username.equals("")) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getUsername().equals(username) && users.get(i).getPassword().equals(password)) {
Timestamp lastLogin = users.get(i).getLastLogin();
int numberOfLogin = users.get(i).getNumberOfLogin();
users.get(i).setNumberOfLogin(++numberOfLogin);
users.get(i).setLastLogin(Timestamp.valueOf(LocalDateTime.now()));
service.update(users.get(i));
context.getExternalContext().getSessionMap().put("username", username);
context.getExternalContext().getSessionMap().put("numberOfLogin", numberOfLogin);
context.getExternalContext().getSessionMap().put("lastLogin", lastLogin);
return "/pages/Home.xhtml?faces-redirect=true";
}
}
}
context.addMessage(null, new FacesMessage("نام کاربری یا رمز عبور اشتباه است!"));
return null;
}
这就是我得到的:
��� �����? ?� ��� ���� ������ ���!
我也猜想问题出在 FacesContext
而不是 <p:message>
我以前尝试过这些解决方案:
How to set JSF message encoding to UTF-8?
i18n with UTF-8 encoded properties files in JSF 2.0 appliaction
UTF-8 encoding of GET parameters in JSF
但是什么都没有!!!
如 BalusC 所述,编辑器的编码是问题所在。如果是Intellij IDEA解决方案是:
File -> Settings -> editor -> File encoding
我想向 UTF-8
格式的 JSF 页面发送消息,但是 <p:message>
没有显示正确的字符。
我正在使用 Tomcat 8.0.28、JSF 2 和 Spring.
JSF 页面:
<h:form id="loginForm">
<p:messages id="messages" style="color:red;margin:8px;" autoUpdate="true"/>
<p:outputLabel value="Username:"/>
<p:inputText id="usernameInput" value="#{userMgmtBean.username}"/>
<p:outputLabel value="Password:"/>
<h:inputSecret id="passwordInput" value="#{userMgmtBean.password}"/>
<p:commandButton value="Login" action="#{userMgmtBean.authenticate}"/>
</h:form>
java豆类方法:
public String authenticate(){
String username = this.username;
String password = Hash.createHash(this.password);
FacesContext context = FacesContext.getCurrentInstance();
if(username != null && password != null && !username.equals("")) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getUsername().equals(username) && users.get(i).getPassword().equals(password)) {
Timestamp lastLogin = users.get(i).getLastLogin();
int numberOfLogin = users.get(i).getNumberOfLogin();
users.get(i).setNumberOfLogin(++numberOfLogin);
users.get(i).setLastLogin(Timestamp.valueOf(LocalDateTime.now()));
service.update(users.get(i));
context.getExternalContext().getSessionMap().put("username", username);
context.getExternalContext().getSessionMap().put("numberOfLogin", numberOfLogin);
context.getExternalContext().getSessionMap().put("lastLogin", lastLogin);
return "/pages/Home.xhtml?faces-redirect=true";
}
}
}
context.addMessage(null, new FacesMessage("نام کاربری یا رمز عبور اشتباه است!"));
return null;
}
这就是我得到的:
��� �����? ?� ��� ���� ������ ���!
我也猜想问题出在 FacesContext
而不是 <p:message>
我以前尝试过这些解决方案:
How to set JSF message encoding to UTF-8?
i18n with UTF-8 encoded properties files in JSF 2.0 appliaction
UTF-8 encoding of GET parameters in JSF
但是什么都没有!!!
如 BalusC 所述,编辑器的编码是问题所在。如果是Intellij IDEA解决方案是:
File -> Settings -> editor -> File encoding