Get started with Spring 4 MVC

After a tiresome weekend, I continued spring 4 dev setup today. After reading this blog post, you will be able to setup a basic webapp with spring 4 MVC.

1. Create a Dynamic web project with your Eclipse IDE.
01-java-dynamic-webproject

2. Create a standard web.xml in WEB-INF folder

3. I have added a Spring dispatcher servlet ‘vriksha’, which is my front controller.

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>CrunchifySpringMVCTutorial</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
    <servlet>
        <servlet-name>vriksha</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>vriksha</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
  
</web-app>

4. Let’s define the vriksha servlet now in vriksha-servlet.xml placed on WEB-INF

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 
    <context:component-scan base-package="org.grassfield.vriksha.controller" />
 
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

All my controller classes on package org.grassfield.vriksha.controller will be scanned.

All my JSPs will be copied inside WEB-INF/jsp

The view name will be sent by the controller. /WEB-INF/<viewname>.jsp will be loaded based on the viewResolver given above.

5. This is a Maven project, add pom.xml with the following entries and resolve it.

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
    </dependencies>

6. Create the Controller now.

package org.grassfield.vriksha.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class VrikshaController {
    @RequestMapping("/welcome")
    public ModelAndView helloWorld(){
        System.out.println("1");
        String message = "<h3>Welcome</h3>";
        return new ModelAndView("welcome", "message", message);
    }
}

The requests to welcome.html will be served by my controller given above. The view name is returned along with the message. Based on the returned view name, WEB-INF/jsp/welcome.jsp should be loaded.

7. Create the index.jsp on the WebContent root folder

<%@ page pageEncoding="UTF-8" %>
<html>
<head>
<title>Spring MVC</title>
</head>
<body>
    <br>
    <div style="text-align:center">
        <h2>
            Swagatham நல்வரவு<br> <br>
        </h2>
        <h3>
            <a href="welcome.html">Call my first servlet</a>@RequestMapping("/welcome")
        </h3>
        <h3>
            <a href="login.html">login </a>(to
            vriksha... @RequestMapping("/vriksha"))
        </h3>
    </div>
</body>
</html>

8. Add the view resolver jsp – welcome.jsp in WEB-INF/jsp

<html>
<head>
<title>Excellent!</title>

</head>
<body>${message}
</body>
</html>

9. After maven downloads all dependencies and project is built successfully, you can load the web pages.

02-index

03-welcome

Advertisements

4 thoughts on “Get started with Spring 4 MVC

  1. Pingback: Spring 4 MVC – 100% annotation based configuration | JavaShine

  2. Pingback: Spring 4 MVC and Spring Security 100% java annotation based configuration | JavaShine

  3. Pingback: Spring 4 MVC and JDBC authenticated Spring Security – 100% java annotation based configuration | JavaShine

  4. Pingback: Spring 4 MVC + Spring 3 Security + Hibernate 4 – integration with java annotations | JavaShine

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s