我刚刚使用Beans测试了我的GlassFish实验。我遇到的问题是唯一的问题是我的财产message
。我打电话的时候似乎并不喜欢。但是我对任何其他属性没有任何问题。
这是我的课程,它可以获取,设置和执行某些操作。
@ManagedBean(name = "userNumberBean")
@SessionScoped
public class UserNumberBean implements Serializable {
Integer randomInt;
Integer userNumber;
String response;
String message = "";
/**
* Creates a new instance of UserNumberBean
*/
public UserNumberBean() {
}
public Integer getUserNumber() {
return userNumber;
}
public void setUserNumber(Integer userNumber) {
this.userNumber = userNumber;
}
public String getMessage() {
return message;
}
public String getResponse() {
try {
if (userNumber != null && userNumber.equals(randomInt)) {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.redirect("index.html");
this.message = "Yay,you guessed it!";
response = "";
return response;
}
response = "Sorry, "+userNumber+" isn't corret. Guess again.";
} catch (Exception e) {
response = "Unable to redirect page.";
}
return response;
}
public void generateRandomNumber() {
try {
Random randomGR = new Random();
randomInt = new Integer(randomGR.nextInt(10));
System.out.println("Random number generated is: "+randomInt);
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.redirect("greeting.xhtml");
} catch (IOException e) {
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR
, e.getMessage(), "");
FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}
}
}
这是我使用userNumberBean.message
<body>
<ui:composition template="./template.xhtml">
<ui:define name="title">
Greeting
</ui:define>
<ui:define name="box" class="subContainer greyBox">
<h4>Hi, my name is Duke!</h4>
<h5>Would you like to play a guessing game?</h5>
<h:form>
<h4><h:outputText escape="false" value="#{userNumberBean.message}"/></h4>
<p:commandButton id="submit" value="Play" action="#{userNumberBean.generateRandomNumber()}" />
</h:form>
</ui:define>
</ui:composition>
</body>