Spring Thymeleaf将对象属性传递给href按钮提交

时间:2017-01-20 20:04:47

标签: java spring thymeleaf

我正在尝试执行表单操作,其中显示每个分组值的选定列表。在用户选择其中一个列表中的值后,我想将此对象的名称作为参数放入按钮链接中,但参数值为空。我不知道怎么办。

这是我的代码:

在我的控制器中:

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的链接出错,请尝试此操作

  <a th:href="@{'/teacher/groups?field=' + ${field.details.fieldOfStudyName} + '&dept=' + ${field.details.department.details.departmentFullName}}" class="btn btn-info" role="button">Dalej</a>