使用 Thymeleaf th:href 将两个值从 HTML 传递到控制器 [英] Passing two values from HTML to Controller using Thymeleaf th:href

查看:64
本文介绍了使用 Thymeleaf th:href 将两个值从 HTML 传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to pass 2 values from html link to my controller.

<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

First I was just passing professor ID like this

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

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

And it would recognize ID from professor when I would hover over link. But when I added classesId like in example above, URL in my browser looks like this.

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

19 号实际上是我班级的 ID.我收到 404 错误.我在这里错过了什么?

Number 19 is actually ID of my classes. I am getting 404 error. What am I missing here?

推荐答案

你有一个错字,你的 url 的语法有点不对.它应该是这样的:

You have a typo and the syntax of your url is a bit off. It should look like this:

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

这篇关于使用 Thymeleaf th:href 将两个值从 HTML 传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆