spring

Spring mvc quickstart

As request from subscriber, today i write a spring mvc quickstart application, that can help every body has a starting point for spring mvc project.

Below list out technologies that you need for this tutorial

  • Eclipse Lunar
  • Spring Framework
  • Tomcat 8 server

1. Create maven web project

In Eclipse IDE choose File->New->Maven Project

springmvc quickstart step 1

Enter details as below and click Finish

spring mvc quickstart step 2

You will see project structure like this

spring mvc quickstart project structure

2. Maven dependencies

We add below content to pom.xml file

<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.5.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies>

3. Web.xml

Out web.xml will have content as below

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

In this application we using servlet 3.1.   Spring MVC using a central servlet is DispatcherServlet when application startup this servlet will lookup default config file {servlet-name}-servlet.xml in WEB-INF folder. You can point out config file explicit  in servlet initial param with name contextConfigLocation when you use difference name.

<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/hello-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

4. Dispatcher configuration

As above mention Spring MVC using dispatcher configuration file to control how it work. Our dispatcher configuration file as below

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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="com.devjav.controller" />
  <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

In this config there are some important points:

  • context:component-scan: this tag tell spring scan on which packages to find out Spring component like @Controller, @RequestMapping etc… We can define many packages seperate by comma
  • viewResolver: this bean tell spring how to find resolve view. In this example we tell spring find in /WEB-INF/jsp/  a file with name as in method on controller return with extension is jsp. To know more about viewresolver you can check other post of mine about integrated apache Tile with Spring here

5. Controller

Our controller in this tutorial is simple

package com.devjav.controller;

import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloworldController {
  @RequestMapping(value = "/hello.htm", method = RequestMethod.GET)
  public String sayHello(Model model) {
    model.addAttribute("date", new Date());
    return "index";
  }
}

In a controller of spring mvc is annotation with @Controller in class level. We can define @RequestMapping in class level or method level, this annotation tell spring how to mapping url and http method with which the method controller. As above code we return “index” so spring will base on viewResolver define to find the file index.jsp in WEB-INF/jsp to show view. We using parameter Model of method to pass attribute between controller and view. In this example we just setting current date time to show to view.

6. View

Out view is simple just to show attribute date that pass from controller

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC quickstart</title>
</head>
<body>
  <div><h4>Hello,<br> Today is ${date}</h4></div>
</body>
</html>

7. Source code

Find out source code on this tutorial on this link

 

Leave a Reply