JavaServer Faces(JSF) - 显示自定义验证错误

时间:2011-04-03 21:53:07

标签: jquery validation jsf

在JSF中,您可以使用<h:messages />显示验证错误。但我想要做的是使用Jnofity(http://www.givainc.com/labs/jnotify_jquery_plugin.htm)显示这些消息。

我尝试过使用<script>$.jnotify(<h:messages />, 'error');</script>,但这导致事实上没有显示任何内容......也许使用自定义bean是最好的解决方案吗?或者还有其他人吗?

提前致谢!

2 个答案:

答案 0 :(得分:0)

使用<h:outputText/>组件。

<h:outputText value="<script>$.jnotify('#{myBean.errorMessage}');</script>"
              escape="false" 
              rendered="#{myBean.renderErrorMessage}"/>

和你的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;

  }

}

答案 1 :(得分:0)

您可以使用其他帮助bean来检查验证消息:

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;
    }   

}

然后,测试表单或特定组件是否有消息:

<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>

如果您使用的是JSF 1.2,则需要jboss-el或类似的功能来启用参数化方法。