我有一个表单,我希望用户填写它,然后点击一个提交按钮,将数据发送到数据库。
这是表格:
<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>
应该在这个webcontroller.java类中调用和addQuestion
@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());
}
然后应该调用viewController连接到DAO以将数据提交到数据库。但是,目前,当点击提交时,它只刷新页面或者只是删除输入到输入框中的所有数据
答案 0 :(得分:0)
在Spring中查找@ModelAttribute
以及Spring Forms
标记和注释。以下内容仅需要一些工作,但它应该指向正确的方向。
<form:form method="POST" modelAttribute="question" action="addQuestion" >
<form:input path="questionId" />Enter Id<br>
<form:input path="theQuestion" />Enter Q <br>
<form:input path="category" />Enter Category<br>
<form:input path="correctAnswer" />Enter correct answer<br>
<form:button type="submit">Next</form:button>
</form:form>
控制器:
@RequestMapping(value="/addQuestion", method=RequestMethod.POST)
public ModelAndView addQuestion(@ModelAttribute Question question) {
@ModelAttribute("question")
public Question createQuestion() {
return new Question();
}