使用jsp el显示来自不同组的用户列表

时间:2015-03-29 21:28:25

标签: java jquery jsp servlets

我正在尝试使用手风琴显示组中的成员列表。我为此目的使用jsp,el和servlets。

这是用于理解问题的两个数据库表

enter image description here

enter image description here

上面分别是两个表-groups和group_members ..我正在从它们访问数据并将它们存储在两个java对象中并列出这两个对象。

我的servlet如下:;

//current user's user-name
    String currentUser = request.getParameter("username");

    String group_name=null;

    String GetGroupInfo = "select * from groups where creator_username=?";
    //get all the groups where the creator's user-name is currentUser


    try {

        List<Group> groups = new ArrayList<Group>();
        List<GroupDetails> groupList = new ArrayList<GroupDetails>();

        ps3 = currentCon.prepareStatement(GetGroupInfo);
        ps3.setString(1,currentUser);
        rs3 = ps3.executeQuery();

        //set values for Group object
        while(rs3.next())
        {
            Group groupObj = new Group();

            groupObj.setGroup_id(rs3.getString("group_id"));
            groupObj.setGroup_name(rs3.getString("group_name"));
            groupObj.setGroup_description(rs3.getString("group_description"));
            groupObj.setCreator_username(rs3.getString("creator_username"));
            groupObj.setCreated_on(rs3.getString("created_on"));

            groups.add(groupObj);

            String query = "select * from group_members where creator_username=? and group_id=?";

            ps = currentCon.prepareStatement(query);
            ps.setString(1,currentUser);
            ps.setString(2, groupObj.getGroup_id());

            rs = ps.executeQuery();


            while(rs.next())
            {
                GroupDetails groupInfo = new GroupDetails();

                groupInfo.setIs_admin(rs.getString("is_admin"));
                groupInfo.setAdded_on(rs.getString("added_on"));
                groupInfo.setCreator_username(rs.getString("creator_username"));
                groupInfo.setGroup_name(rs.getString("group_name"));
                groupInfo.setGroup_id(rs.getString("group_id"));
                groupInfo.setUser_name(rs.getString("user_name"));
                groupInfo.setMember_id(rs.getString("member_id"));

                String memberFullname = "select firstname,lastname,user_type from users where username ='" + groupInfo.getUser_name() + "'";

                ps2 = currentCon.prepareStatement(memberFullname);
                rs2 = ps2.executeQuery();

                if(rs2.next())
                {
                    String member_fullname = rs2.getString("firstname") + " " + rs2.getString("lastname");
                    groupInfo.setMember_name(member_fullname);
                    groupInfo.setMember_usertype(rs2.getString("user_type"));
                }

                group_name = groupInfo.getGroup_name();
                groupList.add(groupInfo);
            }

        }

        //request group name
        request.setAttribute("group_name",group_name);
        request.setAttribute("individual_group", groups);
        request.setAttribute("groupList", groupList);
        request.setAttribute("currentUser", currentUser);

        RequestDispatcher rd = request.getRequestDispatcher("/ViewAllExistingGroups");
        rd.forward(request, response);
    } catch (SQLException e) {
        e.printStackTrace();
    }   

以手风琴风格显示组的Jsp页面 - 组名称显示在手风琴标题中,成员显示在手风琴内容中。

<body>
<% String username = (String)request.getAttribute("currentUser"); %>
    <div class="container">
        <div class="panel panel-default">
            <div class="panel panel-heading">

            </div>

            <div class="panel panel-body">


                                <div id="accordion">
                            <c:forEach var="group1" items="${individual_group}">


                                    <h5> ${group1.group_name}</h5>

                                    <div>
                                    <ul class="list-group"> 
                                        <li class="list-group-item title">
                                            <strong style="display:inline;"> About: <h5 style="display: inline;"> ${group1.group_description}</h5></strong>
                                        </li> 
                                        <c:forEach var="group" items="${groupList}">
                                            <li class="list-group-item title">
                                            <img src="${pageContext.request.contextPath}/css/images/user.png" class="img img-circle" style="display: inline" />
                                            <strong style="display:inline;">  ${group.member_name} <h5 style="display: inline;">(${group.member_usertype})</h5></strong>
                                            </li> 
                                        </c:forEach>
                                    </ul>
                                    </div>

                            </c:forEach>
                        </div>


                <br/>
                <form action="ListConnectedUsers" method="get">
                    <input type="hidden" name="username" value="<%=username %>" />
                    <input type="submit" class="btn btn-default pull-left" value="Back"/>               
                </form>
            </div>

            <div class="panel panel-footer">

            </div>
        </div>
    </div>
</body>

这是我在jsp页面中得到的内容。但所有成员都在每组手风琴小组中展示

enter image description here

每个手风琴部分都显示了group_members表中的所有成员。 我想让它在'BEIT教室'中展示三名成员,在'3 BUDDIES'中展示三名成员,在'SUDD GROUP'中展示一名成员

如果需要以我解释的方式呈现,我希望得到jsp el或任何条件的帮助。

很抱歉这么长的问题,但我试过正确解释。请帮忙

1 个答案:

答案 0 :(得分:0)

我想我可以创建一个对象,我可以将groupname和组成员全名捆绑在一起。