Blaze Blaze - 3 months ago 7x
Java Question

if test not showing section of code - spring mvc

I have a region in my jsp that I want to display a list of the departments by testing if the list is not empty. This is the code in my addDeparment.jsp file

<c:if test="${!empty departments}">
<table align="center" border="1">
<th>Department ID</th>
<th>Department Name</th>

<c:forEach items="${departments}" var="department">
<td><c:out value="${department.departmentId}"/></td>
<td><c:out value="${department.departmentName}"/></td>
<td align="center"><a href="editDepartment?departmentId=${department.departmentId}">Edit</a> | <a href="deleteDepartment?departmentId=${department.departmentId}">Delete</a></td>

this is the controller that gets all the departments and attaches them into a HashMap collection

@RequestMapping(value = "/addDepartment", method = RequestMethod.GET)
public ModelAndView addCategory(@ModelAttribute("command") Department department,
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("departments", departmentService.getDepartments());
return new ModelAndView("addDepartment");

If I run the code, this section of the view in the jsp file does not display

<th>Department ID</th>
<th>Department Name</th>

cannot figure out what could be wrong.

ck1 ck1

In the controller, you need to pass your model to the ModelAndView object as follows:

return new ModelAndView("addDepartment", model);