在我的Spring应用程序中,我在User和Area之间有一个ManyToMany关系,用户可以将Area保存到Area对象的ArrayList。用户可以查看他们的个人资料,并在jsp页面上以表格的形式查看他们保存的区域。我在表格中有一个标题值用于区域名称,下面的行将显示区域对象的用户ArrayList中的每个区域,用于查看Google Map上的区域的链接以及用于从其ArrayList中删除区域的按钮。
// Loop to go through the ArrayList of Areas
<c:forEach var="o" items="${savedAreas}">
<tr>
// Display the name of the area
<td width="10%" height="50"><c:out value="${o.areaName}" /></td>
// View area on the map
<a href = "welcome"> View on the map</a></td>
// Button to pass the particular area object to the "deletearea"
// method in the Controller in the Spring application to run the
// delete method and remove the area object from the ArrayList
<td width="3%" height="50" class="tg-yw4l">
<form th:action="@{/deletearea}" method="post" th:object="${area}">
<button type="submit" value="Submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
</c:forEach>
区域对象应该传递给Controller中的以下方法。
@RequestMapping(value = "/deletearea", method = RequestMethod.POST)
public String deleteArea(@Valid Area area, BindingResult bindingResult, Model model) {
// Gets the current logged in user
Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
String username = loggedInUser.getName(); // Authentication for
//Finds the user details in the database
User user = userRepository.findByUsername(username);
// Calls deleteArea from User Class and deletes area object from
// Arralist. Will include this method below
user.deleteArea(area);
// Saves update to user
userRepository.save(user);
//Simple print debug
System.out.println(user + " deleted : " + area);
// Refresh the jsp page
return "savedAreas";
}
在Controller中的方法中调用deleteArea。它来自User Object类。
public void deleteArea(Area area) {
if (!savedAreas.isEmpty()){
savedAreas.remove(area);
}
}
问题是当我按下删除按钮时出现以下错误。
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
对于我如何解决这个问题,有人会有任何建议吗?干杯