【问题标题】:how to combine a standard method with a doPost method?如何将标准方法与 doPost 方法结合使用?
【发布时间】:2015-01-23 12:37:42
【问题描述】:

基本上我有这个方法:

  @RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
    public ModelAndView addQuestion(Model 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);

        return new ModelAndView("qFour", "question", new Question());
    }

但目前我收到此错误“此 URL 不支持 HTTP 方法 POST”

所以我需要一种可以使用 doPost() 方法但仍然使用这个旧方法的方法,因为我需要返回并且我相信 doPost() 是无效的。

最终目标是运行 servlet,当用户提交数据时,它会附加到数据库中。

我正在尝试从此表单操作中调用 addQuestion 方法:

<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>

【问题讨论】:

  • 你能分享你调用/请求这个addQuestion的代码吗?
  • 它看起来不错,它应该可以工作.. 更改此@RequestMapping(value = "/addQuestion", method = {RequestMethod.POST,RequestMethod.GET}) 看看你是否遇到了一些意外错误,因为addQuestion 将同时支持获取和发布

标签: java http spring-mvc servlets post


【解决方案1】:

试试这个配置:

@RequestMapping(value = "/addQuestion", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView addQuestion(Model 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);

        return new ModelAndView("qFour", "question", new Question());
    }

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 2021-01-30
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多