Spring请求方法' POST'不支持

时间:2018-03-19 16:22:23

标签: spring post

我目前正在研究春天,我正在制作一个非常基本的应用程序。我只是想创建一个发布帖子并将我重定向到另一个页面的按钮。如果我返回一个简单的字符串,该应用程序工作正常,但如果我尝试返回一个HTML页面,该应用程序不再工作...我正在运行一些测试,他使用GET方法,但为什么他不能使用POST? 谢谢,对不起英语。

这是我的代码:

@Controller
public class CompraMaça {

@RequestMapping(value = "/maca", method=RequestMethod.POST)
public String maca() {

    return "maca.html";
  }
}

button.html:

 <tr>
    <td>
        <form action="/maca" method="POST">
            <input type="submit" value="Compre uma maçã" style="display:block">
        </form>
    </td>
</tr>

和我想要返回的页面: maca.html

 <html>
    <head>
        <title>Parabéns !</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>Você comprou uma maçãzona !</div>
    </body>
</html>

编辑:在Chrome网络标签中我得到了这个:

General:
Request URL:http://localhost:8080/maca
Request Method:POST
Status Code:405 
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade 



 Response Headers:

Allow:GET, HEAD
Content-Language:en-US
Content-Length:338
Content-Type:text/html;charset=UTF-8
Date:Mon, 19 Mar 2018 17:23:39 GMT


Request Headers

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:0
Content-Type:application/x-www-form-urlencoded
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36

1 个答案:

答案 0 :(得分:0)

代码使用:

return "redirect:/maca.html";

有人知道为什么吗? THX