Advertisement :
   Log In    OR    Register  
  Topics :  
RMI Example

Home >>> Spring Tutorials >>> Spring Web MVC >>> Example code discussed
Struts Tutorials:
Struts2 Tag CheckBoxList
, Checkbox, Iterator, IF
Struts2 Tag Library Example Struts2 Tiles Example Struts2 Tiles I18N Example Struts2 Questions Struts Tiles I18N Example Struts Eclipse MVC Struts2 Tags Struts2 Example and Tutorial Struts MVC Struts2 Validation
Hibernate Tutorials: Hibernate Case Study Class Hierarchy Persist Example Using Hibernate Interceptor Hibernate Questions with Answer Hibernate Many-to-Many Mapping Example Hibernate one-to-many Mapping Example Hibernate and ORM tools Spring Hibernate Example Hibernate SessionFactory Example Hibernate Mapping Class Hierarchy Hibernate Questions Hibernate SessionFactory Questions Spring Hibernate Example: Spring Hibernate Case Study

Written By : Amit
Title :
Interview Question
Description : SpringFramework
More...


Written By : Amit
Title :
Spring Remoting
Description : Java RMI Example
More...


Written By : ISHTEK
Title :
Spring Reflection
Description : POC Idea example code
More...


Written By : ISHTEK
Title :
Spring singleton
Description : Spring Container Example
More...


Written By : ISHTEK
Title :
Spring Features Updates News
Description : Bringing To Page
More...

Tags/Keywords : Spring web, Spring mvc, Spring example, Example, Code, Tutorial, Article
Author : Amit
Date (Year/Month/Date): 2010-06-24 Working on some example using Spring WEB module

Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways  for doing some sort of example
and may not be fit for any other purpose.

In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.


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.
    
    
    	

    Commented By ->
    Amit
    Using Spring's predefined controller such as 
       ParameterizableViewController
    
    I have tried to use this controller from Spring's api, and defined 
    the bean name as "/sample.go", so by using sample.go as part of the
    target path in URL, I should be able to hit the view, that is 
    defined as the view name as property with a value pointing to the
    JSP file.
    
    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 name="/sample.go" 
      class="org.springframework.web.servlet.mvc.ParameterizableViewController">
         <property name="viewName" value="/test.jsp"/>
      </bean>
    </beans>
    
    As mentioned already, following URL that is using the host name
    as localost, is showing the final output from the test.jsp file.
    
    http://localhost:8080/example-spring-mvc/sample.go
    
    
    Please write your Comment on this Matter
    (This will be visible if found suitable):
    Name: *
    Email (will not be displayed): *
    Matter: *
    33,37
    Enter bigger number from above :*
    Home >>> Spring Tutorials >>> Spring Web MVC >>> Example code discussed
    Visitor/User referred related external URL:
    (Visible upon review and approved by this site Administrator)
    
    Referred By Name *:
    Resource URL *: (e.g, URL should be starting with http://www.-----.---)
     
    Resource Short Description *:
    15,6
    Enter bigger number from above : *

    Please log in to add or reply to any matter<- requires login
    Log in or Register
    Copyright © 2008-2010, Interview-Questions-Tips-Forum, All Rights Reserved.
    CONTACT    PRIVACY POLICY    DISCLAIMER
     
    
    Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    Other names may be trademarks of their respective owners
    
    This web site's Terms of Use and Disclaimer :
    
    This web site provides some of the information about various technologies, example code, tips, tutorials etc. Like any printed materials,
    content of these pages may become out of date over a period of time. Therefore all visitor/users of this web site are requested/advised
    to refer to the originating parties/sources for the latest changes and happenings for detailed information. This information is not
    intended to be a substitute for the original reference provided by the originating parties/sources. These examples with source code or
    without source code, have not been thoroughly tested under all conditions. Interview-questions-tips-forum.net therefore, cannot guarantee
    or imply reliability of these example source code or programs.
    
    By accessing and using this website in any ways, including, without limitation, browsing the website pages, using any information, using
    any content and/or downloading any materials, you agree to and are bound by the terms of use described in this page and Usage Terms and Conditions.
    If you do not agree to all of the terms and conditions contained in the terms of use described in this page and
    Usage Terms and Conditions, do not use this website in any manner. If you are using the website on behalf of your
    employer, you represent that you are authorized to accept these Terms of Use on your employer's behalf.
    
    All Trademarks are property of their respective owner. Appropriate measure is being taken for providing accurate and up-to-date
    information but like any printed materials, these blog(s)/contents may eventually be outdated one day, so if you are using any of these
    information, please refer original content/documentation from respective sources. And under no circumstances shall the Author of these
    contents and/or this web site be liable for any loss, damage, expense incurred or suffered which is claimed to have occurred because of
    usage of the contents of this web site. If you have any questions/queries/feedback/suggestions then please write to this web site owner
    at contact.