Java 如何将标准方法与doPost方法相结合?
基本上我有这个方法:Java 如何将标准方法与doPost方法相结合?,java,http,spring-mvc,servlets,post,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 = tru
@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>
输入Id
输入Q
输入类别
输入正确答案
尝试以下配置:
@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());
}
您能在调用/请求此
addQuestion
的地方共享您的代码吗?看起来不错,应该可以。。更改此@RequestMapping(value=“/addQuestion”,method={RequestMethod.POST,RequestMethod.GET})
并查看您是否收到一些意外错误,因为addQuestion
将同时支持GET和POST