Java 跟踪JSF玩的游戏
我想知道是否有人能帮我 我正在使用JSF创建一个简单的游戏。我已经完成了主要功能,但我想告诉用户他们玩了多少游戏 出于某种原因,我为其编写的代码不起作用 豆子: 第一页(play_game.xhtml):Java 跟踪JSF玩的游戏,java,jsf,jsf-2,javabeans,Java,Jsf,Jsf 2,Javabeans,我想知道是否有人能帮我 我正在使用JSF创建一个简单的游戏。我已经完成了主要功能,但我想告诉用户他们玩了多少游戏 出于某种原因,我为其编写的代码不起作用 豆子: 第一页(play_game.xhtml): 猜数字页 欢迎参加游戏课程 此会话中玩的游戏数:#{gameBeans.gamesPlayed} 输入您的幸运数字猜测,然后单击播放 你的猜测是: game_result.xhtml: <?xml version='1.0' encoding='UTF-8' ?> <!D
猜数字页
欢迎参加游戏课程
此会话中玩的游戏数:#{gameBeans.gamesPlayed}
输入您的幸运数字猜测,然后单击播放
你的猜测是:
game_result.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Game Results</title>
</h:head>
<h:body>
<h:form>
<p>Your Guess: <h:outputText id="outText1" value="#{gameBeans.userGuess}"></h:outputText></p>
<p>Random Number: <h:outputText id="outText2" value="#{gameBeans.randomNumber}"></h:outputText></p>
<p><h:outputText id="outText4" value="#{gameBeans.win}"></h:outputText></p>
<p>Number of Games Played: #{gameBeans.gamesPlayed}</p>
<h:commandButton id="cmdBtn1" value="Play Again" action="play_game" />
</h:form>
</h:body>
</html>
比赛结果
你的猜测是:
随机数:
玩的游戏数:#{gameBeans.gamesPlayed}
我想允许用户再次玩,即使他们赢或输,计数(玩的游戏)应该保持跟踪。这目前不起作用
有人能帮忙吗
谢谢
@SessionScoped
bean仅在客户端第一次访问您的页面时创建一次。然后它将一直运行到会话结束。换句话说,@SessionScoped
bean的构造函数只调用一次。这不是增加你的gamesPlayed
的地方
@ManagedBean
@SessionScoped
public class GameBeans {
private int randomNumber;
private int userGuess;
private int gamesPlayed;
public GameBeans() {
Random number = new Random();
this.randomNumber = number.nextInt(1000);
this.gamesPlayed = 0;
}
public void getWin() {
if (this.userGuess.equals(this.randomNumber))
return "Congratulations! You've Won!";
else return "You Lose!";
}
public void incrementGamesPlayed() {
this.gamePlayed++;
}
// Getters and Setters
}
这是一个游戏。xhtml
:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Guess Numbers Page</title>
</h:head>
<h:body>
<h:form>
<h1>Welcome to Your Game Session</h1>
<p>Number of games played this session: #{gameBeans.gamesPlayed}</p>
<p>Enter your lucky number guess and then click play</p>
<p>Your guess: <h:inputText id="iptxt1" value="#{gameBeans.userGuess}" /></p>
<h:commandButton id="cmdBtn1" value="Play" action="game_result"
actionListener="#{gameBeans.incrementGamesPlayed}" />
</h:form>
</h:body>
</html>
猜数字页
欢迎参加游戏课程
此会话中玩的游戏数:#{gameBeans.gamesPlayed}
输入您的幸运数字猜测,然后单击播放
你的猜测是:
您所说的“不工作”是什么意思?不会产生任何错误,也不会阻止应用程序的其余部分。只是它没有记录已经打了多少场比赛。非常感谢你。。。我知道这将是一件简单的事情——永远如此!
@ManagedBean
@SessionScoped
public class GameBeans {
private int randomNumber;
private int userGuess;
private int gamesPlayed;
public GameBeans() {
Random number = new Random();
this.randomNumber = number.nextInt(1000);
this.gamesPlayed = 0;
}
public void getWin() {
if (this.userGuess.equals(this.randomNumber))
return "Congratulations! You've Won!";
else return "You Lose!";
}
public void incrementGamesPlayed() {
this.gamePlayed++;
}
// Getters and Setters
}
<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Guess Numbers Page</title>
</h:head>
<h:body>
<h:form>
<h1>Welcome to Your Game Session</h1>
<p>Number of games played this session: #{gameBeans.gamesPlayed}</p>
<p>Enter your lucky number guess and then click play</p>
<p>Your guess: <h:inputText id="iptxt1" value="#{gameBeans.userGuess}" /></p>
<h:commandButton id="cmdBtn1" value="Play" action="game_result"
actionListener="#{gameBeans.incrementGamesPlayed}" />
</h:form>
</h:body>
</html>