2

我遇到了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"; 
    } 
} 

回答

0

DELETE方法HTML表單是不支持。所以,當你寫下面的內容時,你的瀏覽器只是使用正常的GET

<form action="#" th:action="@{/delete(id=${topicId})}" method="delete"> 

使用POST方法,因爲您更改了服務器上的數據。如果你想使應用程序認爲使用了DELETE方法,使用HiddenHttpMethodFilter隱藏欄是這樣的:

<form action="#" th:action="@{/delete(id=${topicId})}" method="post"> 
    <input type="hidden" name="_method" value="delete"> 
    <br><a th:href="@{/delete(id=${topicId})}">Delete this topic</a></br> 
</form> 

如果使用thymeleaf-spring> = 2.0.3可以使用th:method屬性和thymeleaf將創建如果需要,可以自動隱藏。

<form action="#" th:action="@{/delete(id=${topicId})}" th:method="delete"> 
    <br><a th:href="@{/delete(id=${topicId})}">Delete this topic</a></br> 
</form> 
+0

嗯,也許我做錯了什麼,但這些方法都沒有幫助:( – Mastrin1994

+0

你正確註冊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)包含另一個過濾器的示例 –

+0

所以,我正在和我的老師談話,他告訴我我有兩個選項: 1.製作javascript,然後使用刪除或 2.製作新控制器,只需刪除然後用郵遞員檢查控制器的工作是否正確 我選擇了第二個選項,並能夠完成這項工作。我對該主題進行了積極評估,因此您可以將此視爲解決方案的一部分。 – Mastrin1994

相關問題