Blaze Blaze - 4 months ago 10
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">
<tr>
<th>Department ID</th>
<th>Department Name</th>
<th>Options</th>
</tr>

<c:forEach items="${departments}" var="department">
<tr>
<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>
</tr>
</c:forEach>
</table>
</c:if>


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

<tr>
<th>Department ID</th>
<th>Department Name</th>
<th>Options</th>
</tr>


cannot figure out what could be wrong.

ck1 ck1
Answer

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

return new ModelAndView("addDepartment", model);

References:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/ModelAndView.html#ModelAndView-java.lang.String-java.util.Map-

Comments