2014-11-25 65 views
-1

基本上我有這樣的方法:如何將標準方法與doPost方法相結合?

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

,但目前我收到此錯誤

所以我需要一種方法,我可以使用的doPost「HTTP POST方法不受此URL支持」()方法,但仍然使用這種老方法,因爲我需要返回,我相信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> 
+0

你能分享從那裏你調用/請求這個'addQuestion'你的代碼? – Pravin 2014-11-25 15:19:51

+0

添加到問題 – BlueShark 2014-11-25 15:33:46

+0

它似乎不錯,它應該工作..更改此@RequestMapping(值=「/ addQuestion」,方法= {RequestMethod.POST,RequestMethod.GET})' 並看到你收到一些意外的錯誤因爲'addQuestion'將支持get和post – Pravin 2014-11-25 16:13:44

回答

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