Tech I Enjoy Logo

Custom Search




  Home >> Spring Framework
In this Page I am going to show and describe an example
using SpringFramework's Web and MVC related functionality.

To start with I am not going to write any custom class or POJOs
and not use any annotations, but I am going to use Spring's
own implementations for URL handling/mapping and Controller.

I assume that you as a reader have already know besics about
Spring core, web modules and know how to use these.

In this example/page I am not going to explain / provide 
definitions for all these components/modules.

Here I am going to use some of the modules/JARs from SpringFramework as follows:
  • org.springframework.aop-3.0.0.RELEASE.jar
  • org.springframework.asm-3.0.0.RELEASE.jar
  • org.springframework.aspects-3.0.0.RELEASE.jar
  • org.springframework.beans-3.0.0.RELEASE.jar
  • org.springframework.context-3.0.0.RELEASE.jar
  • org.springframework.context.support-3.0.0.RELEASE.jar
  • org.springframework.core-3.0.0.RELEASE.jar
  • org.springframework.expression-3.0.0.RELEASE.jar
  • org.springframework.web-3.0.0.RELEASE.jar
  • org.springframework.web.servlet-3.0.0.RELEASE.jar
  • These JAR files along with some other files placed in the WEB-INF/lib folder, as follows:
  • commons-logging-1.1.1.jar
  • log4j-1.2.15.jar
  • There are two main configuration/descriptor files such as "web.xml" and "example-web-controller-servlet.xml" The section in bold red in the file name is same as that of the entry in web.xml file as follows: /WEB-INF/web.xml
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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>example-web-controller</servlet-name>
    	<servlet-class>
    	  org.springframework.web.servlet.DispatcherServlet
    	</servlet-class>
    	<load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>example-web-controller</servlet-name>
    	<url-pattern>*.go</url-pattern>
      </servlet-mapping>
    </web-app>
    
    As you might have noticed that the web.xml file is having declaration for the web app as web-app_2_5.xsd, so I have tested this example on Tomcat web server version 6.0.18. So the web.xml descriptor file is having a Spring's DispatcherServlet configured to be load on web server startup. Spring Framework requires a *-servlet.xml file for automatically using this file for context related operations. So this file example-web-controller-servlet.xml is having some tags used such as follows:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"    
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
       <bean id="exampleController" 
        class="org.springframework.web.servlet.mvc.UrlFilenameViewController">  
         <property name="prefix" value="/"/>
         <property name="suffix" value=".jsp"/>
       </bean>
      <bean id="exampleHandlerMapping" 
       class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
          <map>
            <entry key="test.go" value="exampleController"/>
          </map>
        </property>
      </bean>
    </beans>
    
    As mentioned above, I am going to use org.springframework.web.servlet.handler.SimpleUrlHandlerMapping as handler mapping implementation and org.springframework.web.servlet.mvc.UrlFilenameViewController as controller. The URL that I used to test this application is : http://localhost:8080/example-spring-web/test.go After completing the above example, I moved on to a slightly different approach, whereby I created a custom/user defined controller by implementing Spring's Controller interface and providing implementation for the abstract handleRequest method, as shown below: ExampleController.java
    package example.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    public class ExampleController implements Controller{
    	public ModelAndView handleRequest(HttpServletRequest request,
    			                          HttpServletResponse response) 
    	                                  throws Exception {
         return new ModelAndView("/test.jsp");
    	}
    }
    
    It does nothing apart from just returning the ModelAndView instance along with the destination JSP file. Now there is a little bit of change in the *-servlet.xml file as well: example-web-controller-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
       <bean id="exampleController" 
        class="example.controller.ExampleController">  
       </bean>
      <bean id="exampleHandlerMapping" 
       class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
          <map>
            <entry key="test.go" value="exampleController"/>
          </map>
        </property>
      </bean>
    </beans>
    
    These changes works pretty the same way it is expected to. On exploring the following URL, it is observed to show the output from the test.jsp file as expected. http://localhost:8080/example-spring-web/test.go Then I moved onto another phase of this example by exploring some other ways of making this example work. I managed to do this by changing/using some other type of handler mapping from SpringFramework WEB module. I used org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping As per the official documentation from Spring, this handler mapping automatically picks up bean that is defined with a name matching with that of the URI, for this example, the bean that is required to be defined with a name as "/test.go" and the class I used as that of the ExampleController, as shown in the following section: example-web-controller-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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
      <bean id="exampleHandlerMapping" 
       class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
      </bean>
      <bean name="/test.go" class="example.controller.ExampleController"/>
    </beans>
    
    http://localhost:8080/example-spring-mvc/test.go So the URL has the URI as /test.go, and it is mapped to the ExampleController. Thus ExampleController returns the view as "/test.jsp". Hope this helps. If anything missed out , please let me know at techienjoy at yahoo . com
    Example using Spring DAO with Hibernate :
    Example using Spring DAO with Hibernate Framework Part 2.
    Example using Spring DAO with Hibernate :
    Example using Spring DAO with Hibernate Framework.
    Spring Batch Framework Example :
    Spring Batch Framework Example source code
     and Step by step code walkthrough
    Spring Framework Remote Session Bean :
    Spring Framework Example source code of
    Remote Session Bean.
    Spring Framework Singleton Example :
    Spring Framework Singleton Example source code 
    compared with GOF Singleton.
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework Part 1.
    Spring Framework Web Example :
    Spring Framework Web Example source code Explained.
    Spring Framework MVC and Tiles :
    Spring Framework MVC and Tiles with Example 
    source code explained.
    Spring Framework Web Example and WSDL :
    Spring Framework Web Example source code
    with WSDL.Step by step code walkthrough
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework Part 2.
    Spring Framework Example :
    Spring Framework Example source code Explained.
    Step by step code walkthrough
    Spring Framework Web Example :
    Spring Framework Web Example source code Explained.
    Spring Framework Local Stateless Session bean :
    Spring Framework Local Stateless Session bean with
    example source code explained.
    Interview Questions with answer on Spring Framework :
    Interview Questions with answer on Spring Framework.
    Example using Spring with Hibernate :
    Example using Spring with Hibernate Framework and use
    Current Session Context.


    References :
    Tags: spring dao hibernate example
    Tags: spring hibernate dao example
    Tags: spring hibernate example 2
    Tags: spring hibernate example current session context
    Tags: spring hibernate example
    Tags: Spring Interview Questions Answer
    Tags: Spring Local SLBean
    Tags: Spring MVC Tiles2
    Tags: Spring Remote SLBean
    Tags: Spring Singleton GOF Singleton Difference
    Tags: spring web example
    Tags: spring ws example
    Tags: Spring
    Tags: springws example wsdl
    

    
    
    DISCLAIMER :
    The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
    techienjoy.com is not liable for any negative consequences that may result/arise from 
    implementing directly/indirectly any information covered in these pages/articles/tutorials.
    
    All contents of this site is/are written and provided on an "AS IS" basis,
    without WARRANTIES or conditions of any kind, either express or implied, including, without
    limitation, merchantability, or fitness for a particular purpose. You are solely responsible
    for determining the appropriateness of using or refering this and assume any risks associated
    with this.
    
    In spite of all precautions taken to avoid any typo in these pages, there might be some 
    issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
    extends sincerest apologies to all our visitors for the same.
    
    
    

    Android Examples || Android Examples

    © Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions