Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Wiring MongoDB Repository class with Springs controller

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 351
    Comment on it

    Wiring MongoDB Repository Class with SpringMVC:- Spring Data point out the specialties of repository support for MongoDB. To use the repository concept you have to define your repository and attach the required XML element inside Spring Context XML file. Mongo namespace provides you an XML element for defining the proper repository for being used later on.

    StudentDTO.java

    package com.evon.common;
    
    public class StudentDTO {
    
        private String rollno;
        private String name;
        private int age;
    
        public String getRollno() {
            return rollno;
        }
    
        public void setRollno(String rollno) {
            this.rollno = rollno;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    
    
    
    }
    

    EvonController.java

    package com.evon.controller;
    
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.ModelAttribute;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.servlet.ModelAndView;
    
        import com.evon.common.StudentDTO;
        import com.evon.dao.domain.Student;
        import com.evon.mongo.repository.UserRepository;
    
        @Controller
        public class EvonController {
    
            @Autowired
            UserRepository repository;
    
            @RequestMapping(value="/login",method=RequestMethod.GET)
            public ModelAndView login()
            {
                ModelAndView mnv=new ModelAndView("login");
    
                return mnv;
            }
    
            @RequestMapping(value="/insertRec",method=RequestMethod.POST)
            public ModelAndView insertRec(@ModelAttribute("studentDTO") StudentDTO studentModel)
            {
                System.out.println("Inside Controller****************");
                Student student=new Student();
                student.setName(studentModel.getName());
                student.setRollno(studentModel.getRollno());
                student.setAge(studentModel.getAge());
                repository.save(student);
                return new ModelAndView("redirect:success");
            }
    
            @RequestMapping(value="/success",method=RequestMethod.GET)
            public ModelAndView success()
            {
                ModelAndView mnv=new ModelAndView("success");
                return mnv;
            }
    
        }
    

    Student.java

    package com.evon.dao.domain;
    
    import java.io.Serializable;
    
    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;
    
    @Document(collection="student")
    public class Student implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        @Id
        private String rollno;
    
        private String name;
        private int age;
    
        public String getRollno() {
            return rollno;
        }
    
        public void setRollno(String rollno) {
            this.rollno = rollno;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    UserRepository.java

    package com.evon.mongo.repository;
    
    import org.springframework.data.mongodb.repository.MongoRepository;
    import org.springframework.stereotype.Repository;
    
    import com.evon.dao.domain.Student;
    
    @Repository
    public interface UserRepository extends MongoRepository<Student, String> {
    
    }
    

    mvc-dispatcher.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mongo="http://www.springframework.org/schema/data/mongo"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/data/mongo
             http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
    
        <context:component-scan base-package="com.evon.controller" />
        <context:component-scan base-package="com.evon.mongo.repository" />
    
        <mvc:view-controller path="/login" view-name="login"/>
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/jsp/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
    
        <!-- Factory bean that creates the Mongo instance -->
        <bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
            <property name="host" value="192.168.1.219" />
            <property name="port" value="27017"></property>
        </bean>
    
        <!-- MongoTemplate for connecting and quering the documents in the database -->
        <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg name="mongo" ref="mongo" />
            <constructor-arg name="databaseName" value="manish" />
        </bean> 
    
        <mongo:repositories base-package="com.evon.mongo.repository" />
    
    
        <mvc:annotation-driven />
    </beans>
    

    login.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="insertRec" method="post" commandName="studentDTO">
            RollNo:-<input type="text" name="rollno" /><br>
            Name:- <input type="text" name="name" /><br>
            Age:- <input type="text" name="age" /><br><br>
            <input type="submit" value="Submit" />
        </form>
    </body>
    </html>
    

    success.jsp

    <html>
    <body>
        <h1>Record Successfully Inserted</h1>   
    </body>
    </html>
    

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.evon</groupId>
      <artifactId>SpringMVC</artifactId>
      <version>1.0</version>
      <packaging>war</packaging>
    
         <dependencies>
    
        <!-- mongodb java driver -->
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
                <version>2.11.0</version>
            </dependency>
    
            <!-- Spring data mongodb -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-mongodb</artifactId>
                <version>1.2.0.RELEASE</version>
            </dependency>
    
            <!-- need this for @Configuration -->
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency>
    
          <!-- Spring 3 dependencies -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>3.2.2.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>3.2.2.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>3.2.0.RELEASE</version>
            </dependency>
    
            <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-webmvc</artifactId>
                 <version>3.2.0.RELEASE</version>
             </dependency>
    
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
               <groupId>javax.servlet</groupId>
               <artifactId>servlet-api</artifactId>
               <version>2.5</version>
            </dependency>
          </dependencies>
    
    
    </project>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: