asl*_*lan 5 javascript datatable jsp many-to-many jstl
我一直在寻找,但没有找到任何解决方案来做以下事情.
我试图显示一个包含让我们说用户列表的表.显示来自用户的字段并不是什么大问题,但对于关联字段来说它更复杂.假设我有以下表格:User,Post和UserPosts,在UserPosts中我们有一个标志main来指示给定帖子是否是用户的主要位置.
在我的数据表中,我必须显示用户的信息,它表示用户表中的字段+他/她的主要帖子.
到目前为止,我有这样的事情:
<table>
<thead>
<tr>
<th>name</th>
<th>FrstName</th>
<th>Age</th>
<th>Main position</th>
<th>Action</th>
</tr>
<tbody>
<c:forEach items="${listUsers}" var="user" varStatus="status">
<tr>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.firstName}" /></td>
<td><c:out value="${user.age}" /></td>
<td><c:out value="Position here" /></td>
<td>
Delete
<a href="/myAppli/user/${user.idUser}">Update</a>
</td>
</tr>
</c:forEach>
</tbody>
</thead>
Run Code Online (Sandbox Code Playgroud)
我认为对于位置我将不得不从给定列中给定用户的数据库主位置检索,但是如何设置此值(javascript,ajax?)
我是否以错误的方式思考,我应该使用关联类.我的意思是,检索main field = true的所有UserPosts?然后迭代userPosts而不是用户,在这种情况下,我会有这样的事情:
<c:forEach items="${listUserPosts}" var="userPost" varStatus="status">
<tr>
<td><c:out value="${userPost.user.name}" /></td>
<td><c:out value="${userPost.user.firstName}" /></td>
<td><c:out value="${userPost.user.age}" /></td>
<td><c:out value="${userPost.post.postName}" /></td>
<td>
Delete
<a href="/myAppli/user/${userPost.user.idUser}">Update</a>
</td>
</tr>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很奇怪,也许我错了.
如果我正确理解你的数据的布局,你会得到这样的东西:
如果是这种情况,您可以像这样构造您的 User 对象:
public class User {
private int id;
private String name;
private String firstName;
private int age;
private Position mainPosition;
}
Run Code Online (Sandbox Code Playgroud)
您的查询只需要对标记为“main”的 UserPosition 进行左连接。然后该 UserPosition 将加入到正确的 Position。然后,您的 JSP 只需要检查您的 Position 对象是否为空。
<c:forEach items="${listUsers}" var="user" varStatus="status">
<tr>
<td><c:out value="${user.name}" /></td>
<td><c:out value="${user.firstName}" /></td>
<td><c:out value="${user.age}" /></td>
<td>
<c:if test="empty ${user.position}">
No main position
</c:if>
<c:if test="not empty ${user.position}">
<c:out value="${user.position.title}" />
</c:if>
</td>
<td>
Delete
<a href="/myAppli/user/${user.idUser}">Update</a>
</td>
</tr>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)