如何在jsf 2上显示成功消息

时间:2012-09-05 13:10:20

标签: jsf-2

如何显示成功消息?我有以下代码,但只显示警告消息。我需要更改什么才能在成功页面中显示成功消息?

userbean.java

  public String Login() throws Exception {
     String status = "failure"; 
    current=userBo.validateUser(getLogin(),getPass()); 
    if(current!=null){ exist=false; status = "success"; 
    String message = "submitted successfully !!"; 
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message)); }
     exist=true; return status; }

login.xhtml:

<f:view>
                    <h:form>
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <th>Username</th>
                                <td><h:inputText value="#{user.login}"
                                        styleClass="login-inp" /></td>
                            </tr>
                            <tr>
                                <th>Password</th>
                                <td><h:inputSecret value="#{user.pass}"
                                        onfocus="this.value=''" styleClass="login-inp" /></td>
                            </tr>
                            <tr>
                                <th></th>
                                <!-- <td><input type="button" class="submit-login"  /> -->
                                <td><h:commandButton action="#{user.checkUser}"  styleClass="submit-login" />
                                <h:messages  globalOnly="true"  />
                                </td>
                            </tr>
                        </table>
                    </h:form>
                </f:view>

success.xhtml:

 <h:form>
    <h:body>

        <h1>heyyyy ,y sucseded  !!!</h1> <br/><br/>

        <h:commandLink  action ="#{user.logout}" > disconnect </h:commandLink>

    </h:body>
    </h:form>

警告信息:

Infos: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=null[severity=(INFO 0), summary=(submitted successfully !!), detail=(submitted successfully !!)]

2 个答案:

答案 0 :(得分:2)

您的<h:messages globalOnly="true" />被放置在错误的视图中。您已将其放入login.xhtml,但您在成功时导航到success.xhtml,而不是返回login.xhtml。如果您将<h:messages globalOnly="true" />放入success.xhtml,那么它将以您期望的方式显示。

答案 1 :(得分:0)

在您的操作方法上,即action =“#{user.checkUser}”返回一个字符串,该字符串将决定使用faces-config.xml文件显示哪个页面。

例如。         &LT;导航规则&GT;

        < from-view-id>*< /from-view-id>                
            < navigation-case>
               <from-outcome>ReturnedString</from-outcome>
               <to-view-id>success.xhtml(path of your file)</to-view-id>
            <redirect/>
        </navigation-case>
    < /navigation-rule>

这肯定会有效。