gpt4 book ai didi

javascript - 在 spring mvc Web 应用程序中的 jsp 中使用 if 循环

转载 作者:行者123 更新时间:2023-12-01 02:51:30 25 4
gpt4 key购买 nike

我正在使用 jdbc 在 spring mvc 中做小型社交应用程序。我有一个管理面板,可以选择查看所有用户并对其进行管理。因此,在包含用户详细信息的表中,我有一个状态为“已启用”的列。我尝试编写一个循环,在下一列中写入 href,如果启用状态为 1,则写入 href 以禁用用户,如果启用状态为 0,则写入 href 以启用用户,但我失败了。

查看用户的控制者:

  @RequestMapping(value="/admin/view-users", method = RequestMethod.GET)
public ModelAndView viewUsers()
{
List<User> listUser =dao.getUsersList();
return new ModelAndView("view-users","listUser",listUser);

}

用于显示表中所有用户的 .jsp 代码片段。

<c:forEach var="aUser" items="${listUser}">   

<tr>
<td>${aUser.id}</td>
<td>${aUser.username}</td>
<td>${aUser.nickname}</td>
<td>${aUser.email}</td>
<td>${aUser.country}</td>
<td>${aUser.role}</td>
<td id="isEnabled">${aUser.enabled}</td>
<td id="enableOrDisableLink">

</td>
</tr>
</c:forEach>

我尝试用这样的方法来做到这一点:

  <script>
if( document.getElementById("isEnabled").value=="1" )
{
document.getElementById("enableOrDisableLink").innerHTML = "<a
href='disableUser/${aUser.id}'>Disable Account</a>";
}
else
{
document.getElementById("enableOrDisableLink").innerHTML = "<a
href='enableUser/${aUser.id}'>Enable Account</a>";
}
</script>

但是我在图片上看到了类似的东西。

Marked link is a link to enable disabled account of user with id=3, what is obviously wrong. So I assume that this script takes value of enabled correctly but has a problem with properly writing href in every row

最佳答案

你可以这样做:

<td id="enableOrDisableLink">
<c:if test="${aUser.enabled}">
<a href='disableUser/${aUser.id}'>Disable Account</a>
</c:if>
<c:if test="${not aUser.enabled}">
<a href='enableUser/${aUser.id}'>Enable Account</a>
</c:if>
</td>

关于javascript - 在 spring mvc Web 应用程序中的 jsp 中使用 if 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46938952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com