2014-11-24 63 views
0

我想从春天的web应用程序添加到数据库。该Web应用程序有一个简单的jsp页面,用户可以输入详细信息,然后点击提交按钮。一旦按钮被点击,结果应该出现在刷新的数据库中。我编写了基本的Java类,例如包含插入特定数据库的SQL语句的类。我只是不确定如何将提交按钮链接到添加到数据库的特定java类。如何从Web应用程序添加到数据库?

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

向我们展示您尝试过的代码。 – 2014-11-24 13:28:20

+0

@TomJonckheere添加到问题 – BlueShark 2014-11-24 13:33:20

回答

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"; 
     } 
+0

已编辑我的代码在评论中匹配此,运行它,但获得404.任何想法?或需要查看更多的代码? – BlueShark 2014-11-24 14:06:04

+0

你有形式:form method =“GET”,但你应该有形式:form method =“POST” – 2014-11-24 14:23:57

+0

我已经改变了,但我仍然得到404在浏览器 – BlueShark 2014-11-24 15:12:05

相关问题