【发布时间】:2017-07-17 09:32:50
【问题描述】:
我的网络应用程序有问题。我是学生,还在学习。这是一个简单的论坛,包括登录、注册、添加新主题和帖子。也可以删除没有问题的主题 GetMapping 方法。不幸的是,我得到了一个将 GetMapping 更改为 Delete 的命令,并且在更改后的内容应用程序中出现错误: “出现意外错误(类型 = 不允许的方法,状态 = 405)。请求方法 'GET' 不支持' 我在网上寻找解决这个问题的方法,但是在检查了各种选项后,它仍然是同样的错误。我在这个主题上也没有足够的经验,因此对我来说很多说明都不清楚。所以请帮忙。
所以这是删除更改之前的 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>
<th><p th:text="${topicName}" /></th>
</tr>
<tr>
<td th:text="${topicAuthor}" ></td>
<td th:text="${topicDate}" ></td>
<table border="1">
<td th:text="${topicContent}"></td>
</table>
</tr>
<table>
<br><b>-------------------------------------------------------</b></br>
<a th:href="@{/new_message(id=${topicId})}">Add new post</a>
<br><a th:href="@{/delete(id=${topicId})}">Delete this topic</a></br>
<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">
<td th:text="${message.content}"></td>
</table>
<table>
<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";
}
}
【问题讨论】:
标签: javascript java spring-mvc web-applications hibernate-mapping