Tuesday, May 17, 2016

Spring MVC Tutorial Part II - Hello World Example

Spring MVC Hello World

Topics covered
1. Create Maven Project
2. Spring Dependency by Maven pom
3. Web.xml
4. Controller class
5. Displaying Views

Maven Project Creation

Create a new maven project called SpringMVC in eclipse. File --> new --> other --> Maven Project. After creating maven project , add tomcat as your application server in eclipse.

pom.xml

All the dependencies should be added here for Spring MVC configuration.  pom.xml code is below.
 
<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.javahit</groupId>
 <artifactId>SpringMVC</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>SpringMVC Hellow World</name>
 <url>http://maven.apache.org</url>

 <!--Spring version -->
 <properties>
  <spring.version>4.2.1.RELEASE</spring.version>
 </properties>

 <dependencies>

  <!-- Spring dependencies Start-->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <!--Spring dependencies End-->
 </dependencies>
 <build>
  <finalName>SpringMVC</finalName>
 </build>
</project>

web.xml with Dispatcher Servlet

<web-app>
 
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
 </context-param>

 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

 <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>/SpringMvc/*</url-pattern>
 </servlet-mapping>
</web-app>

dispatcher-servlet.xml

<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

 <context:component-scan base-package="com.javaHit.controller"/>

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

Controller Class

        package com.javaHit.controller;

 import org.springframework.stereotype.Controller;

 import org.springframework.ui.ModelMap;

 import org.springframework.web.bind.annotation.RequestMapping;

 import org.springframework.web.bind.annotation.RequestMethod;

  

 @Controller

 @RequestMapping("/helloWorld")

 public class HelloWorldController {

         @RequestMapping(method = RequestMethod.GET)

         public String hello(ModelMap model) {  

             model.addAttribute("name", "Spring Hello World!");

             return "helloWorld";
               }

     }

Displaying Views

helloWorld.jsp

<html>

 <body>

     <h1>Spring MVC Test</h1>
     <h3>Name : ${name}</h3>

 </body>
   </html>

Run the Application

Hit the URL, http://localhost:8080/SpringMvc/helloWorld

localhost:8080   -- Your local host( tomcat)
SpringMvc         -- Configured in web.xml for dispatcher
helloWorld         --  To execute correct Controller class (Request Mapping)

From the controller class method output string "helloworld", it will add both prefix and suffix to execute view page.

No comments:

Post a Comment