Reputation: 2843
I'm looking for a way to achieve the following switch in JSF:
...
<div>
if empty(<h:message for="x">)
<h:outputText value="default value" />
else
<h:message for="x" />
endif
</div>
....
Any ideas?
Upvotes: 2
Views: 1183
Reputation: 1108722
This is doable with a little help of a PhaseListener
.
Here's how the view look like, not shocking, just a <f:ajax>
on every input component which hooks on blur
event and thus immediately re-renders 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
, input2
and input3
and a void submit()
method. Just let your IDE autogenerate the boilerplate.
Here's the 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) {
// NOOP. It's too late anyway.
}
}
Register it as follows in faces-config.xml
(no, unfortunately, there are no JSF 2.0 annotations for phaselisteners).
<lifecycle>
<phase-listener>com.example.MessagePhaseListener</phase-listener>
</lifecycle>
You may want to parameterize the "OK!" message in the phaselistener.
Upvotes: 2