T.Seroka
T.Seroka

Reputation: 13

Loop with if, in jsp in spring mvc web application

I'm doing small social application in spring mvc with jdbc. I have an admin panel, with option to view all users and to manage with them. So in the table with users details I have a column with "enabled" status. I tried to write a loop that will write href in next column, a href to disable user if his enabled status is 1, and href to enable user if his enabled status is 0, but I have failed.

Controller of viewing users:

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

  }

Snippet of .jsp code to display all users in table.

<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>  

I tried to do it with something like this:

  <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>

But I got something like this on the picture.

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

Upvotes: 0

Views: 1207

Answers (3)

T.Seroka
T.Seroka

Reputation: 13

Works great, just had to change to

 test = "${aUser.enabled == 1}" , because enabled isn't boolean type.

Upvotes: 0

Miroslav Ligas
Miroslav Ligas

Reputation: 1307

Loop and ID attribute does not go very well together. I would ditch the whole JS part and use <c:choose><c:test ><c:otherwise> to do the condition.

Upvotes: 1

Jack Flamp
Jack Flamp

Reputation: 1233

you can do this:

<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>

Upvotes: 1

Related Questions