Spring MVC Hello World

Follow Steps as

  • Go to File
  • New Maven Project
  • Move to Archetype
  • Select webapp
  • Enter group id & artifact id

Now project has appeared in the project explorer.

Open pom.xml by double click go to the last tab of that add following dependencies:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.4.RELEASE</version>
</dependency>

Right click on project go to maven and update project.

Then open web.xml and add following in it

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

   <servlet-mapping>
      <servlet-name>sample</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

As you can see I have added a servlet mapping here which is named as sample which  is dispatcher servlet of spring module

I have done URL mapping to /

Now create a sibling file of web.xml named as sample-servlet.xml (because sample is name of our dispatcher servlet) and add following in it

<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.sam" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/view/" />
      <property name="suffix" value=".jsp" />
   </bean>
 
</beans>

In this config file of spring, we have done following

  • Base package mapping to get component’s of spring like controller, service etc
  • View Resolver which will provide view to our requests.

Now create a controller in com.sam or any sub named as HelloController which will look like

package com.sam.controller;

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

@Controller
public class HelloController {

    @RequestMapping(value="/hello")
    public ModelAndView handle(){
        return new ModelAndView("hello");
    }
}

Here we have done request mapping of URL /hello so if someone is requesting this URL it will come to this functions.

This method is returning a ModelAndView object which is consists of model which is data we want to send as response and View which is file name so that view resolver use that name to get the view and spring will put data in that view and send response.

Now let’s create a folder view under webapp and a file named as hello.jsp which will look like this :

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>

${message}

</body>
</html>

Now add tomcat and run project.

Once your project started properly then hit hello url with project context.

for example:

http://localhost:8080/sample/hello

Advertisements

4 thoughts on “Spring MVC Hello World

Add yours

  1. Well one of the things that I really liked about the Internet is the Freedom of Speech that it can allow you. You can get your point of view out to millions which in the past was solely the domain of MainStream professional Media outlets.Unfortunately just as quikly as we have gained this access we have various Users trying to restrict our Freedom of Speech to meet their own standards and personal agendas…..an unfortunate but mostlikely unavoidable outcome and of course you at YouTube of folded under the pressure…..truly a shame!

    Like

  2. / Quantas incriveis e revolucionárias reliquias arqueológicas não devem estar soterradas nos fundos dos mares e nas areias dos desertos apenas para o homem moderno encontrar… e se surpreender com novas revelações do seu passado!Gostei deste comentário ou não: 0

    Like

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

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: