1

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 !!!

Community
  • 1
  • 1
Amin
  • 975
  • 8
  • 24
  • 1
    You seem to be sending a string which is hardcoded in a Java class instead of retrieved from a properties file. Is that Java class itself saved using UTF-8? Is that Java class itself read using UTF-8? What if you replace that hardcoded message by this string? `"\u0646\u0627\u0645 \u06A9\u0627\u0631\u0628\u0631\u06CC \u06CC\u0627 \u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u0627\u0634\u062A\u0628\u0627\u0647 \u0627\u0633\u062A\!"` – BalusC Dec 23 '15 at 11:59
  • @BalusC I don't know how I can check the java class format and I replace your string, but it didn't work. By the way thanks for your suggestion – Amin Dec 23 '15 at 16:25
  • actually I removed the last "\" and it worked ! Is there any way to change my string to yours dynamically? – Amin Dec 23 '15 at 16:37
  • 1
    You'd better fix the editor, compiler/builder and runtime to use UTF-8 over all place. Then you can just continue writing Arabic in your source files (although the better practice is to put localized text in resource bundle files and then use `ResourceBundle` API to obtain them, see also: http://stackoverflow.com/q/13655540). First step, which editor are you using? If Eclipse, check if this answer helps: http://stackoverflow.com/q/19169136 – BalusC Dec 23 '15 at 21:31
  • @balusc Thank you so much, I changed the editor setting and my problem solved. I'm using Intellij IDEA 14. The path of this setting is: File -> Settings -> Editor -> File encoding – Amin Dec 24 '15 at 06:55

1 Answers1

1

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

File -> Settings -> editor -> File encoding

Amin
  • 975
  • 8
  • 24