JavaServer Faces (JSF) - Custom validation error displaying
In JSF you can display validation error's using <h:messages />
. But what I want to do is display these messages using Jnofity (http://www.givainc.com/labs/jnotify_jquery_plugin.htm).
I've tried using <script>$.jnotify(<h:messages />, 'error');</script>
, but this result in the fact that nothing is displayed than... Maybe using a custom bean is the best solution for this? Or are th开发者_如何学运维ere others?
Thanks in advance!
Use an <h:outputText/>
component.
<h:outputText value="<script>$.jnotify('#{myBean.errorMessage}');</script>"
escape="false"
rendered="#{myBean.renderErrorMessage}"/>
and in your BackingBean
public class MyBean {
private String errorMessage = "";
private bolean renderErrorMessage = false;
public String doSomethingWrong(){
//Something wrong goes here
this.errorMessage = "Something Wrong Happened";
this.renderErrorMessage = true;
return null;
}
}
You can use additional helper bean that will check for validation messages:
public class MessagesBean {
public List<FacesMessage> forComponent(String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = getContext().getViewRoot();
UIComponent component = findComponent(viewRoot, id, UIComponent.class);
if(component instanceof UIForm) {
return getMessagesRecursively(component);
}
return forComponent(context, component);
}
public List<FacesMessage> forComponent(FacesContext context, UIComponent component) {
List<FacesMessage> messages = new ArrayList<FacesMessage>();
if(component != null) {
Iterator<FacesMessage> msgItr = context.getMessages(component.getClientId(context));
while(msgItr.hasNext()) {
messages.add(msgItr.next());
}
}
return messages;
}
private List<FacesMessage> getMessagesRecursively(UIComponent parent) {
List<FacesMessage> messages = new ArrayList<FacesMessage>();
if(parent != null) {
FacesContext context = getContext();
Iterator<FacesMessage> msgItr = context.getMessages(parent.getClientId(context));
while(msgItr.hasNext()) {
messages.add(msgItr.next());
}
if(parent.getChildCount() > 0) {
for (UIComponent child : parent.getChildren()) {
List<FacesMessage> childMesssages = getMessagesRecursively(child);
if(childMesssages.size() > 0) {
messages.addAll(childMesssages);
}
}
}
}
return messages;
}
private <T> T findComponent(UIComponent base, String id, Class<T> returnType) {
if (id.equals(base.getId())) {
return returnType.cast(base);
}
Iterator<UIComponent> children = base.getFacetsAndChildren();
while (children.hasNext()) {
T component = findComponent(children.next(), id, returnType);
if (component != null) {
return returnType.cast(component);
}
}
return null;
}
}
Then, test whether the form or a specific component have messages:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jstl/core"
template="/WEB-INF/template/jnotify.xhtml">
<ui:param name="title" value="jnotify with jsf"/>
<ui:define name="content">
<h:form id="frmTest">
<h:outputText value="Submitted Text: #{not empty testBean.text ? testBean.text : 'N/A'}"/><br/>
<h:inputText id="txtTest" required="true"
value="#{testBean.text}"/>
<h:commandButton value="Submit"/>
<script type="text/javascript">
<c:forEach items="#{messagesBean.forComponent('frmTest')}" var="message">
$.jnotify("#{message.summary}", "#{message.severity}");
</c:forEach>
</script>
</h:form>
</ui:define>
</ui:composition>
In case you are using JSF 1.2 you will need jboss-el or similar to enable parameterized methods.
精彩评论