我正在开发Spring + Thymeleaf应用程序。我正在用多个参数实现搜索。我有一个表格与相应的DTO。以下是DTO的代码:
public class ClassSearchDto {
private String searchParam;
private Long programId;
private List<String> teacherNames;
//getters, setters and constructor are omitted
}
如您所见,我的DTO中有一个名为teacherNames
的字符串列表。这是我显示表单的方式:
<form th:action="@{/classes/search}" method="get" th:object="${classSearchDto}">
<div class="form-group">
<input type="hidden" class="form-control"
th:value="${classSearchDto.programId}" th:field="*{programId}"/>
<label for="searchParam">Search</label>
<input type="text" class="form-control" id="searchParam" placeholder="keyword"
th:value="${classSearchDto.searchParam}" th:field="*{searchParam}"/>
<div>
<th:block th:each="name, iter ${classSearchDto.teacherNames}">
<input th:value="${name}" th:field="*{teacherNames[__${iter.index}__]}/>
</th:block>
</div>
</div>
<button class="btn btn-default" type="submit">Find</button>
</form>
我想在后端使用@RequestParam
注释帮助实现我的搜索。这是我的控制者:
@RequestMapping(value = "/search")
public String findClassByName(@RequestParam("searchParam") final String searchParam,
@RequestParam("programId") final Long programId,
@RequestParam("teacherNames") final List<String> teacherNames,
final Model model) {
...
}
问题在于我无法以这种方式获得教师姓名列表。我得到了这个例外:
org.springframework.web.bind.MissingServletRequestParameterException:Required List parameter 'teacherNames' is not present
请您帮我用这种方法将DTO中的元素列表转移到我的后端?也许你知道如何以另一种方式正确地做到这一点。提前谢谢。
答案 0 :(得分:0)
我可以建议你一件事,我不知道它是否有效。尝试更改
public String findClassByName(@RequestParam("searchParam") final String searchParam,@RequestParam("programId") final Long programId,@RequestParam("teacherNames") final List<String> teacherNames,final Model model)
到
public String findClassByName(@ModelAttribute("classSearchDto") ClassSearchDto classSearchDto,@RequestParam("searchParam") String searchParam,@RequestParam("programId") Long programId,@RequestParam("teacherNames") List<String> teacherNames,Model model)