MVC stands for Model View Controller. The Spring MVC framework is one of the popular frameworks across all the frameworks available in the market. Because the advantages of Spring MVC framework tells the importance of Spring MVC. In this tutorial we are going develop step by step Spring MVC Helloworld Example using Netbeans.

Spring MVC Helloworld Example:

Step 1: Create a new Project in Netbeans by selecting the new project in File menu and select the web application in the choose project pop-up and click next. Spring MVC I Step 2: Give the project name as you want, For now I am giving SampleSpringApplication. Choose the project location where you want to save the project. By default it is in Netbeans projects in your home directory. Spring MVC II If you want to place all dependencies (libraries) in one folder, you can check the Use Dedicated Folder for Sorting Libraries option. Then click next. Step 3 : Select the application server and J2EE version from the drop-down menu. And select next. Spring MVC III Step 4: Select the Spring Web MVC frame work from Frameworks menu. You can also select the version of the selected framework in libraries tab. If you want to include the JSTL in your application, you can select the JSTL check box. And click finish. Spring MVC IV Step 5 : Yes, we are done. By clicking on the finish button, netbeans will provide us the basic spring application architecture like below. Spring MVC V Step 6: Update the diapatcher-servlet.xml like below.

[sourcecode language="xml"]

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="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
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <context:annotation-config />
    <context:component-scan base-package="controllers" />
    <mvc:annotation-driven />

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />
</beans>

[/sourcecode]

Step 7: Web.xml

[sourcecode language="xml"]

<?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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.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>2</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>

[/sourcecode]

Step 8: Create controllers package in Source Package. And Create HelloWorldController.java in controllers package. HelloworldController.java

[sourcecode language="java"]

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.htm")
public class HelloWorldController {

    @RequestMapping(method = RequestMethod.GET)
    public String helloWorld(ModelMap modelMap) {
        System.out.println("on method");
        modelMap.put("printme", "Hello Spring !!");
        return "index";
    }
}

[/sourcecode]

Step 9: Modify index.jsp like below,

[sourcecode language="html"]

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>
    <body>
        <div style="background-color: green;height: 200px;width: 500px">
            <h1 style="color:black;"> ${printme}</h1>
        </div>
    </body>
</html>

[/sourcecode]

Step 10 : Modify the redirect.jsp like below,

[sourcecode language="java"]

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<% response.sendRedirect("helloworld.htm"); %>

[/sourcecode]

Well, it DONE !! To run the application Out put: Spring MVC Helloworld