【问题标题】:c:when and c:if with dynamic parameters from h:dataTable var [duplicate]c:when 和 c:if 带有来自 h:dataTable var [重复] 的动态参数
【发布时间】:2018-03-19 12:16:57
【问题描述】:

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

基本上我试图显示一个按钮,如果一个人没有被刻录到一个说“inscribir”的课程中,并且有一个动作来刻录它,如果被刻录了相反的动作。

检查是否正在尝试使用 materiaController.cursaMateria(materia.id) 传递 materia 的 id (materia is course) 来检查登录的用户是否已注册该课程。

这是我的代码:

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

问题是“cursaMateria”方法总是将参数设为空。顺便说一句,第一列 (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>

有人可以帮我吗?

先谢谢了。

【问题讨论】:

    标签: jsf datatable jstl


    【解决方案1】:

    我想这就是您要搜索的内容:要显示已定义的按钮,您可以使用 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;
            }
        }
    }
    

    结果页面将如下所示:

    希望这能回答您的问题。

    【讨论】:

    • 谢谢@MikitaBerazouski 它有效,
    • @LeonardoCabré 欢迎您。祝你好运。
    猜你喜欢
    • 2012-11-14
    • 1970-01-01
    • 2011-03-22
    • 2023-03-10
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2014-11-15
    相关资源
    最近更新 更多