Follow Nirav Raval

Friday 19 July 2013

Spring MVC with Annotation

Hello Friends,

Nice to see you again.
In Previous Sessions  we have seen  Hibernate Basic Demo. And learn how they work.
Today, we will see the example of  Spring MVC with annotation basic tutorial.

This demo I have created using Netbeans IDE 7.1.2.
Netbeans provide Spring Project, So no need to find out every Jars as it provides by Netbeans itself.

Ok Here we will create one Student Register Application demo.
Where first page will have URL redirect for New Student Registration Page.
When Registration done then view registration page will call.

Ok below is the screen dump showing my project structure.

And below is the Libraries added by Netbeans for Spring Project. You choose which you want to use.



Output: 

Run http://localhost:8080/StudentSpringDemo/addStudent


1st Scrren:


2nd Screen:


3rd Screen:

Source Code:
---------------------------------------------------------------------------
(1) Web.xml
---------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>Spring MVC Form Handling With Nirav Raval</display-name>

    <servlet>
        <servlet-name>J2eeDeveloper</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>J2eeDeveloper</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
   
</web-app>

---------------------------------------------------------------------------
(2) J2eeDeveloper-servlet.xml
---------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.j2eedeveloper" />   
       
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

---------------------------------------------------------------------------
(3) StudentController.java
---------------------------------------------------------------------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.j2eedeveloper;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;
/**
 *
 * @author Nirav
 */
@Controller
public class StudentController {
 
 
    @RequestMapping(value="/index", method= RequestMethod.GET)
    public ModelAndView Startup(){
        return new ModelAndView("index");
     
    }
 
    @RequestMapping(value="/student", method= RequestMethod.GET)
    public ModelAndView Student(){
        return new ModelAndView("Student", "command", new Student());
    }
 
    @RequestMapping(value="/addStudent", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model){
        model.addAttribute("id", student.getId());
        model.addAttribute("rollno", student.getRollno());
        model.addAttribute("age", student.getAge());
        model.addAttribute("name", student.getName());
     
        return "result";
    }
}

---------------------------------------------------------------------------
(4) index.jsp
---------------------------------------------------------------------------

<%--
    Document   : index
    Created on : July 19, 2013, 9:38:17 PM
    Author     : Nirav
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring MVC DemoWith Nirav</title>
    </head>
    <body>
        <h1>Spring MVC DemoWith Nirav</h1>
        <a href="/StudentSpringDemo/student/">Add Student</a>
    </body>
</html>


---------------------------------------------------------------------------
(5) Student.jsp
---------------------------------------------------------------------------

<%--
    Document   : Student
    Created on : Jun 10, 2013, 9:49:55 PM
    Author     : Nirav
--%>

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling With Nirav Raval</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/StudentSpringDemo/addStudent">
   <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age" /></td>
    </tr>
    <tr>
        <td><form:label path="rollno">Roll No</form:label></td>
        <td><form:input path="rollno" /></td>
    </tr>
    <tr>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table> 
</form:form>
</body>
</html>



---------------------------------------------------------------------------
(6) result.jsp
---------------------------------------------------------------------------
<%--
    Document   : result
    Created on : Jun 19, 2013, 9:51:52 PM
    Author     : Nirav
--%>

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling With Nirav Raval</title>
</head>
<body>

<h2>Submitted Student Information</h2>
   <table>
    <tr>
        <td>Name</td>
        <td>${name}</td>
    </tr>
    <tr>
        <td>Age</td>
        <td>${age}</td>
    </tr>
    <tr>
        <td>Roll No</td>
        <td>${rollno}</td>
    </tr>
    <tr>
        <td>ID</td>
        <td>${id}</td>
    </tr>
</table> 
</body>
</html>

---------------------------------------------------------------------------
 
Now run URL http://localhost:8080/StudentSpringDemo/addStudent  in your browser.
 
Hope you find this helpful....!

No comments:

Post a Comment