我正在尝试从spring Web应用程序添加到数据库。 Web应用程序有一个简单的jsp页面,用户可以在其中输入详细信息,然后点击提交按钮。单击按钮后,结果应显示在刷新的数据库中。我编写了基本的java类,例如包含插入特定数据库的SQL语句的类。我只是不确定如何将提交按钮链接到添加到数据库的特定java类。
WebController.java
@RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
public String addQuestion(@RequestParam(value="question", required = true) String theQuestion , @RequestParam(value=" questionId", required = true) Integer questionId, @RequestParam(value="category", required = true) String category) throws SQLException{
ViewController viewController = new ViewController();
viewController.createQuestion(questionId, theQuestion, category,
return "qFour";
}
ViewController.java
public Question createQuestion(int questionId, String theQuestion, String category, String correctAnswer) throws SQLException{
Question question = questionController.addQuestion(questionId, theQuestion, category, correctAnswer);
return question;
}
QuestionController.java
public Question addQuestion(int questionId, String theQuestion, String category, String correctAnswer)throws SQLException{
Question question = questionFactory.createQuestion();
question.setQuestionId(questionId);
question.setTheQuestion(theQuestion);
question.setCategory(category);
question.setCorrectAnswer(correctAnswer);
qdao.addQuestion(questionId, theQuestion, category, correctAnswer);
return question;
}
QuestionsDAO.java
public void addQuestion(int questionID, String question, String category, String correctAnswer)throws SQLException{
Connection connection = connFactory.getConnection();
String query = "INSERT INTO es_rm_questions (questionID, question, category, correctAnswer) VALUES (?, ?, ?, ?)";
PreparedStatement prepState = connection.prepareStatement(query);
prepState.setInt(1, questionID);
prepState.setString(2, question);
prepState.setString(3, category);
prepState.setString(4, correctAnswer);
int numberOfRowsUpdated = prepState.executeUpdate();
prepState.close();
connection.close();
}
qOne.jsp
<form:form method="GET" action="addQuestion">
<input type="text" name="questionId" value="">Enter Id<br>
<input type="text" name="theQuestion" value="">Enter Q <br>
<input type="text" name="category" value="">Enter Category<br>
<input type="text" name="correctAnswer" value="">Enter correct answer<br>
<input type="submit" value="Next" >
</form:form>
的web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
答案 0 :(得分:0)
下一行将POST请求绑定到路径YOUAPPCONTEXT / addQuestion
@RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
所以你只需将表单提交到指定的路径, 但你的方法中没有参数。例如,如果您的表单包含名为question,category,questionId的输入字段,则应按如下方式定义方法
@RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
public String addQuestion(@RequestParam(value="question", required = true) String theQuestion , @RequestParam(value=" questionId", required = true) Integer questionId, @RequestParam(value="category", required = true) String category) throws SQLException{
ViewController viewController = new ViewController();
viewController.createQuestion(questionId, theQuestion, category,
return "qFour";
}