0

我正在运行 duke guess number 示例,我没有看到生命周期按预期执行。我有一个inputText期望一个数字。我们还有一个转换器和一个验证器。一旦我提交了<h:form>验证器,就会被调用。它应该检查输入是否为整数。托管 bean 属性的值未得到更新。请解释一下,下面是代码片段。

<h:inputText id="userNo" label="User Number" value="#{UserNumberBean.userNumber}" 
               converterMessage="#{ErrMsg.userNoConvert}"  validator="#{UserNumberBean.validate}">
      <f:valueChangeListener  type="#{UserNumberBean.valueChange()}"></f:valueChangeListener>
                  <f:validateLongRange minimum="#{UserNumberBean.minimum}" maximum="#{UserNumberBean.maximum}" /> 
  </h:inputText>

这里验证器方法是在inputText组件中设置的。一旦我提交<h:form>,该validate方法被调用,下面是该方法

public String validate(javax.faces.context.FacesContext fc, javax.faces.component.UIComponent ui, java.lang.Object o){
        System.out.println("in my own validation method");
        if(userNumber ==8){
            return "validation";
        }
        return "validation";
    }

userNumber是该类的支持 bean 属性

public class UserNumberBean {

     public Integer userNumber = null;

public void setUserNumber(Integer user_number) {
        System.out.println("setting userName" + user_number);
        userNumber = user_number;
    }

    public Integer getUserNumber() {
        return userNumber;
    }
}

它有 getter 和 setter,但仍然userNumber没有设置值。我NullPointerException在访问时进入验证器方法userNumber。请让我知道出了什么问题。下面是代码片段

<HTML xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <HEAD> <title>Hello</title> </HEAD>
    <%@ page contentType="application/xhtml+xml" %>
    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <body bgcolor="white">
    <f:view>
    <h:form id="helloForm" >
      <h2>Hi. My name is Duke.  I'm thinking of a number Man from
      <h:outputText lang="en_US" value="#{UserNumberBean.minimum}"/> to
      <h:outputText value="#{UserNumberBean.maximum}"/>.  Can you guess
      it?</h2>

      <h:graphicImage id="waveImg" url="/wave.med.gif" alt="Duke waving" />
      <h:inputText id="userNo" label="User Number" value="#{UserNumberBean.userNumber}" 
                   converterMessage="#{ErrMsg.userNoConvert}"  validator="#{UserNumberBean.validate}">
          <f:valueChangeListener  type="#{UserNumberBean.valueChange()}"></f:valueChangeListener>
                      <f:validateLongRange minimum="#{UserNumberBean.minimum}" maximum="#{UserNumberBean.maximum}" /> 
      </h:inputText>
      <h:commandButton id="submit" action="success" value="Submit" />
      <p>
      <h:message style="color: red; font-family: 'New Century Schoolbook', serif; font-style: oblique; text-decoration: overline" id="errors1" for="userNo"/>
    </p>
   </h:form>
  </f:view>
 </body>
</HTML> 




import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.LongRangeValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.Random;


public class UserNumberBean {
    Integer randomInt = null;
     public Integer userNumber = null;
    String response = null;
    private boolean maximumSet = false;
    private boolean minimumSet = false;
    private long maximum = 0;
    private long minimum = 0;

    public UserNumberBean() {

        System.out.println(" in constructor");
        Random randomGR = new Random();
        randomInt = new Integer(randomGR.nextInt(10));
        System.out.println("Duke's number: " + randomInt);
    }

    public void setUserNumber(Integer user_number) {
        System.out.println("setting userName" + user_number);
        userNumber = user_number;
    }

    public Integer getUserNumber() {
        return userNumber;
    }

    public String getResponse() {
        System.out.println(" in getResponse");
        if ((userNumber != null) && (userNumber.compareTo(randomInt) == 0)) {
            return "Yay! You got it!";
        } else {
            return "Sorry, " + userNumber + " is incorrect.";
        }
    }

    public long getMaximum() {
        return (this.maximum);
    }

    public void setMaximum(long maximum) {
        this.maximum = maximum;
        this.maximumSet = true;
    }

    public long getMinimum() {
        return (this.minimum);
    }

    public void setMinimum(long minimum) {
        this.minimum = minimum;
        this.minimumSet = true;
    }


    public String validate(javax.faces.context.FacesContext fc, javax.faces.component.UIComponent ui, java.lang.Object o){
        System.out.println("in my own validation method");
        if(userNumber ==8){
            return "validation";
        }
        return "validation";
    }

    public void valueChange(){
        System.out.println(" in value change");
    }
}

应用请求阶段实际上是什么?应用请求阶段会发生什么?与更新模型值阶段有何不同

4

1 回答 1

4

您的验证器以两种方式损坏:

  1. 错误的方法签名。它应该返回无效。在验证失败时,您应该抛出一个ValidatorException. 成功后,您应该只是返回并且什么都不做。

  2. 您应该验证作为第三个参数提供的值,而不是模型值(当时根本没有设置)。

所以,这应该这样做:

public void validate(FacesContext context, UIComponent component, Object value) {
    if (value == null) {
        return; // Ignore it. Let required="true" handle.
    }

    if (value != 8) {
        // Assuming that 8 is the guess number?
        throw new ValidatorException(new FacesMessage("Wrong guess, try again."));
    }
}

NullPointerException的原因是您试图将模型值userNumber与原始整数进行比较8。这会导致自动装箱尝试将其拆箱userNumber为原语,但是如果它userNumber本身null不能以任何原语形式表示,则会失败。

至于阶段,肯定会调用应用请求值阶段,否则永远不会调用验证阶段。您似乎期望应用请求值阶段会更新模型值。这不是真的,它将请求参数应用于 JSF 输入组件的submittedValue.

也可以看看:

于 2013-07-18T15:26:50.403 回答