This can be done with a little help PhaseListener.
Here, how the view looks, rather than shocking, is just <f:ajax>for each input component that captures the event blurand thus immediately re-displays the message component when the input field loses focus.
<!DOCTYPE html>
<html lang="en"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Stackoverflow question 4313917</title>
<style>.info { color: green; } .error { color: red; }</style>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel for="input1">Input 1</h:outputLabel>
<h:inputText id="input1" value="#{bean.input1}" required="true">
<f:ajax event="blur" render="input1message" />
</h:inputText>
<h:message id="input1message" for="input1" infoClass="info" errorClass="error" />
<h:outputLabel for="input2">Input 2</h:outputLabel>
<h:inputText id="input2" value="#{bean.input2}" required="true">
<f:ajax event="blur" render="input2message" />
</h:inputText>
<h:message id="input2message" for="input2" infoClass="info" errorClass="error" />
<h:outputLabel for="input3">Input 3</h:outputLabel>
<h:inputText id="input3" value="#{bean.input3}" required="true">
<f:ajax event="blur" render="input3message" />
</h:inputText>
<h:message id="input3message" for="input3" infoClass="info" errorClass="error" />
<h:panelGroup />
<h:commandButton value="Submit" action="#{bean.submit}" />
<h:panelGroup />
</h:panelGrid>
</h:form>
</h:body>
</html>
Bean speaks for itself: 3 properties input1, input2and input3and void submit(). Just let your IDE auto-generate the template.
Here PhaseListener:
package com.example;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
public class MessagePhaseListener implements PhaseListener {
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
@Override
public void beforePhase(PhaseEvent event) {
FacesContext facesContext = event.getFacesContext();
if (facesContext.isPostback()) {
UIViewRoot viewRoot = facesContext.getViewRoot();
for (String clientId : facesContext.getExternalContext().getRequestParameterMap().keySet()) {
UIComponent component = viewRoot.findComponent(clientId);
if (component instanceof UIInput && !facesContext.getMessages(clientId).hasNext()) {
facesContext.addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_INFO, "OK!", null));
}
}
}
}
@Override
public void afterPhase(PhaseEvent event) {
}
}
Register it in faces-config.xml(no, unfortunately, no JSF 2.0 annotations for phaselists).
<lifecycle>
<phase-listener>com.example.MessagePhaseListener</phase-listener>
</lifecycle>
"OK!" .