2017-10-08 23 views
0

即時通訊工具和JAVA也很新穎,但是我在大學項目上工作,並且遇到了一些無法解決的問題:c:when和c:if動態參數來自h:dataTable var

基本上我試着去展現一個按鈕,如果一個人沒有落款的課程,說:「inscribir」與動作銘刻它與oposite如果刻。

檢查是否題名即時通訊設法使用materiaController.cursaMateria(materia.id)通過材料的id(materia當然)檢查是否登錄的用戶被寫入該課程。

這裏是我的代碼:

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

可以有人幫助我呢?

提前致謝。

回答

0

我想這是你在尋找:用於顯示定義按鈕,您可以使用「渲染」的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

希望這會回答你的問題。

+0

謝謝@MikitaBerazouski它的工作原理, –

+0

@LeonardoCabré歡迎您。祝你好運。 –