可能出錯的控制器方法:
在這里,我使用 <form method="POST> 轉到 URL "/create"
@GetMapping("/create")
public ModelAndView create() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/create.jsp");
return modelAndView;
}
這是 create.jsp
<%@ page isELIgnored="false"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Create</title>
</head>
<body>
<h1>Create author</h1>
<form method="POST" action="">
Id <input name="userId"> <br>
Last name: <input name="lastName"> <br>
First name: <input name="firstName"> <br>
Second name: <input name="secondName"> <br>
Phone: <input name="phone"> <br>
Hobby: <input name="hobby"> <br>
BitBucketUrl: <input name="bitBucketUrl"> <br>
<input type="submit" value="Create">
</form>
</body>
</html>
最后是@Postmapping 方法
@PostMapping
public ModelAndView createPerson(
@RequestParam("id") String id,
@RequestParam("lastName") String lastName,
@RequestParam("firstName") String firstName,
@RequestParam("secondName") String secondName,
@RequestParam("phone") String phone,
@RequestParam("hobby") String hobby,
@RequestParam("bitBucketUrl") String bitBucketUrl) {
personCache.create(Person.builder()
.setId(id)
.setFirstName(firstName)
.setLastName(lastName)
.setSecondName(secondName)
.setPhone(phone)
.setHobby(hobby)
.setBitBucketUrl(bitBucketUrl)
.build());
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("id", id);
modelAndView.addObject("lastName", lastName);
modelAndView.addObject("firstName", firstName);
modelAndView.addObject("secondName", secondName);
modelAndView.addObject("phone", phone);
modelAndView.addObject("hobby", hobby);
modelAndView.addObject("bitBucketUrl", bitBucketUrl);
modelAndView.setViewName("/all.jsp");
return modelAndView;
}
頁面 all.jsp 正在作業(在方法 @GetMapping("/all") 中實作)。
我想,當按下“創建”按鈕時,轉到“/all”頁面,在那里我可以看到一個新的人。但是我在終端中出現錯誤“不支持請求方法'POST'”,在瀏覽器中出現錯誤“type=Method Not Allowed,status=405”。
我還創建了默認頁面 index.jsp,并在 @PostMapping 方法中嘗試了 modelAndView.setViewName("/index.jsp"),但不幸的是,即使在這種情況下,當我推送“創建”瀏覽器時,也不會轉到 index.jsp
uj5u.com熱心網友回復:
默認情況下,瀏覽器僅執行獲取請求,因此使用獲取映射創建另一個控制器方法并將人員物件添加到模型中,以便您可以在視圖頁面中設定值,并且當您按下提交時給予后期映射以便您可以分配值在視圖頁面中輸入到人員物件。
uj5u.com熱心網友回復:
瀏覽器地址欄發送 GET 請求。您可以像 Postman 一樣使用 smth 來發送您需要的請求。
uj5u.com熱心網友回復:
嘗試為表單標記提供除 root 以外的任何其他操作,并在映射后提供相同的操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/417597.html
標籤:
