【问题标题】:Passing two values from HTML to Controller using Thymeleaf th:href使用 Thymeleaf th:href 将两个值从 HTML 传递到控制器
【发布时间】:2018-01-18 22:15:19
【问题描述】:

我想将 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 错误。我在这里错过了什么?

【问题讨论】:

    标签: html spring thymeleaf


    【解决方案1】:

    你有一个错字,你的网址的语法有点不对劲。它应该是这样的:

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

    【讨论】:

    • 是的,问题在我的括号中。不知道我是怎么错过的。谢谢你的帮助。 :)
    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 2019-07-05
    • 2019-12-07
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    相关资源
    最近更新 更多