我有类别树。我的类别有 n 个子类别。如何在 jsp 上列出递归类别?
List<Category> categories = categoryService.findAll();
modelMap.put("categories",categories)
我有类别树。我的类别有 n 个子类别。如何在 jsp 上列出递归类别?
List<Category> categories = categoryService.findAll();
modelMap.put("categories",categories)
使用 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);
}
}
}