gpt4 book ai didi

java - 如果测试不显示代码部分 - spring mvc

转载 作者:行者123 更新时间:2023-12-01 09:41:36 27 4
gpt4 key购买 nike

我的 jsp 中有一个区域,我想通过测试列表是否不为空来显示部门列表。这是我的 addDeparment.jsp 文件中的代码

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

这是获取所有部门并将它们附加到 HashMap 集合中的 Controller

@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");
}

如果我运行代码,jsp 文件中的 View 的这一部分不会显示

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

无法弄清楚可能出了什么问题。

最佳答案

在 Controller 中,您需要将模型传递给 ModelAndView 对象,如下所示:

return new ModelAndView("addDepartment", model);

引用文献:

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

关于java - 如果测试不显示代码部分 - spring mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411502/

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