0

im very new to JSF and JAVA too, but im working on a University project and i'm having some problems that can't solve:

basically im trying to show a button if a person is not inscribed to a course that said "inscribir" with an action to inscribe it and the oposite if is inscribed.

to check if is inscribe im trying to use materiaController.cursaMateria(materia.id) passin the id of materia (materia is course) for check if the logued user is inscribed to that course.

Here is my code:

<h:dataTable value="#{materiaController.getAll()}" var="materia">
    <h:column>
        <f:facet name="header">id</f:facet>
        #{materia.id}
    </h:column>
    <h:column>
        <f:facet name="header">Nombre</f:facet>
        #{materia.nombre}
    </h:column>
    <h:column>
        <f:facet name="header">Estado</f:facet>
        #{materia.estado.descripcion}
    </h:column>
    <c:choose>
        <c:when test="#{materiaController.cursaMateria(materia.id) == false}">
            <h:column>
                <h:form>
                    <h:commandButton value="Inscribir" action="#{materiaController.inscribirMateria(materia.id)}"/>
                </h:form>
            </h:column>
        </c:when>
        <c:otherwise>
            <h:column>
                <h:form>
                    <h:commandButton value="Desinscribir" action="#{materiaController.desinscribirMateria(materia.id)}"/>
                </h:form>
            </h:column>
        </c:otherwise>
    </c:choose>
</h:dataTable>

the problem is that the method "cursaMateria" is always getting the parameter as null. btw the first colum (id) is printed with the corresponding id.

I try other ways too, but is always the same, cant send the parameter:

<c:if test="#{materiaController.cursaMateria(materia.id)}">
    <h:form>
        <h:commandButton value="Inscribir" action="#{materiaController.inscribirMateria(materia.id)}"/>
    </h:form>
</c:if>
<c:if test="#{!materiaController.cursaMateria(materia.id)}">
    <h:form>
        <h:commandButton value="Desinscribir" action="#{materiaController.desinscribirMateria(materia.id)}"/>
    </h:form>
</c:if>

and this way:

<h:panelGroup rendered="#{materiaController.cursaMateria(materiaId) == true}">
    <h:column>
        <h:form>
            <h:commandButton value="Inscribir" action="#{materiaController.inscribirMateria(materiaId)}"/>
        </h:form>
    </h:column>
</h:panelGroup>
<h:panelGroup rendered="#{materiaController.cursaMateria(materiaId) == false}">
    <h:column>
        <h:form>
            <h:commandButton value="Desinscribir" action="#{materiaController.desinscribirMateria(materiaId)}"/>
        </h:form>
    </h:column>
</h:panelGroup>

and this:

<h:column>
    <h:form>
        <h:commandButton value="Inscribir" action="#{materiaController.inscribirMateria(materia.id)}" rendered="#{materiaController.cursaMateria(materia.id)}" />
        <h:commandButton value="Desinscribir" action="#{materiaController.desinscribirMateria(materia.id)}" rendered="#{!materiaController.cursaMateria(materia.id)}"/>
    </h:form>
</h:column>

can anybody help me on this?

thank's in advance.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Leonardo Cabré
  • 1,165
  • 1
  • 11
  • 17

1 Answers1

0

I suppose this is what are you searching for: for showing defined button you can use 'rendered' parameter of h:commandButton. In result you should have something like that:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

<h:head>
    <title>JSF Example</title>
</h:head>
<h:body>
    <h:dataTable value="#{materiaBean.getAll()}" var="materia">
        <h:column>
            <f:facet name="header">id</f:facet>
            #{materia.id}
        </h:column>
        <h:column>
            <f:facet name="header">Nombre</f:facet>
            #{materia.nombre}
        </h:column>
        <h:column>
            <h:form>
                <h:commandButton value="Inscribir" action="#{materiaBean.inscribirMateria(materia.id)}"
                                 rendered="#{not materiaBean.cursaMateria(materia.id)}"/>
                <h:commandButton value="Desinscribir" action="#{materiaBean.desinscribirMateria(materia.id)}"
                                 rendered="#{materiaBean.cursaMateria(materia.id)}"/>
            </h:form>
        </h:column>
    </h:dataTable>
</h:body>
</html>

For making the answer more demonstrative I also created managed bean:

package com.mberazouski.stackoverflow.components;

import com.mberazouski.stackoverflow.domain.Materia;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Managed bean for supporting operations with <code>materias</code>.
 * <p/>
 * Bean contains only stub methods with mock logic which have to be replaced with real business logic. It was created as
 * a quick example for the answer on
 * <a href="https://stackoverflow.com/questions/46626943/jsf-cwhen-and-cif-with-dynamic-parameters/46630158#46630158">
 * stackoverflow.com question</a>.
 *
 * @author mberazouski
 */
@ManagedBean
@ViewScoped
public class MateriaBean implements Serializable {

    /**
     * Stub method for returning collection of {@link Materia} objects.
     * <p/>
     * Initially method was implemented in such manner that returns collection of <code>3</code> objects with incremented
     * values for <code>id</code> and <code>nombre</code>.
     *
     * @return collection of three Materia objects
     */
    public List<Materia> getAll() {
        List<Materia> result = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            Materia materia = new Materia();
            materia.setId(i);
            materia.setNombre(String.valueOf(i));
            result.add(materia);
        }

        return result;
    }

    /**
     * Stub method for action related to controls which are interested in <code>Inscribir</code> action.
     * <p/>
     * In this realisation just print an id of the {@link Materia} for which was clicked the button.
     *
     * @param id id of the {@link Materia} for which was clicked the button
     */
    public void inscribirMateria(int id) {
        System.out.println("inscribirMateria method called with id: " + id);
    }

    /**
     * Stub method for action related to controls which are interested in <code>Desinscribir</code> action.
     * <p/>
     * In this realisation just print an id of the {@link Materia} for which was clicked the button.
     *
     * @param id id of the {@link Materia} for which was clicked the button
     */
    public void desinscribirMateria(int id) {
        System.out.println("desinscribirMateria method called with id: " + id);
    }

    /**
     * Stub method for determining which button type from two available should be displayed (<code>Desinscribir</code> or
     * <code>Inscribir</code>).
     * <p/>
     * Initially it returns true for each even number.
     *
     * @param id id of the {@link Materia} for which was clicked the button
     * @return <code>true</code> if id of Materia is even, <code>false</code> otherwise. <code>true</code> is corresponding
     * to <code>Desinscribir</code> button and <code>false</code> to <code>Inscribir</code>
     */
    public boolean cursaMateria(int id) {
        if (id % 2 == 0) {
            return true;
        } else {
            return false;
        }
    }
}

Resulting page will look like that:

enter image description here

Hope this will answer on your question.