Saturday, April 18, 2020

Spring MVC Hello World Example with Maven and JSTL

In this Spring MVC tutorial, we are building hello world application using Spring MVC framework. Follow the given instructions step by step and learn the basics. We have used JSTL for writing the view files.

1. Development environment

  1. Maven
  2. Eclipse juno
  3. Tomcat 7

2. Create Spring MVC Application

2.1. Create maven web application

To create a maven web application, open command prompt and make current working directory into eclipse workspace.
Now execute below command.
$ mvn archetype:generate -DgroupId=com.howtodoinjava.mvc 
       -DartifactId=firstSpringApplication
       -DarchetypeartifactId=maven-archetype-webapp 
       -DinteractiveMode=false
Please remove the line break from above command. I added it for making it readable.
Now run below given commands. This will convert the created project into eclipse web project.
$ cd firstSpringApplication

$ mvn eclipse:eclipse -Dwtpversion=2.0

2.2. Update maven dependencies

Update the pom.xml file with these dependencies.
pom.xml
<properties>
    <spring.version>3.0.5.RELEASE</spring.version>
</properties>
 
<!-- Spring 3 dependencies -->
<dependency>
    <groupid>org.springframework</groupid>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
    <scope>runtime</scope>
</dependency>
 
<dependency>
    <groupid>org.springframework</groupid>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
    <scope>runtime</scope>
</dependency>
 
<dependency>
    <groupid>org.springframework</groupid>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
    <scope>runtime</scope>
</dependency>
 
<dependency>
    <groupid>javax.servlet</groupid>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    <scope>runtime</scope>
</dependency>
 
<dependency>
    <groupid>taglibs</groupid>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
    <scope>runtime</scope>
</dependency>

3. Create Dispatcher Servlet

3.1. Update web.xml

Update web.xml for Servlet mapping and spring configuration location.
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 
<web-app>
  <display-name>www.howtodoinjava.com</display-name>
   
  <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
  
    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
    </context-param>
  
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

3.2. Bean configuration file

We will need to write configuration file (spring-mvc-servlet.xml) inside WEB-INF folder. We can name it at our will, but keep remember that it must match the servlet name we declared in web.xml.
spring-mvc-servlet.xml
    xsi:schemaLocation="
 
    <context:component-scan base-package="com.howtodoinjava.web" />
 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
 
</beans>

3. Create Spring MVC Controller

This request handler will receive all requests to a particular URL and return the model data to view handler configured.
DemoController.java
package com.howtodoinjava.web;
 
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("/access")
public class DemoController
{
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model)
    {
        model.addAttribute("message", "Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com");
        return "helloWorld";
    }
}

4. Create Views

Our view is a jsp file, which will be rendered in browser when related view will be returned from server.
helloWorld.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
 
<html>
    <head>
        <title>My first example using Spring 3 MVC</title>
    </head>
    <body>
        <h1>Welcome message : <c:out value="${message}"></c:out></h1>
    </body>
</html>

5. Demo

To run the application, configure tomcat server in eclipse. Now, right click on project and choose run as. On sub menu, choose Run on server.
Now type on browser: http://localhost:8080/firstSpringApplication/access
We should be able to see the message “Spring 3 MVC Hello World !! Thanks to www.howtodoinjava.com” in browser.
If you find any problem in running or deploying given application, drop me a comment.

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...