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?
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/