So, I’m going to create more of a ‘dashboard’ application, at least from the development standpoint. So, while my RPS application has three possible outcomes, win lose or tie, I only want to use one JSP page, the index.jsp.
Here’s the RockPaper
package com.mcnz.jsf.bean;
import javax.faces.context.FacesContext;
public class RPSBean {
private String clientGesture = null;
private String computerGesture = null;
public String getClientGesture() {return clientGesture;}
public void setClientGesture(String clientGesture) {this.clientGesture = clientGesture;}
public String getComputerGesture() {return computerGesture;}
public void setComputerGesture(String computerGesture) {this.computerGesture = computerGesture;}
public String doRockPaperScissorsGame() {
String result = "failure";
java.util.Map requestParameterMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
this.computerGesture = "rock";
if (clientGesture.equalsIgnoreCase("scissors")){
result = "youlose";
}
if (clientGesture.equalsIgnoreCase("paper")){
result = "youwin";
}
if (clientGesture.equalsIgnoreCase("rock")){
result = "tie";
}
System.out.println(clientGesture);
return result;
}
}
And here’s the faces-config.xml file:
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<application>
<locale-config>
<default-locale>es</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
</application>
<managed-bean>
<managed-bean-name>rpsbean</managed-bean-name>
<managed-bean-class>com.mcnz.jsf.bean.RPSBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-action>#{rpsbean.doRockPaperScissorsGame}</from-action>
<from-outcome>youwin</from-outcome>
<to-view-id>/win.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{rpsbean.doRockPaperScissorsGame}</from-action>
<from-outcome>youlose</from-outcome>
<to-view-id>/lose.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{rpsbean.doRockPaperScissorsGame}</from-action>
<from-outcome>tie</from-outcome>
<to-view-id>/tie.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{rpsbean.doRockPaperScissorsGame}</from-action>
<from-outcome>failure</from-outcome>
<to-view-id>/failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/win.jsp</from-view-id>
<navigation-case>
<from-outcome>playAgain</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/lose.jsp</from-view-id>
<navigation-case>
<from-outcome>playAgain</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/tie.jsp</from-view-id>
<navigation-case>
<from-outcome>playAgain</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/*</from-view-id>
<navigation-case>
<from-outcome>playAgain</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
And here’s the landing page, the index.jsp file:
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<html>
<f:view locale="sw" >
<f:loadBundle basename="com.mcnz.jsf.UIResources" var="i18n"/>
<head>
<title>Rock Paper Scissors: JSF Style</title>
</head>
<body>
Please chose between Rock Paper and Scissors:
<h:form id="rpsgame" >
<h:inputText id="gesture" value="#{rpsbean.clientGesture}"></h:inputText>
<h:commandButton id="submit" type="submit" value="Play!" action="#{rpsbean.doRockPaperScissorsGame}" ></h:commandButton>
</h:form>
</body>
</f:view>
</html>
No comments:
Post a Comment