spring MVC

Introduction to Spring MVC Web Framework - Web Tier
1) Introduction

The Spring MVC provides rich functionality for building robust Web Applications and it is available as a separate module in the Distribution. As a pre-requisite, readers are advised to go through the introductory article on Spring Framework http://spring.javabeat.net/articles/2007/06/introduction-spring-web-framework. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also Spring can integrate effortlessly with other popular Web Frameworks like Struts, WebWork, Java Server Faces and Tapestry. It means that you can even instruct Spring to use any one of the Web Frameworks. More than that Spring is not tightly coupled with Servlets or Jsp to render the View to the Clients. Integration with other View technologies like Velocity, Freemarker, Excel or Pdf is also possible now. This article provides an introduction over the various components that are available in the Spring MVC for the Web Tier. Specifically the major Core Components like Dispatcher Servlet, Handler Mappings, Controller, Model, View and View Resolver along with the appropriate Api are discussed briefly. Finally the article will conclude by presenting a Sample Application.

* Page 1 - Introduction to Spring MVC Web Framework - Web Tier
* Page 2 - Handler Mappings
* Page 3 - Model,View,Controller,View Resolver
* Page - 4 Sample Application

2) The Spring Workflow

Before taking a look over the various Components that are involved in the Spring MVC Framework, let us have a look on the style of Spring Web Flow.

1. The Client requests for a Resource in the Web Application.
2. The Spring Front Controller, which is implemented as a Servlet, will intercept the Request and then will try to find out the appropriate Handler Mappings.
3. The Handle Mappings is used to map a request from the Client to its Controller object by browsing over the various Controllers defined in the Configuration file.
4. With the help of Handler Adapters, the Dispatcher Servlet will dispatch the Request to the Controller.
5. The Controller processes the Client Request and returns the Model and the View in the form of ModelAndView object back to the Front Controller.
6. The Front Controller then tries to resolve the actual View (which may be Jsp, Velocity or Free marker) by consulting the View Resolver object.
7. Then the selected View is rendered back to the Client.

Let us look into the various Core Components that make up the Spring Web Tier. Following are the components covered in the next subsequent sections.
3) Dispatcher Servlet

The Dispatcher Servlet as represented by org.springframework.web.servlet.DispatcherServlet, follows the Front Controller Design Pattern for handling Client Requests. It means that whatever Url comes from the Client, this Servlet will intercept the Client Request before passing the Request Object to the Controller. The Web Configuration file should be given definition in such a way that this Dispatcher Servlet should be invoked for Client Requests.

Following is the definition given in the web.xml to invoke Spring's Dispatcher Servlet.

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4">

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

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

</web-app>


Look into the definition of servlet-mapping tag. It tells that whatever be the Client Request (represented by *.* meaning any Url with any extension), invoke the Servlet by name 'dispatcher'. In our case, the 'dispatcher' servlet is nothing but an instance of type 'org.springframework.web.servlet.DispatcherServlet'.

Closing associated term with the Dispatcher Servlet is the Application Context. An Application Context usually represents a set of Configuration Files that are used to provide Configuration Information to the Application. The Application Context is a Xml file that contain various Bean Definitions. By default the Dispatcher Servlet will try to look for a file by name <servlet-name>-servlet.xml in the WEB-INF directory. So, in our case the Servlet will look for a file name called dispatcher-servlet.xml file in the WEB-INF directory.

It is wise sometimes to split all the Configuration information across multiple Configuration Files. In such a case we have to depend on a Listener Servlet called Context Loader represented by org.springframework.web.context.ContextLoaderListener.


<web-app>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

</web-app>


By default, this Context Listener will try to look for the Configuration File by name 'applicationContext.xml' in the '/WEB-INF' directory. But with the help of the parameter 'contextConfigLocation' the default location can be overridden. Even multiple Configuration Files each containing separate piece of Information is also possible.

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4">

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/contacts.xml, /WEB-INF/resources.xml</param-value>
</context-param>

</web-app>


The above definition instructs the Framework to look and load for the Configuration Files by name 'contacts.xml' and 'resources.xml' in the WEB-INF directory.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值