我想用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 Bean方法:
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