使用Thymeleaf th:href将两个值从HTML传递到Controller

时间:2017-08-10 15:12:22

标签: html spring thymeleaf

我想将2个值从html链接传递给我的控制器。

<a th:href="@{/classesTable/saveProfessor/{professorId}/{classesId} (profesorId=${professor.id}), (classesId=${classes.id})}"</a>

这是我的控制器

@RequestMapping(value = "/classesTable/saveProfessor/{professorId}/{classesId}")
public ModelAndView saveClassesProfessor(@RequestParam("classesId") long classesId,
                                         @RequestParam("professorId") long professorId) {

    Classes classes = classesRepository.findOne(classesId);
    Profesor profesor = professorRepository.findOne(professorId);

    classes.getProfesors().add(professor);
    profesor.getClasses().add(classes);

    return new ModelAndView("redirect:/classesTable");
}

首先,我只是像这样传递教授ID

<a th:href="@{/classesTable/saveProfessor/{professorId} (profesorId=${professor.id})"</a>

当我将鼠标悬停在链接上时,它会识别教授的ID。但是当我在上面的示例中添加classesId时,浏览器中的URL看起来像这样。

http://localhost:8080/classesTable/saveProfesor/%7BprofesorId%7D/19%20(profesorId=$%7Bprofesor.id%7D),

第19号实际上是我班级的ID。我收到404错误。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

你有一个拼写错误,你的网址的语法有点偏。它应该是这样的:

th:href="@{/classesTable/saveProfessor/{professorId}/{classesId} (professorId=${professor.id}, classesId=${classes.id})}"