2012-04-02 43 views
1

我想完成的是使用JDBC连接和Spring MVC简单地将员工数据添加到MS访问数据库。我有一个类Employee(基本上是Model),一个输入员工细节的表单,一个我不确定的控制器是否正确,一个web.XML和dispatcher-servlet.XML。我已经阅读文档,但没有准确地知道如何将数据添加到数据库。我还需要更多的课程或方法?编写应用程序上下文是必要的?你可以建议我出一个好的链接或一些代码,我可以通过使用Spring MVC将数据输入到数据库中。到目前为止,我已经写的代码是:关于Spring MVC中数据流的困惑

EmployeeModel.java

package models.app; 

public class EmployeeModel { 

    String emp_id; 
    String fname; 
    String lname; 
    String password; 
    String e_mail; 
    int actinout; 
    String profile; 
    int offshore; 
     public int getOffshore() { 
     return offshore; 
    } 
    public void setOffshore(int offshore) { 
     this.offshore = offshore; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 
    public String getE_mail() { 
     return e_mail; 
    } 
    public void setE_mail(String e_mail) { 
     this.e_mail = e_mail; 
    } 
    public int getActinout() { 
     return actinout; 
    } 
    public void setActinout(int actinout) { 
     this.actinout = actinout; 
    } 
     public String getEmp_id() { 
     return emp_id; 
    } 
    public void setEmp_id(String emp_id) { 
     this.emp_id = emp_id; 
    } 
    public String getFname() { 
     return fname; 
    } 
    public void setFname(String fname) { 
     this.fname = fname; 
    } 
    public String getLname() { 
     return lname; 
    } 
    public void setLname(String lname) { 
     this.lname = lname; 
    } 
    public String getProfile() { 
     return profile; 
    } 
    public void setProfile(String profile) { 
     this.profile = profile; 
    } 

窗体页:

<form:form modelAttribute="EmployeeModel" method="POST" action="/jsp/DojoContentPanes.jsp"> 

<div id= "addemployeedivid"> 
<center><table> 
<tr style="height: 50px"> 
<td style="width: 150px"><b>EMPLOYEE ID : </b></td> 
<td style="width: 600px"><form:input path="pathid" dojoType="dijit.form.ValidationTextBox" id="employeeidid"/> 
<span dojoType="dijit.Tooltip" connectId="employeeidid">Enter Name</span></td> 
<td style="width: 150px" ><b>OFFSHORE : </b></td> 
<td style="width: 600px"><form:input path="pathoffshore" dojoType="dijit.form.ValidationTextBox" id="offshoreid"/> 
<span dojoType="dijit.Tooltip" connectId="offshoreid">Enter Name</span></td></tr> 

<tr style="height: 50px"> 
<td style="width: 150px"><b>E-MAIL ID : </b></td> 
<td style="width: 600px"><form:input path="pathemail" dojoType="dijit.form.ValidationTextBox" id="emailidid"/> 
<span dojoType="dijit.Tooltip" connectId="emailidid">Enter ID</span></td> 
<td style="width: 150px"><b>PROFILE : </b></td> 
<td style="width: 600px"><form:input path="pathprofile" dojoType="dijit.form.ValidationTextBox" id="profileid"/> 
<span dojoType="dijit.Tooltip" connectId="profileid">Enter Name</span></td></tr> 
</table></center><br><br> 
<center><input type="submit" value= "addemployee"></center> 
</div> 
</form:form> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
<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>2</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.jsp</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 


    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"/> 
<bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" 
      p:suffix=".jsp" /> 
</beans> 

ManageEmployeeController.java

package controller.web; 

import models.app.EmployeeModel; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
public class ManageEmployeeController { 

    @RequestMapping(value = "/jsp/DojoContentPanes.jsp", method = RequestMethod.POST) 
    public void addemployee(@ModelAttribute("EmployeeModel") EmployeeModel obj, Model model){ 

System.out.println(obj.getEmp_id()); 

} 


} 

我知道我没有正确写入控制器。我想在这里做的只是印刷我通过形式获得的价值。在提交表单时以及在控制器中接收表单时,是否可以告诉他们如何在form action属性中正确传递URL?由于

回答