2012-10-21 53 views
1

我下面的代码是不工作按预期。验证不按预期工作。JSF验证工作不

<!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); 
//  } 
    } 

} 

正如你可以看到我确认我只是抛出一个例外,任何东西都输入。必填字段也不起作用。没有错误消息被打印。我究竟做错了什么 ?

亲切的问候

回答

2

你没有实际提交表单。你基本上是分别调用一个独立的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" /> 
+0

没有仍然没有工作? – user1730789

+0

假设你的意思“没有工作”,即既不确认,也不是操作方法是不断调用(由调试器证实),那么问题在别处比迄今发布的代码可见引起的,再加上变化答案已发布到目前为止。首先,根据webbrowser的开发者工具集实际发送了一个HTTP请求?你能成功提交一个Hello World JSF表单吗? – BalusC

+0

我的验证器正在工作,因为我在那里有一个调试线,并打印。但抛出的异常没有被打印? – user1730789