Tech I Enjoy Logo
  
When it comes to find a solution for integrating various Frameworks
being used in a medium to large web based enterprise applicaton,
I prefer Spring Frameworks, and all its support classes for
working with Hibernate, JDBC, Transaction, Security, Spring MVC 
and many more.

Let us start with a very simple example demonstrating usage of 
Spring's MVC capability.

This example is for those who want a very simple to start with
code for trying out one of the way to use Spring MVC using
Spring's dependency injection.

We will have a simple URL as 
http://localhost:8080/spring-example/test.do  pasted in the URL
box of a browser, and on submitting this URL, we shall see the
actual example controller getting invoked and then there is a
simple JSP view page showing a single liner on screen.

Various parts/files of this example are as follows:

  • SpringFramework 1.2.8
  • web.xml descriptor file with the Spring context loader listener and Spring's Dispatcher Servlet configuration
  • <web-app>
     <listener>
      <listener-class>
       org.springframework.web.context.ContextLoaderListener
      </listener-class>
     </listener>
     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/applicationContext.xml
      </param-value>
      </context-param>
     <servlet>
      <servlet-name>example</servlet-name>
      <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
     </servlet>
     <servlet-mapping>
      <servlet-name>example</servlet-name>
      <url-pattern>*.do</url-pattern>
     </servlet-mapping>
    </web-app>
    
  • When the Dispatcher Servlet name is defined as example, so we have to define the controller and URL specific mapping in a XML file with a name as example-servlet.xml, in the WEB-INF folder.
  •  <beans>
      <bean id="defaultHandlerMapping" 
     class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="urlMap">
        <map>
         <entry key="test.do" value="example-controller"/>
        </map>
       </property>
      </bean>
    
     <bean name="example-controller" 
           class="example.controller.ExampleController">
      <property name="exampleServiceImpl" 
                ref="exampleService"/>
     </bean>
    </beans>
    
  • applicationContext.xml file has the DAO, Service and DataSource specific configuration for doing dependency injection so as to inject the final loaded service imple to the example controller
  •   <beans>
      <!-- for this example, I have used a simple Drivermanager
           data source, instead a connection pool should be used -->
      <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="username" value="test"/>
        <property name="password" value="test"/>
      </bean>
      <bean id="exampleDAO" class="example.dao.ExampleDAO">
        <property name="dataSource" ref="dataSource"/>
      </bean>
      <bean id="exampleService" class="example.service.ExampleServiceImpl">
        <property name="exampleDao" ref="exampleDAO"/>
      </bean>
    </beans>
    
  • ExampleDAO.java
  • package example.dao;
    import javax.sql.DataSource;
    
    public class ExampleDAO {
      private DataSource dataSource;
      public void setDataSource(DataSource ds) {
    	  dataSource = ds;
      }
    }
    
  • ExampleServiceImpl.java
  • package example.service;
    import example.dao.*;
    
    public class ExampleServiceImpl {
      private ExampleDAO exampleDao;
      public void setExampleDao(ExampleDAO exDao) {
    	  exampleDao = exDao;
      }
    }
    
  • ExampleController.java
  • package example.controller;
    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.servlet.ModelAndView;
    import javax.servlet.http.*;
    import example.service.*;
    
    public class ExampleController implements Controller
    {
    	private ExampleServiceImpl exampleServiceImpl;
    	public void setExampleServiceImpl(ExampleServiceImpl exServ) {
    		exampleServiceImpl = exServ;
    	}
        public ModelAndView handleRequest(HttpServletRequest request,
    		                              HttpServletResponse response) 
    		                              throws Exception {
    		System.out.println("controller called");
    		return new ModelAndView("/test.jsp");
    	}
    }
    
  • test.jsp
  • <%
     out.println("This is a test page");
    %>
    
    This example is working in my local development environment.
    © Copyright 2010, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer