Skip to main content
Kinetic Community

Exercise Four - adding conditionals

Overview

In the first exercise we displayed all of the categories of the kapp and all of the subcategories each category had.  Categories will show even if they are empty.  An empty category is one that doesn't have any active forms or subcategories in it.  In this exercise we will add conditions to the catgories.jsp file and the subcategory.jsp file that will check to see if the category is empty or not.

Status

Part 1 status of code

Below is what the code in the categories.jsp file should currently look like with the addition of comment where the conditionals closing tags should be added.

<%@page pageEncoding="UTF-8" contentType="text/html" trimDirectiveWhitespaces="true"%>
<%@include file="../bundle/initialization.jspf" %>

<c:set var="categories" value="${CategoryHelper.getCategories(kapp)}" scope="request"/>

<bundle:layout page="../layouts/layout.jsp">
  <ul>
    <c:forEach var="category" items="${categories}">
      
      <!-- Replace this with step 1 of Part 1 of exercise 4.  Wrap conditional logic to only display the category, 
      it's forms and subcategories if the category has active forms and/or it has a subcategory that has an active form. -->
      
      <li>${text.escape(category.name)}</li>
      <li>
        <ul>
          <c:forEach var="form" items="${category.forms}">
            <li>${text.escape(form.name)}</li>
          </c:forEach>
        </ul>
      </li>
        
      <%-- Replace this with step 3 of Part 1 of exercise 4.  Wrap conditional logic to only display subcategories that have active forms.  --%>

      <li>
        <ul>
          <%-- Recursive Subcatgegories --%>
          <c:set scope="request" var="currentCat" value="${category}"/>
          <c:import url="${bundle.path}/partials/subCategory.jsp" charEncoding="UTF-8" />
        </ul>
      </li>
        
      <%-- This is where the the closeing tag of the two if conditions goes --%>
          
    </c:forEach>
  </ul>
</bundle:layout>

Part 2 status of code

Below is what the code in the subCategory.jsp file should currently look like with the addition of comment where the conditionals closing tags should be added.

<%@include file="../bundle/initialization.jspf" %>

<c:forEach var="subcategory" items="${currentCat.getSubcategories()}">

  <!-- Replace this with step 1 of Part 2 of exercise 4.  Wrap conditional logic to only display the category,
  it's forms and subcategories if the category has active forms and/or it has a subcategory that has an active form. -->

  <li>${text.escape(subcategory.name)}</li>    
  <li>
    <ul>
      <c:forEach var="form" items="${subcategory.forms}">
        <li>${text.escape(form.name)}</li>
      </c:forEach>
    </ul>
  </li>

  <%-- Replace this with step 3 of Part 2 of exercise 4.  Wrap conditional logic to only display subcategories that have active forms.  --%>

  <li>
    <ul>
      <c:set var="currentCat" value="${subcategory}" scope="request"/>
      <jsp:include page="subCategory.jsp"/>
    </ul>
  </li> 
  
  <%-- This is where the the closeing tag of the two if conditions goes --%>
  
</c:forEach>

Activity

Part 1 add conditionals to categories.jsp file

  1. In the categories.jsp add a conditional using the JSTL tag <c:if> that will check to see if the category has any forms or nonempty subcategories.
<c:if test="${category.hasForms() || category.hasNonEmptySubcategories()}">
  1. Add the first closing tag, </c:if>,where the comment instructs you to place the closing tag.
  2. Replace the comment with the below code.  The code checks if the category has subcategories before injecting the subcategory.jsp file.
 <c:if test="${category.hasNonEmptySubcategories()}">
  1. Add the second closing tag, </c:if>, where the comment instructs you to place the closing tag.

Part 2 add conditionals to subCategory.jsp file

  1. In the subCategory.jsp add a conditional using the JSTL tag <c:if> that will check to see if the category has any forms or nonempty subcategories.
 <c:if test="${subcategory.hasForms() || subcategory.hasNonEmptySubcategories()}">
  1. Add the first closing tag, </c:if>,where the comment instructs you to place the closing tag.
  2. Replace the comment with the below code.  The code checks if the category has subcategories before injecting the subCategory.jsp file.
<c:if test="${subcategory.hasNonEmptySubcategories()}">
  1. Add the second closing tag, </c:if>, where the comment instructs you to place the closing tag.