c:when和c:如果使用h:dataTable var的动态参数

时间:2017-10-08 02:18:18

标签: jsf datatable jstl

我对JSF和JAVA也很陌生,但我在大学项目上工作,我遇到了一些无法解决的问题:

基本上即时通讯我试图显示一个按钮,如果一个人没有写入一个课程说" inscribir"如果被铭刻,则采取行动来记录它和对立面。

检查我是否正在尝试使用materiaController.cursaMateria(materia.id)传递材料ID(材料是课程),以检查注册用户是否记入该课程。

这是我的代码:

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

问题在于方法&#34; cursaMateria&#34;始终将参数设为null。顺便说一下,第一个列(id)打印出相应的id。

我也尝试其他方式,但总是一样,不能发送参数:

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

这样:

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

和此:

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

有人可以帮我吗?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我想这就是你要搜索的内容:要显示已定义的按钮,你可以使用&#39;呈现&#39;参数h:commandButton。结果你应该有类似的东西:

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

为了使答案更具说明性,我还创建了托管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;
        }
    }
}

结果页面如下所示:

enter image description here

希望这会回答你的问题。