My Sites


Tuesday, September 17, 2013

Spring 3 MVC (RESTful) Hello World Example

pom.xml
add these dependencies to your maven project (dynamic web application which converted to a maven project).

<dependencies>

        <!-- Spring MVC depends on spring-core, spring-beans, spring-context, spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>

        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>

        </dependency>

           
    </dependencies>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/appServlet/dashboardservlet-context.xml</param-value>
        </init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>


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


</web-app>

dispatcher-servlet.xml
 <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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <context:component-scan base-package="com.iitWebApp.framework.web.controller" />
        <!-- the mvc resources tag does the magic -->

    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">


    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/pages/" />
    <property name="suffix" value=".jsp" />


    </bean>
</beans>

TestController.java

@Controller
public class TestController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public final ModelAndView TestControllerMethod() {
               
        ModelAndView HomeModelView = new ModelAndView("Welcome");
        HomeModelView.addObject("welcomeMessage", "Welcome");
        return HomeModelView;
}

Welcome.jsp
 <html>
<head></head>
<body>
    <h1>Spring 3 MVC REST web service</h1>
    <h3> ${
welcomeMessage}</h3>  
</body>
</html>



 Cheers!! :) :)

An architectural overview of Spring Web MVC

Model-View-Controller (MVC) pattern


 Spring Web MVC Architecture









image courtesy  : spring in practice (Willie Wheeler)