我正在运行 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");
}
}
应用请求阶段实际上是什么?应用请求阶段会发生什么?与更新模型值阶段有何不同