根据FacesMessage </p:growl>动态更改<p:growl sticky =“”>属性

时间:2014-09-29 10:58:23

标签: jsf jsf-2 primefaces growl

以下是PrimeFaces展示的关于<p:growl>标记的示例代码。在此示例中,当您单击&#34;保存&#34;按钮,出现两条消息&#34;成功&#34;和#34; SecondMessage&#34;。

link就是我所说的。

这是PrimeFaces Showcase的示例代码

<h:form> 
    <p:growl id="growl" showDetail="true" sticky="true" />  

    <p:panel header="Growl">  
        <h:panelGrid columns="2" cellpadding="5">  
            <p:outputLabel for="msg" value="Message:" />   
            <p:inputText id="msg" 
                         value="#{growlView.message}" 
                         required="true" />  
        </h:panelGrid>  

        <p:commandButton value="Save" 
                         actionListener="#{growlView.saveMessage}" 
                         update="growl" />  
    </p:panel> 
</h:form>

来自PrimeFaces的bean GrowlView.java再次展示。

@ManagedBean
public class GrowlView {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void saveMessage() {
        FacesContext context = FacesContext.getCurrentInstance();

        context.addMessage(null, new FacesMessage("Successful",  "Your message: " + message) );
        context.addMessage(null, new FacesMessage("Second Message", "Additional Message Detail"));
    }
}

我的问题是,我怎样才能做出成功的&#34; message&quot; sticky = true,SecondMessage&s; sticky = false?

我尝试用两个<p:growl>标签来改变第二个咆哮位置和粘性。但是如果你关闭第一个咆哮,第二个咆哮的位置不会像PrimeFaces Showcase示例那样改变

1 个答案:

答案 0 :(得分:1)

这是修改后的示例代码:

<h:form id="growlForm">
    <p:growl id="growl1" showDetail="true" sticky="true" />
    <p:growl id="growl2" showDetail="true" sticky="false" />

    <p:panel header="Growl">
        <h:panelGrid columns="2" cellpadding="5">
            <p:outputLabel for="msg" value="Message:" />
            <p:inputText id="msg" value="#{growlView.message}"
                required="true" />
        </h:panelGrid>

        <p:commandButton value="Save"
            actionListener="#{growlView.saveMessage}" update="growl1 growl2" />
    </p:panel>
</h:form>

saveMethod:

public void saveMessage() {
    FacesContext context = FacesContext.getCurrentInstance();

    context.addMessage("growlForm:growl1", new FacesMessage("Successful",  "Your message: " + message) );
    context.addMessage("growlForm:growl1", new FacesMessage("Second Message", "Additional Message Detail"));

    context.addMessage("growlForm:growl2", new FacesMessage("Successful 2",  "Your message: " + message) );
    context.addMessage("growlForm:growl2", new FacesMessage("Second Message 2", "Additional Message Detail"));
}

在这种情况下,我发现了一些奇怪的东西。一开始,两个咆哮都在saveMethod中添加了四条消息。过了一会儿(6秒,life属性的默认值),来自growl1的所有消息都按预期消失。

我不知道为什么两个咆哮最终都会收到4条消息。我搜索了现有的咆哮问题,并发现了与multiple grows相关的问题。

看一下生成的html。你有同样的事吗?