下面给出的是我的 JSP 页面中的代码 Sample.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Spring Boot</title>
</head>
<body>
<br>
<br>
<h1>Spring Boot Application</h1>
<br>
<br>
<form action="sample">
<label>Name</label>
<input type="text" name="name" required></input>
<br>
<br>
<label>Description</label>
<input type="text" name="description" required></input>
<br>
<br>
<label>Price</label>
<input type="text" name="price" required></input>
<br>
<br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
这是我的控制器类中的代码 SampleController.java
@Controller
@RequestMapping("/books")
public class BooksController {
@RequestMapping("/sample")
public String saveBook(@RequestParam("name") String name,@RequestParam("price") int price,
@RequestParam("description") String description)
{
System.out.println(name+" "+price+" "+description);
}
}
当我改变时
<form action="sample">
到
<form action="sample" method="POST">
当我改变时
@RequestMapping("/sample")
到
@PostMapping("/sample")
一旦我这样做,我就会收到 403 禁止错误。我无法理解为什么,因为据我所知,当我们向服务器提交某些内容时必须使用 @PostMapping,并且显然我正在传递一些细节,例如我打算存储在数据库中的名称、描述、价格,但是一旦使用@PostMapping,我就会出错。我哪里错了?什么时候应该使用@PostMapping,什么时候应该使用@RequestMapping?