繁体   English   中英

Thymeleaf url加问号

[英]Thymeleaf adds question mark to url

我目前正在开发一个使用 spring 引导和 thymeleaf 的程序,遇到了一些奇怪的事情。 使用 th:action="/addEmployee" 提交表单后,它会将我重定向到所需的视图,但 url 看起来像这样 'http://localhost:8050/addEmployee?' 而不是这个“http://localhost:8050/addEmployee”。 它增加了一个? mark even tough 我不想添加 pathParams。

看法:

<!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org" lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Departments</title>
    </head>
    <body>
        <h1>Department Administration</h1>
        <form method="get" th:action="@{/addEmployee}">
        <div>New Employee: <button type="submit">Add</button></div>
    </form>
  
   </body>
   </html>

Controller:

   @Slf4j
   @Controller
   @RequestMapping("/addEmployee")
   @SessionAttributes({"department", "sortOrder"})
   @RequiredArgsConstructor
   public class EmployeeController {

       private final EmployeeRepository employeeRepository;


       @GetMapping
       public String displayForm(Model model, @SessionAttribute("department") Department department,
                              @SessionAttribute("sortOrder") Boolean sortOrder){
           model.addAttribute("department", department);
           model.addAttribute("employee", new Employee());

           return "employeeView";
       }

   }

我真的不知道出了什么问题,因为我以前的项目中没有出现意想不到的问号。

如果您需要其他代码,请告诉我。

欣赏它!

这是因为GET在URL字符串中附加请求参数,而POST在消息体中携带请求参数。 您应该将语法更改为

<form method="post" th:action="@{/addEmployee}">

对于此方法,您的 controller 应该使用@PostMapping进行注释。

您需要更密切地关注文档中的示例,直至 controller。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM