繁体   English   中英

如何在 jsp 中使用 JSTL 标签?

[英]how to use JSTL Tag in jsp?

我不习惯 JSTL 标签库。 我学习了 JSTL for 循环的基本用法。 但是当我开始我的项目时,我得到了一些复杂性。 在这个阶段,我不知道如何使用 JSTL。 下面的 JSP 页面代码。

<%
    //get the group list for post
    GroupOperation gpo = new GroupOperation(DBConnection.getConnection());
    List<Groups> groupList = gpo.getGroups();
    pageContext.setAttribute("myGroup", groupList);

   //get the user post
  PostOperation postOperation = new PostOperation(DBConnection.getConnection());
  List<Posts> postList = postOperation.getAllPost();
  pageContext.setAttribute("psList", postList);
  if (postList == null) {
    System.out.println("there is no post to show");
    }
%>

 <%
         for (GroupMembers memgr : grm) {
              for (Posts p : postList) {
                if (memgr.getGroupId() == p.getGroupId()) {
%>
                 <h4><%= gpo.getSingleGroup(p.getGroupId()).getGroupName()%></h4>
<%
                }
            }
        }
%>

<%= gpo.getSingleGroup(p.getGroupId()).getGroupName()%>这是我最复杂的部分。 我将永远感谢您的回答。

首先,您需要在构建路径中添加 jstl jar 文件。 并使用以下代码,我希望它能帮助你并得到一些想法

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<c:forEach items="grm" var="memgr">
    <c:forEach items="postList" var="p">
        <c:if test="${memgr.groupId eq p.groupId}">
              <h4>
                    ${gpo.getSingleGroup(p.getGroupId()).groupName}
              </h4>
        </c:if>
    </c:forEach>
</c:forEach>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM