Total Visitors

Sunday, March 16, 2014

Spring 4.0 MVC Example

Here I will show you how to create a simple example by using Spring MVC.
I will be creating this example without the help of xml, i.e whole configuration is done using pure Java.
Just copy paste the content and you yourself google it out to understand the code.
Note* this application is working fine with GlassFish applicaiton server.

1) Instead of web.xml write the class Initializer.java 

package com.spring.init;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class Initializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);

ctx.setServletContext(servletContext);

Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}

2) Instead of servlet-context.xml

package com.spring.init;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration //Specifies the class as configuration
@ComponentScan("com.spring") //Specifies which package to scan
@EnableWebMvc //Enables to use Spring's annotations in the code
public class WebAppConfig {
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}

3) Define your controller class.

package com.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SpringController {
@RequestMapping(value="/hello-page")
public ModelAndView goToHelloPage() {
ModelAndView view = new ModelAndView();
view.setViewName("hello"); //name of the jsp-file in the 'page' folder

String str = "Finally Spring 4.0 Working !";
view.addObject("message", str); //adding of str object as 'message' parameter

return view;
    }
}

4) hello.jsp 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

    <p>Hello world: ${message}</p>
    <p>Nice!</p>

5) index.jsp (Run this and Enjoy!)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<h1>Home page</h1>
<p>This is a Home Page.</p>
<p><a href="hello-page.htm">Hello world link</a></p>