我遇到了web应用程序的问题。我是一名学生,仍在学习。这是一个简单的论坛,通过登录,注册,添加新主题和帖子。也可以删除没有问题的主题GetMapping方法。不幸的是,我得到一个命令来更改GetMapping为Delete,并且在更改得到内容应用程序的错误之后: “出现了意外错误(type = Method Not Allowed,status = 405)。请求方法'GET'不支持' 我在网上寻找解决这个问题的方法,但是在查看了各种选项后,仍然是一样的错误,在这个话题上也没有足够的经验,所以很多对我的说明都不清楚。请帮助不支持请求方法'GET'更改后发生错误@GetMapping删除方法
所以这是删除更改之前topic.html观点:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Forum - Topic</title>
</head>
<body background="http://2.bp.blogspot.com/-BsL9gRE80Ug/U0OgeWbbxtI/AAAAAAAAF-w/teXrzw-TBcU/s1600/nacre-background-tile.jpg">
<table class="table table-striped">
<a href="http://localhost:8080/forum">Powrot</a>
<tr>
<th>Title: </th>
\t <th><p th:text="${topicName}" /></th>
</tr>
<tr>
\t <td th:text="${topicAuthor}" ></td>
\t <td th:text="${topicDate}" ></td>
\t <table border="1">
\t <td th:text="${topicContent}"></td>
\t </table>
</tr>
<table>
\t \t <br><b>-------------------------------------------------------</b></br>
\t \t <a th:href="@{/new_message(id=${topicId})}">Add new post</a>
\t \t <br><a th:href="@{/delete(id=${topicId})}">Delete this topic</a></br>
\t \t <br><b>-------------------------------------------------------</b></br>
</table>
</table>
<table class="table table-striped">
<tr th:each="message,iterStat : ${list}">
<td th:text="${message.author}"></td>
<td th:text="${message.date}"></td>
<table border="1">
\t <td th:text="${message.content}"></td>
</table>
<table>
\t <p> - - - - - - - - </p>
</table>
</tr>
</table>
</body>
</html>
和变化后:
...
<table>
<br><b>-------------------------------------------------------</b></br>
<a th:href="@{/new_message(id=${topicId})}">Add new post</a>
<form action="#" th:action="@{/delete(id=${topicId})}" method="delete">
<br><a th:href="@{/delete(id=${topicId})}">Delete this topic</a></br>
</form>
<br><b>-------------------------------------------------------</b></br>
</table>
...
另外我编辑控制器。这是以前工作正常版本getMapping:
@Controller public class TopicController
{
@Autowired
private TopicRepository topicRepository;
@Autowired
private MessageRepository messageRepository;
@Autowired
private UserRepository userRepository;
@GetMapping("/delete")
public String deleteTopic(@CookieValue(value = "userId", defaultValue = "-1") String userId,
@RequestParam("id") String topicId, Model model)
{
if(userId.equals("-1"))
{
model.addAttribute("user", new User());
return "login";
}
else
{
Topic topic = topicRepository.findByIdIn(Integer.parseInt(topicId));
if(topic.getUserId() == Integer.parseInt(userId))
{
topicRepository.delete(topic);
}
return "redirect:/forum";
}
}
}
,新的版本,不工作:
@RequestMapping(value = "/{delete}", method = RequestMethod.DELETE)
public @ResponseBody String deleteTopic(@CookieValue(value = "userId", defaultValue = "-1") String userId,
@RequestParam("id") String topicId, @ModelAttribute Topic topic, Model model)
{
if(userId.equals("-1"))
{
model.addAttribute("user", new User());
return "login";
}
else
{
Topic topicDB = topicRepository.findByIdIn(Integer.parseInt(topicId));
if(topicDB.getUserId() == Integer.parseInt(userId))
{
topicRepository.delete(topicDB);
}
return "redirect:/forum";
}
}
嗯,也许我做错了什么,但这些方法都没有帮助:( – Mastrin1994
你正确注册HiddenHttpMethodFilter?有文档中看看(http://docs.spring.io/spring -boot/docs/current/reference/html/howto-embedded-servlet-containers.html#howto-add-a-servlet-filter-or-listener)来获得详细信息。/26157610/2847174)包含另一个过滤器的示例 –
所以,我正在和我的老师谈话,他告诉我我有两个选项: 1.制作javascript,然后使用删除或 2.制作新控制器,只需删除然后用邮递员检查控制器的工作是否正确 我选择了第二个选项,并能够完成这项工作。我对该主题进行了积极评估,因此您可以将此视为解决方案的一部分。 – Mastrin1994