【问题标题】:message: HTTP method POST is not supported by this URL消息:此 URL 不支持 HTTP 方法 POST
【发布时间】:2014-11-26 09:50:04
【问题描述】:

在有人因为缺乏研究而试图将其标记为重复或标记之前,我承认这个问题已经存在于堆栈溢出但提供的解决方案不能解决我的问题,所以我想看看人们是否可以解决这个独特的问题我正在经历。

这是我的表格

<form:form method="POST" action="addQuestion" >

   <input type="text" name="questionId" />Enter Id<br>
   <input type="text" name="theQuestion" />Enter Q <br>
   <input type="text" name="category" />Enter Category<br>
   <input type="text" name="correctAnswer" />Enter correct answer<br>
   <input type="submit" value="Next"  >

</form:form>

这出现在我的 web.xml 中

<servlet>
        <servlet-name>addQ</servlet-name>
        <servlet-class>main.WebController</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>addQ</servlet-name>
        <url-pattern>/addQuestion</url-pattern>
    </servlet-mapping>

这是我的网络控制器

@RequestMapping("/addQuestion")
        public String addQuestion(ModelMap model, @RequestParam(value="question", required = true)  String theQuestion , @RequestParam(value="questionId", required = true)  Integer questionId, @RequestParam(value="category", required = true)   String category, @RequestParam(value="correctAnswer", required = true)   String correctAnswer) throws SQLException{
            ViewController viewController = new ViewController();
            viewController.createQuestion(questionId, theQuestion, category, correctAnswer);
            model.addAttribute("message", "Hello hope this flipping works");

        return "addQuestion";
    }

我得到的错误信息是这个 URL 不支持 HTTP 方法 POST

【问题讨论】:

  • 你的 servlet 中有 doPost 方法吗?这是 doPost() 的默认实现,它会抛出错误,指出该方法不受支持。
  • 我的 doPost 方法实际上应该去哪里,它与我的方法有什么不同?
  • 当你想拦截 HTTP GET 请求时,你应该使用 doGet()。当您想要拦截 HTTP POST 请求时,您应该使用 doPost()。请看这个问题; stackoverflow.com/questions/2349633/…
  • @SemihEker 原始 servlet 处理程序方法与这个问题无关,这是一个 Spring MVC 问题。
  • @BlueShark 您似乎在大量混合 Spring MVC 类与原始 Servlet web.xml 配置。由于其他原因,这仍然不起作用,但我怀疑您的控制器是否正在加载。

标签: java spring spring-mvc http-post


【解决方案1】:

这样做:

@RequestMapping(value="/addQuestion", method=RequestMethod.POST)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 2016-07-02
    • 2017-01-28
    • 2018-04-03
    • 2020-09-17
    • 2023-03-29
    相关资源
    最近更新 更多