将UTF-8消息发送到JSF页面

时间:2015-12-23 11:36:18

标签: jsf jsf-2 utf-8

我想以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
但没什么!!!

1 个答案:

答案 0 :(得分:0)

正如BalusC所说,编辑器的编码是问题所在。如果是Intellij IDEA解决方案,则为:

File -> Settings -> editor -> File encoding