Shantanu Tomar
Shantanu Tomar

Reputation: 1712

Confused regarding Data flow in Spring MVC

What i want to accomplish is to simply add an employee data to a MS access database using JDBC connectivity and Spring MVC. I have a class Employee (Basically the Model) , A form to input details for an Employee, A controller which i am not sure is correct, a web.XML and dispatcher-servlet.XML. I have read documentations, but not getting exactly how to add the data to database. What more classes or methods i need ? writing an applicationcontext is necessary ? Can u please suggest me out a good link or some code where i can get my data entered in the form simply added to the database using Spring MVC. The code i have written so far is :

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 page :

<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>

dispatcher-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());

}


}

I know i have not written the controller properly. what i am trying to do here is simply printing the value i am getting through form. Can u tell em how to pass the URLs properly in form action attribute when submitting the form and when receiving it in controller ? Thanks

Upvotes: 1

Views: 1662

Answers (1)

Related Questions