繁体   English   中英

如何在不同的消息组件上添加 Faceslets 消息?

[英]How to add Faceslets message on different messages component?

我想在不同的消息组件上有不同的消息,但不幸的是消息总是显示在两个消息组件上。

TestFaces.java

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@javax.enterprise.context.RequestScoped
@javax.inject.Named
public class TestFaces implements java.io.Serializable {
    static final long serialVersionUID = 1L;
    static final Logger logger = LogManager.getLogger();

    public String callMsg001() {
        FacesContext.getCurrentInstance().addMessage("msg001",
                new FacesMessage(FacesMessage.SEVERITY_INFO, "This is msg001", null));

        return null;
    }
    
    public String callMsg002() {
        FacesContext.getCurrentInstance().addMessage("msg002",
                new FacesMessage(FacesMessage.SEVERITY_INFO, "This is msg002", null));

        return null;
    }

}

index.html

<!DOCTYPE HTML>
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Index</title>
</h:head>
<h:body>
    <h:form>
        msg001: <h:messages id="msg001" style="msg001" />
        msg002: <h:messages id="msg002" style="msg002"/>
        <h:commandButton
            value="Trigger msg001"
            action="${testFaces.callMsg001()}">
        </h:commandButton>
        <h:commandButton
            value="Trigger msg002"
            action="${testFaces.callMsg002()}">
        </h:commandButton>
    </h:form>
</h:body>
</html>

Output 在此处输入图像描述

我想在不同的消息组件上有不同的消息。

感谢@WoAiNii

工作示例如下。

TestFaces.java

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

@javax.enterprise.context.RequestScoped
@javax.inject.Named
public class TestFaces implements java.io.Serializable {
    static final long serialVersionUID = 1L;

    public String callCmd001() {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "This is global cmd001", null));
        
        FacesContext.getCurrentInstance().addMessage("form001:cmd001",
                new FacesMessage(FacesMessage.SEVERITY_INFO, "This is cmd001", null));

        return null;
    }

    public String callCmd002() {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "This is global cmd002", null));
        
        FacesContext.getCurrentInstance().addMessage("form001:cmd002",
                new FacesMessage(FacesMessage.SEVERITY_INFO, "This is cmd002", null));

        return null;
    }

}

index.html

<!DOCTYPE HTML>
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Message Test</title>
</h:head>
<h:body>
    <h:form id="form001">
        msgs: <h:messages/><br/>
        msgs global Only: <h:messages globalOnly="true"/><br/>
        msg001: <h:message for="form001:cmd001"/><br/>
        msg002: <h:message for="form001:cmd002"/><br/>
        <h:commandButton id="cmd001"
            value="call cmd001"
            action="${testFaces.callCmd001()}">
        </h:commandButton>
        <h:commandButton id="cmd002"
            value="call cmd002"
            action="${testFaces.callCmd002()}">
        </h:commandButton>
    </h:form>
</h:body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM