对Spring MVC中的数据流感到困惑

时间:2012-04-02 14:14:26

标签: spring spring-mvc

我想要实现的是使用JDBC连接和Spring MVC简单地将员工数据添加到MS访问数据库。我有一个类Employee(基本上是模型),一个输入Employee详细信息的表单,一个我不确定的控制器,一个web.XML和dispatcher-servlet.XML。我已经阅读了文档,但没有准确地知道如何将数据添加到数据库。我需要更多的课程或方法?编写applicationcontext是必要的吗?你可以建议我一个好的链接或一些代码,我可以在表单中输入我的数据,只需使用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());

}


}

我知道我没有正确地编写控制器。我想在这里做的只是打印我通过表格获得的价值。在提交表单和在控制器中接收表单时,您能告诉em如何在表单操作属性中正确传递URL吗?谢谢