I would like to have different messages on different message components, but unfortunately message always display on both message components.
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>
I would like to have different messages on different message components.
Thanks to @WoAiNii
Working example as below.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.