【问题标题】:JSF Validation no wok in templateJSF Validation no wok in template
【发布时间】:2013-04-17 15:55:34
【问题描述】:

JSF 验证不起作用当我提交时,我在 myfaces(2.1.10) 和 mojarra(2.1.21) 下尝试过,结果相同。这是我的代码。理论上不会执行 onSubmit() 并显示错误消息在m_name中。但实际上是执行onSubmit()而不显示错误信息。

模板.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:rich="http://richfaces.org/rich">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    <title>Title</title>
</h:head>
<h:body>
        <ui:insert name="body"></ui:insert>
</h:body>
</html>

模板客户端文件templatevalidation.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    template="/template.xhtml">
    <ui:define name="body">
        <h:form>
            <div>
                <h:inputText id="name" value="#{templateBean.name}">
                    <f:validateRequired />
                    <f:validateLength minimum="5" />
                </h:inputText>
                <h:message id="m_name" for="name" />
            </div>
            <div>
                <h:commandButton action="#{templateBean.onSubmit}" value="submit">
                    <f:ajax execute="name" render="m_name" />
                </h:commandButton>
            </div>
        </h:form>
    </ui:define>
</ui:composition>

这是我的 bean TemplateBean.class

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class TemplateBean implements Serializable{

    private static final long serialVersionUID = 9009393522101806766L;

    private String name;

    public void onSubmit(){
        System.out.println("Name: " + name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


}

【问题讨论】:

    标签: jsf jsf-2 facelets


    【解决方案1】:

    您需要在 AJAX 调用之后更新您的 &lt;h:message&gt; 组件,以使用 &lt;f:ajax&gt; 标签中的 render 属性显示错误消息:

    <h:message id="nameMsg" for="name" />
    
    <h:commandButton action="#{templateBean.onSubmit}" value="submit">
        <f:ajax execute="name" render="nameMsg" />
    </h:commandButton>
    

    【讨论】:

      【解决方案2】:

      必须使用休眠验证

      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-validator</artifactId>
          <version>5.0.0.Final</version>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 2021-08-26
        • 2016-11-24
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        • 2013-03-24
        • 2020-08-26
        • 1970-01-01
        相关资源
        最近更新 更多