Tuesday, July 06, 2010

Simple Spring MVC Example





Simple Spring MVC Example:
This is a simple Spring MVC Program that display the "Hello World!" string using Spring IOC(Inversion of Control) and DI(Dependency Injection concept).

Step to Create Simple Spring Example

Tool used:

1) STS Spring Source Tool

Steps:

1) Right Click on the Explorer Play ground and create a Web Dynamic Project.
2) Add all necessary jar to lib folder (Coming as Package for Spring Frame Work.)
3) Add a *.jsp in to display the result to the user.
4) Create a dispather-servlet.xml inside SpringWebApp\WebContent\WEB-INF
5)Create Controller Class that contain business logic in it.

Flow:
At the time of deployment of the Spring project, Spring Bean Container (Context Package : BeanFactory Class)read/refer deployment descriptor and load all the necessary class i.e. class "org.springframework.web.servlet.DispatcherServlet"
as load-on-startup parameter is kept as "1" in descriptor.

Whenever the request (url from the user containing *.html) hits the container DispatcherServlet read dispatcher-servlet.xml and perform all the necessary step.

Finally the logic written in Controller class which extends AbstractController is executed and propagated further.


Following are the list of the file :

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" id="WebApp_ID" version="2.5">

<servlet>

<servlet-name>dispatcherservlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

<load-on-startup>1load-on-startup>

servlet>

<servlet-mapping>

<servlet-name>dispatcherservlet-name>

<url-pattern>*.htmurl-pattern>

servlet-mapping>

<welcome-file-list>

<welcome-file>redirect.jspwelcome-file>

welcome-file-list>

web-app>


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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="viewResolver"

class="org.springframework.web.servlet.view.InternalResourceViewResolver" >

<property name="prefix">

<value>/WEB-INF/jsp/value>

property>

<property name="suffix">

<value>.jspvalue>

property>

bean>

<bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >

<property name="message" value="Hello World!" />

bean>

beans>


Welcome.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Welcome Pagetitle>

head>

<body>

${welcomeMessage}

<input type="text">

body>

html>


HelloWorldController:

package com.vaannila;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {

private String message;

@Override

protected ModelAndView handleRequestInternal(HttpServletRequest request,

HttpServletResponse response) throws Exception {

return new ModelAndView("welcomePage","welcomeMessage", message);

}

public void setMessage(String message) {

this.message = message;

}

}



No comments: