Thursday, July 16, 2009

Not Liking the Dashboard

I did a little work with the dashboard JSF app, but didn’t like it. I think I’ll go back to the original plan with multiple JSP pages.

In my managed bean, I threw a String into the JSF message and displayed that message on the index.jsp


package com.mcnz.jsf.bean;

import javax.faces.application.FacesMessage;
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 = "success";
        java.util.Map requestParameterMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.computerGesture = "rock";
        String message = null;
        if (clientGesture.equalsIgnoreCase("scissors")){
            //result = "youlose";
            message = "I picked Rock. You lose!";
        if (clientGesture.equalsIgnoreCase("paper")){
            //result = "youwin";
            message = "I picked rock. You win!";
        if (clientGesture.equalsIgnoreCase("rock")){
            //result = "tie";
            message = "We both picked rock. It's a tie.";

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
        return result;

Personally, that’s not a good use of message. After all, that should really be for errors or warning, not normal communication between client and server.

I just used the h:messages JSF tag for display. It worked, but as I said, I don’t like it. I’m going back and will now focus on input validation.



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>

No comments:

Post a Comment