h:message为空时的默认输出 [英] Default output when h:message is empty
问题描述
我正在寻找一种在JSF中实现以下转换的方法:
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>
....
有什么想法吗?
推荐答案
在这是视图的外观,而不是令人震惊,只是每个输入组件上的<f:ajax>
都挂在blur
事件上,因此当输入字段失去焦点时立即重新渲染消息组件.
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不言自明:3个属性input1
,input2
和input3
和一个void submit()
方法.只需让您的IDE自动生成样板即可.
Bean speaks for itself: 3 properties input1
, input2
and input3
and a void submit()
method. Just let your IDE autogenerate the boilerplate.
这是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.
}
}
在faces-config.xml
中进行如下注册(不幸的是,没有,没有用于侦听器的JSF 2.0注释).
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.
这篇关于h:message为空时的默认输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!