Mohammad
Mohammad

Reputation: 45

How to show message in jsf 2?

In the login.xhml page i have this code:

<h:inputText id="it1" value="#{requestScope.bestGuess}"/>
<p />
<h:message for="it1" />
<p />
<h:commandButton value="try" id="cb1" actionListener="#   {LoginBean.verifyAnswer}" Submit="true" immediate="false"/>

and this code in LoginBean :

public class LoginBean {
    public void verifyAnswer(ActionEvent actionEvent) {
        FacesContext fctx = FacesContext.getCurrentInstance();
        ExternalContext ectx =  fctx.getExternalContext();
        HttpServletRequest request = (HttpServletRequest) ectx.getRequest();
        Captcha captcha = (Captcha) ectx.getSessionMap().get(Captcha.NAME);
        try {
          request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
          //bad luck - but ignore
          System.out.println("UTF not supported !");
        }
        String answer = (String) ectx.getRequestMap().get("bestGuess");
        if (answer != null && captcha.isCorrect(answer)){
          /*message("Hello, Human");*/
          fctx.addMessage("it1",new FacesMessage("Hello, Human"));
        }
        else{
          /*message("Hello Mr. Roboto. Try again.");*/
          fctx.addMessage("it1",new FacesMessage("Hello Mr. Roboto. Try again."));
        }
      }  
}

How to show message when cpatcha? for example : cpatcha true show "Hello, Human" or cpatcha false show "Hello Mr. Roboto. Try again."

Upvotes: 0

Views: 108

Answers (1)

cнŝdk
cнŝdk

Reputation: 32145

I think you have to change the specification of the message in your bean, you need to specify the form and the message tag, try to change this code in your bean :

fctx.addMessage("it1",new FacesMessage("Hello Mr. Roboto. Try again."));

With the following:

fctx.addMessage("yourForm:it1",new FacesMessage("Hello Mr. Roboto. Try again."));

with yourForm as the id of your form.

Upvotes: 1

Related Questions