0

I'm trying to build a hangman game with JSF. I have a Facelet file where I put the command buttons for the abecedary letters.

<ui:composition template="/template.xhtml">
        <ui:define name="title">
            <h:outputText value="Dak's Hangman"></h:outputText>
        </ui:define>
        <ui:define name="body">
            <h:panelGroup id="messagePanel" layout="block">
                <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
            </h:panelGroup>
            <h:form id="formJuego">
                <h:head  id="demo" >Opportunities: </h:head>            
                <h:outputText value="${partidaController.getRemainingOp()}"/>    

                <br/>
                        <h:outputText value="${partidaController.getNombreJugador()}"/>

                    <br/>
                    <h:form>

                        <c:forEach var="x" items="${partidaController.getLetrasColocadas()}" >
                            <h:inputText disabled="true" size="1" value="${x}"/>
                        </c:forEach>
                    </h:form>
                    <br/>
                    <h:panelGrid columns="2">
                        <h:form> 
                            <h:panelGrid columns="9">
                                <p:commandButton style="height: 30px; width: 30px;" value="a" action="#{turnoController.createTurno('a')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="b" action="#{turnoController.createTurno('b')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="c" action="#{turnoController.createTurno('c')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="d" action="#{turnoController.createTurno('d')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="e" action="#{turnoController.createTurno('e')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="f" action="#{turnoController.createTurno('f')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="g" action="#{turnoController.createTurno('g')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="h" action="#{turnoController.createTurno('h')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="i" action="#{turnoController.createTurno('i')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="j" action="#{turnoController.createTurno('j')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="k" action="#{turnoController.createTurno('k')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="l" action="#{turnoController.createTurno('l')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="m" action="#{turnoController.createTurno('m')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="n" action="#{turnoController.createTurno('n')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="o" action="#{turnoController.createTurno('o')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="p" action="#{turnoController.createTurno('p')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="q" action="#{turnoController.createTurno('q')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="r" action="#{turnoController.createTurno('r')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="s" action="#{turnoController.createTurno('s')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="t" action="#{turnoController.createTurno('t')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="u" action="#{turnoController.createTurno('u')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="v" action="#{turnoController.createTurno('v')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="w" action="#{turnoController.createTurno('w')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="x" action="#{turnoController.createTurno('x')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="y" action="#{turnoController.createTurno('y')}"/>
                                <p:commandButton style="height: 30px; width: 30px;" value="z" action="#{turnoController.createTurno('z')}"/>
                            </h:panelGrid>                        
                        <h:graphicImage value="../resources/images/hangman.jpg" width="480" height="400" />
                        <br/>
                        </h:form>
                    </h:panelGrid>

                    <h:link outcome="/index" value="#{bundle.CreateJugadorIndexLink}"/>

            </h:form>
        </ui:define>
    </ui:composition>

Here's the backing bean class class:

public class TurnoController implements Serializable {

    private Turno current;
    private DataModel items = null;
    @EJB
    private DBClasses.TurnoFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

Which has a createTurno() method:

public String createTurno(String s) {
        try {
            Map<String, Object> sesionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
            Integer id = (Integer) sesionMap.get("id_partida");

            Partida p = new Partida();
            p.setIdPartida(id);
            current= new Turno();
            current.setIdPartida(p);
            current.setLetraTurno(s);
            ejbFacade.create(current);
            return "tablero";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

What I want to do is to call the method from the class controller which updates the movement in the database (I use PostgreSQL).

However, the method is not invoked when the command button is pressed. How is this caused and how can I solve it?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
dak
  • 179
  • 1
  • 4
  • 18

1 Answers1

0

Is your TurnoController annotated? If not, put the following 2 lines above your controller

@ManagedBean(name="turnoController")
@SessionScoped
public class TurnoController implements Serializable {

If this doesn't work, try if calling a method without a paramter works.

EDIT: Use " (double quotes) for string and ' (single quotes) for Chars. So replace your single quotes with double quotes or change your param type to char.

Check out this link for help on passing params to a backing bean:

http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

Rob
  • 2,226
  • 1
  • 17
  • 39
  • what do you exactly mean by annotated? I have put single a instead of 'a'in commandbutton action but it still doesn't work... – dak Jun 01 '13 at 13:29
  • there must be something wrong in the xhtml file... can I have a form inside a form? is the form necessary to execute the createPartida? I'm trying to debug what's happening but the method is not accessed... if I dont put any parameter it doesn't work neither. – dak Jun 01 '13 at 13:41
  • I have the @ManagedBean in my code... there must be another problem :S – dak Jun 01 '13 at 13:42
  • 1
    AFAIK you can't nest forms in eachother. EDIT: http://stackoverflow.com/questions/7371903/multiple-hform-in-a-jsf-page – Rob Jun 01 '13 at 13:42
  • AHAAAAA!!!!! I'll try to change the xhtml file and see what happens ... I'll tell you ;) thanks! – dak Jun 01 '13 at 13:44
  • Note: you can use partidaController.remainingOp instead of partidaController.getRemainingOp(). JSF will translate is to the appropiate getter and setter – Rob Jun 01 '13 at 13:50
  • Glad to hear that it works now. Please mark the answer as accepted so that others with the same problems will be able to find the solution. – Rob Jun 01 '13 at 13:57
  • @dak as Rob states in his last comment, please [mark the answer as accepted](http://meta.stackexchange.com/a/5235/182862) so future readers can benefit from this Q/A. – Luiggi Mendoza Jun 03 '13 at 01:13