0

我有类别树。我的类别有 n 个子类别。如何在 jsp 上列出递归类别?

List<Category> categories = categoryService.findAll();
modelMap.put("categories",categories)
4

1 回答 1

0

使用 JSTL,尝试这样的事情:

简单的物品清单

<c:forEach var="category" items="${categories}">
   ${category.id} -> ${category.name}
</c:forEach>

嵌套项目列表

如果您有嵌套类别,请尝试以下自定义标签。

public class CategoryDisplayTag extends TagSupport
{
   public int doStartTag() throws JspException
   {
      Category rootCategory = new Category();

      printEachCategory(rootCategory);

      return SKIP_BODY;
   }

   private void printEachCategory(Category category)
   {
      JspWriter out = pageContext.getOut();

      try
      {
         out.write("Category: " + category.getName());

         for (Category c : category.getCategories())
         {
            out.write("Sub-category: " + c.getName());
            printEachCategory(c);
         }
      }
      catch (IOException e1)
      {
         throw new RuntimeException(e1);
      }

   }  
}
于 2012-11-15T13:28:38.377 回答