Search code examples
jsfjsf-2utf-8

Sending UTF-8 message to JSF page


I want to send a message to a JSF page with UTF-8 format, but the <p:message> doesn't show correct characters.
I'm using Tomcat 8.0.28, JSF 2 and Spring.
JSF page:

<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 method:

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;
}  

And this is what I get: ��� �����? ?� ��� ���� ������ ���!

I also guess that the problem is about FacesContext not <p:message>

I've tried these solutions before:
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
But Nothing !!!


Solution

  • as BalusC mentioned, editor's encoding is the problem. In case of Intellij IDEA solution is:

    File -> Settings -> editor -> File encoding