0

When I hit a certain page in my application then the commandbutton's method (method called submitEntry) gets invoked during the page load. This is an unexpected behaviour, because I want it gets called when i click on the commandbutton. Could you explain why I experience this behaviour? What am I doing wrong?

The backing bean:

@Named
@ViewScoped
public class TournamentDetailBean implements Serializable {

private static final Logger logger
        = Logger.getLogger("bean.TournamentDetailBean");

private TournamentEntity tournamentEntity;

@EJB
private TournamentServiceLocal tournamentServiceLocal;

public TournamentDetailBean() {
}

public TournamentEntity getTournamentEntity() {
    return tournamentEntity;
}

public void setTournamentEntity(TournamentEntity tournamentEntity) {
    this.tournamentEntity = tournamentEntity;
}

public String submitEntry() {
    logger.log(Level.INFO, "submitEntry is called..");
    String username = ((UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
    tournamentServiceLocal.persistEntry(tournamentEntity.getId(), username);
    return "/index";
}

}

The page:

 <ui:define name="customContent">
    <f:metadata>
        <f:viewParam name="id" value="#{tournamentDetailBean.tournamentEntity}" converter="#{tournamentConverter}" 
                     converterMessage="Unknown tournament, please use a link from within the system." 
                     required="true" requiredMessage="Bad request, please use a link from within the system." />
    </f:metadata>
    <h:messages />
    <h:form rendered="#{not empty tournamentDetailBean.tournamentEntity}">
        <div class="container">
            <div class="custom-card">
                <h4 class="custom-card-header">#{tournamentDetailBean.tournamentEntity.name}</h4>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3">
                            <h:outputLabel for="tournamentName" value="Name" styleClass="font-weight-bold"/>
                        </div>
                        <div class="col-md-3">
                            <h:outputText id="tournamentName" value="#{tournamentDetailBean.tournamentEntity.name}"/>
                        </div>
                        <div class="col-md-3">
                            <h:outputLabel for="tournamentType" value="Type" styleClass="font-weight-bold"/>
                        </div>
                        <div class="col-md-3">
                            <h:outputText id="tournamentType" value="{TYPE}" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">
                            <h:outputLabel for="tournamentStatus" value="Status" styleClass="font-weight-bold" />
                        </div>
                        <div class="col-md-3">
                            <h:outputText id="tournamentStatus" value="#{tournamentDetailBean.tournamentEntity.status}" />
                        </div>
                        <div class="col-md-3">
                            <h:outputLabel for="tournamentNo" value="Given number of competitors" styleClass="font-weight-bold" />
                        </div>
                        <div class="col-md-3">
                            <h:outputText id="tournamentNo" value="#{tournamentDetailBean.tournamentEntity.numberOfCompetitors}" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">
                            <h:outputLabel for="tournamentDescription" value="Description" styleClass="font-weight-bold" />
                        </div>
                        <div class="col-md-9">
                            <h:outputText id="tournamentDescription" value="#{tournamentDetailBean.tournamentEntity.description}" />
                        </div>
                    </div>
                    <div class="row justify-content-end">
                        <div class="col-4">
                            <h:commandButton id="submit" rendered="#{tournamentDetailBean.tournamentEntity.status eq 'OPEN'}" styleClass="btn btn-lg btn-primary" value="ENTRY" actionListener="#{tournamentDetailBean.submitEntry()}"/>
                            <!--<h:commandButton id="submit" styleClass="btn btn-lg btn-primary" value="ENTRY" actionListener="#{tournamentDetailBean.submitEntry()}"/>-->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </h:form>
</ui:define>
gfazek
  • 73
  • 1
  • 6

1 Answers1

3

What a shame..There was a commented commandbutton tag in the page structure (see above) and the EL expression inside that was evaluated that's why the method was invoked while the page loading.

gfazek
  • 73
  • 1
  • 6