如何将标准方法与doPost方法结合起来?

时间:2014-11-25 13:20:44

标签: java http spring-mvc servlets post

基本上我有这个方法:

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

1 个答案:

答案 0 :(得分:0)

尝试此配置:

@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());
    }