我正在尝试执行一个表单操作,在其中显示每个分组值的选定列表。用户在这些列表之一中选择一个值后,我想将该对象的名称作为参数放入按钮链接中,但参数值为空。我不知道该怎么做。
这是我的代码:
在我的 Controller 中:
@RequestMapping(value="/teacher/depts", method=RequestMethod.GET)
public String depts(Model model) {
model.addAttribute("deptsfields", fieldOfStudyService.findAllFieldsForAllDepartments());
model.addAttribute("field", new FieldOfStudy());
return "teacher/depts";
}
@RequestMapping(value="/deptsfields", method=RequestMethod.POST)
public String deptsfields(@ModelAttribute(value="field") FieldOfStudy field) {
return "teacher/depts";
}
还有我的 html 页面:
<form action="#" th:action="@{/deptsfields}" th:object="${field}" method="post">
<table>
<tr>
<th>Wydział</th>
<th>Kierunek</th>
</tr>
<tr th:each="entry : ${deptsfields}">
<td th:text="${entry.getKey().details.departmentFullName}"
th:value="${entry.getKey().details.departmentFullName}"
th:field="*{details.department.details.departmentFullName}"></td>
<td>
<select id="fieldslist" class="form-control" th:field="*{details.fieldOfStudyName}">
<option selected="selected" value="">Wybierz...</option>
<option th:each="field : ${entry.getValue()}" th:value="${field.details.fieldOfStudyName}" th:text="${field.details.fieldOfStudyName}"></option>
</select>
</td>
</tr>
</table>
<a th:href="@{/teacher/groups(field=${field.details.fieldOfStudyName}, dept=${field.details.department.details.departmentFullName})}" class="btn btn-info" role="button">Dalej</a>
</form>
单击按钮后,我将重定向到此页面:
http://localhost:8080/teacher/groups?field={here should be name but is empty}&dept={here should be name but is empty}
我做错了什么?
最佳答案
您的链接有误,请尝试此操作
<a th:href="@{'/teacher/groups?field=' + ${field.details.fieldOfStudyName} + '&dept=' + ${field.details.department.details.departmentFullName}}" class="btn btn-info" role="button">Dalej</a>
关于java - Spring Thymeleaf 将对象属性传递给 href 按钮提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41771498/