【问题标题】:JSF Validation not workingJSF 验证不起作用
【发布时间】:2012-10-21 13:16:43
【问题描述】:

我的以下代码没有按预期工作。验证未按预期工作。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:f="http://java.sun.com/jsf/core">

<h:head>
    <title>My Registration Page</title>
    <link href="stylesheet/reset.css" rel="stylesheet" type="text/css" />
    <link href="stylesheet/style.css" rel="stylesheet" type="text/css" />
    <link rel="icon" type="image/png" href="images/icons/favicon.png" />
    <script src="script/script.js" />
</h:head>
<h:body>
    <h:outputText id="progress_bar" styleClass="progress-bar" /> 
    <a4j:jsFunction name="login" action="#{loginBean.validateUser}" />


    <div id="login-container">
        <div class="login-logo">
            <img src="images/logo.png" />
        </div>
        <f:view>
            <div id="loin-form">
                <h1>
                    Log in to your account or <a href="#">sign up</a> to Get Started
                </h1>
                <h:form>
                    <h:inputText id="userName" value="#{loginBean.userName}"
                        label="User Name" required="true" class="txtFld">
                        <f:validator
                            validatorId="com.coinfling.validation.LoginBeanValidator" />
                    </h:inputText>
                    <h:message for="userName" style="color:red"></h:message>
                    <h:inputSecret id="passWord" value="#{loginBean.passWord}"
                        label="Password" required="true" class="pswrdFld">
                        <f:validator
                            validatorId="com.coinfling.validation.LoginBeanValidator" />
                    </h:inputSecret>
                    <h:message for="passWord" style="color:red"></h:message>
                    <p>
                        <a4j:commandLink id="forgotPasswordLink" lable="Password"
                            value="Forgot Your Password? " />
                        <a4j:commandButton id="loginButton" value="Sign In"
                            onclick="login();" styleClass="sign-btn" />
                    </p>
                </h:form>
            </div>


        </f:view>
    </div>

</h:body>
</html>

我的简单验证类代码如下 包 com.coinfling.validation;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator("com.coinfling.validation.LoginBeanValidator")
public class LoginBeanValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent toValidate,
            Object value) throws ValidatorException {
        FacesMessage message = new FacesMessage("*UserName is not in Correct Format");
        throw new ValidatorException(message);

//      
//      if(toValidate.getId().equals("userName"))
//      {
//          FacesMessage message = new FacesMessage("*UserName is not in Correct Format");
//          throw new ValidatorException(message);
//      }
//      else if(toValidate.getId().equals("passWord"))
//      {
//          FacesMessage message = new FacesMessage("*Password is not in Correct Format");
//          throw new ValidatorException(message);
//      }
    }

}

如您所见,我的验证只是针对输入的任何内容抛出异常。必填字段也不起作用。不打印错误消息。我做错了什么?

亲切的问候

【问题讨论】:

    标签: jsf richfaces


    【解决方案1】:

    您实际上并没有提交表单。您基本上是在单独调用一个独立的 bean 方法。这是一个巨大的差异。要对表单执行验证,您必须提交表单。

    替换错误的a4j:jsFunction方法:

    <a4j:jsFunction name="login" action="#{loginBean.validateUser}" />
    ...
    <a4j:commandButton id="loginButton" value="Sign In"
       onclick="login();" styleClass="sign-btn" />
    

    通过正常的表单提交方式:

    <a4j:commandButton id="loginButton" value="Sign In"
       action="#{loginBean.validateUser}" styleClass="sign-btn" />
    

    我不确定您为什么使用a4j:jsFunction,但无论您认为从a4j:jsFunction 中受益,肯定都必须以不同的方式解决。

    不要忘记添加 render="@form" 以在完成 ajax 请求时更新表单,以便显示所有消息:

    <a4j:commandButton id="loginButton" value="Sign In"
       action="#{loginBean.validateUser}" render="@form" styleClass="sign-btn" />
    

    【讨论】:

    • 假设您的意思是“不工作”,即验证和操作方法都没有被调用(由调试器确认),那么问题是在其他地方引起的,而不是在发布的代码中可见到目前为止,结合迄今为止发布的答案的变化。首先,一个 HTTP 请求实际上是根据 webbrowser 的开发工具集发送的吗?能否成功提交 Hello World JSF 表单?
    • 我的验证器正在工作,因为我在那里有一条调试线,并且正在打印。但是抛出的异常没有被打印出来?
    • 调用了操作mettod和验证器函数,但没有打印验证消息。
    • 这很有帮助。将来,请像这样详细地描述问题,而不是像您是最终用户而不是开发人员那样无言以对的“不工作”。好吧,结果根本没有以表格的形式呈现出来。您需要将render="@form" 添加到&lt;a4j:commandButton&gt; 以指示它在请求后更新表单。这将更新&lt;h:form&gt; 的 HTML 输出,因此还会显示任何消息。我已经用一个例子编辑了答案。
    • 谢谢一百万。现在又是一个问题。我有我的 a4j 函数,因为 onBegin 和 onComplete 我显示并隐藏了一个 ProgressBar。我现在该怎么做?
    猜你喜欢
    • 2014-05-29
    • 2013-11-25
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多