如何在jsf 2中显示消息?

时间:2015-01-19 10:50:06

标签: jsf

在login.xhml页面中我有这段代码:

<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"/>

和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."));
        }
      }  
}

如何在cpatcha时显示消息? 例如:cpatcha true show&#34; Hello,Human&#34;或cpatcha false show&#34;你好Roboto先生。再试一次。&#34;

1 个答案:

答案 0 :(得分:1)

我认为您必须更改bean中消息的规范,您需要指定表单和消息标记,尝试在您的bean中更改此代码:

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

以下内容:

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

yourForm作为您表单的ID。