Wednesday, February 10, 2010

Simple Annotation Based Spring Controller

The example below shows a simple annotation based spring contoller. Efforts has been made to keep the example as simple as possible.

1) Create a dynamic project in Eclipse ( used here is STS- Spring Source Toolsuite)


2) Jars needed: Place them in the WEB-INF/lib directory.
spring-webmvc
spring.jar
spring-context.jar
commons-logging.jar
jstl.jar
3) Create a controller in the src directory.

package com.yogi.test.Controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
 @RequestMapping("/HelloWorld.htm")
 protected ModelAndView onSubmit(HttpServletRequest request, HttpSession session) throws Exception {
 
  return new ModelAndView("helloworld.jsp");
 }
}

4) Update web.xml


<?xmlversion="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" id="WebApp_ID" version="2.5">
  <display-name>MySpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 
  <servlet>
    <servlet-name>MySpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
 
  <servlet-mapping>
    <servlet-name>MySpringMVC</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>



5) Creat the application context in WEB-INF (in this case MySpringMVC-servlet.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:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-2.0.xsd">
 
   <!--====================================================-->
   <!-- URL Handler Mappings-->
 <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
  <bean id="urlMapping" 
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="alwaysUseFullPath" value="true"/>
    <property name="mappings">
      <props> 
  <prop key="/HelloWorld.htm">helloWorldController</prop>
      </props>
    </property>
  </bean>
 
  <!--====================================================-->
   <!-- View Resolver -->
 <!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
 <bean id="view-Resolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="viewClass">
      <value>org.springframework.web.servlet.view.JstlView</value>
   </property>
</bean>
 
  <bean id="helloWorldController" class="com.yogi.test.Controller.TestController" />
 
</beans>
6) Create JSP in Web-Content



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>Insert title here</title>
</head>
<body>
MY FIRST SPRING MVC APPLICATION.
</body>
</html>

7) Build and Run the application. (Right click and click on Run As --> Run on Server)
8) The package explorer should look something like this: