By using this tutorial you will easily understand how to develop a Spring 4 MVC example with Maven. I have developed this spring hello world application according to annotation based and step by step process, beginners can easily understand this tutorial. We can also do the same example without maven but you have to download and add required jars manually. Spring is a famous framework using spring we can build complicated applications. Spring mvc is a web part of spring framework build around the principles of spring and it support for Themes, Locales/i18n, Restful services, Annotation based configuration and Seamless integration with other Spring Services/Beans.

Tools used for this application:
  • Spring 4.0.4.RELEASE
  • JDK 1.6
  • Maven 3
  • Jstl
  • Spring Tool Suite (Version: 3.2.0.RELEASE): It is an open source eclipse flavoured IDE like juno, indigo, etc. Spring Tool Suite is also called as STS and it improves your Spring development environment with tips, popup guides, Spring guides and wizards that will make your working experience simpler and quicker.
 Note: Maven will create the necessary directory structure required for your web project. We can also do this example without using maven but in real time projects most of them use maven so i did this example using maven.

Step 1: First you have to Setup Maven Project in Eclipse for that open the eclipse go to File menu and do as shown in below screenshots.




Select the Maven Project and click on the next button then the select project name and location wizard will appears use the default workspace location, click the next and continue as shown in below screenshots.



Enter the below values and click finish.
  • Group Id: com.studywithdemo
  • Artifact Id: Spring4Example
  • Version: 0.0.1-SNAPSHOT
  • Package: com.studywithdemo
Using Maven we need only three dependencies
List of dependencies:
                         Spring-webmvc
                         Servlet-api
                         Jstl

Now double click on the pom.xml file after that select the dependencies and click on the add button and give the values as shown below.


Spring-webmvc:
      Group Id: org.springframework
      Artifact: spring-webmvc
      Version:  4.0.4.RELEASE
Servlet-api:
      Group Id: javax.servlet
      Artifact: jstl
      Version:  1.2
Jstl:
      Group Id: javax.servlet
      Artifact: servlet-api
      Version:  2.5

Now right click on the pom.xml file and select the open with->text editor. It looks like below format.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.studywithdemo</groupId>
  <artifactId>Spring4Example</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring4Example Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.4.RELEASE</version>
    </dependency>
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>provided</scope>
    </dependency>
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>Spring4Example</finalName>
  </build>
</project>

Spring Configuration Parts


Configuring the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
       <servlet>
             <servlet-name>sampleServlet</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/config/servlet-config.xml</param-value>
             </init-param>
       </servlet>
      
       <servlet-mapping>
             <servlet-name>sampleServlet</servlet-name>
             <url-pattern>*.html</url-pattern>
       </servlet-mapping>
       
       <display-name>Archetype Created Web Application</display-name>
</web-app>

Configuring the servlet-config.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
       <mvc:annotation-driven/>
       <context:component-scan base-package="com.studywithdemo.controller"/>
       <!--
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="prefix" value="/WEB-INF/jsp/"/>
             <property name="suffix" value=".jsp"/>
       </bean>
        -->
        
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
       p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

Creating the Controller

package com.studywithdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SampleController {
      
       @RequestMapping(value ="/welcome")
       public String sayHello (Model model) {
            
             model.addAttribute("greeting", "Hello, Welcome to StudywithDemo !");
            
             return "hello";
       }
}

Create JSP for View

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Example</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>

https://www.youtube.com/watch?v=Rs8r01BH2nU

Finally Run the Application

Type the url: http://localhost:8080/Spring4Example/welcome.html

  

0 comments:

Post a Comment

 
Top