spring web框架
spring web框架
1.spring web mvc简介
2.DispatcherServlet
3.Controllers
4.请求映射处理
5.Views and resolving them
6.Spring's multipart (fileupload) support(文件上传的支持)
7.The Model - ModelMap (ModelAndView)
8.The View - RequestToViewNameTranslator
1.spring web mvc简介
Spring's Web MVC framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for upload files. The default handler is a very simple Controller interface, just offering a ModelAndView handleRequest(request,response) method. This can already be used for application controllers, but you will prefer the included implementation hierarchy, consisting of, for example AbstractController, AbstractCommandController and SimpleFormController. Application controllers will typically be subclasses of those. Note that you can choose an appropriate base class: if you don't have a form, you don't need a form controller. This is a major difference to Struts.spring web的mvc框架是以DispatcherServle为中心的,同时存在对 view resolution, locale and theme,upload files的支持。
2.DispatcherServlet
spinrg mvc框架,同其他的开源的mvc web框架相似,采用是request-driven的方式。spring中提供了相对应的servlet- DispatcherServlet来实现这种request-driven的方式。下面就是spring处理一个request的过程:
The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web.xml of your web application. Requests that you want the DispatcherServlet to handle will have to be mapped using a URL mapping in the same web.xml file. This is standard J2EE servlet configuration; an example of such a DispatcherServlet declaration and mapping can be found below.
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
each DispatcherServlet has its own WebApplicationContext , which inherits all the beans already defined in the rootWebApplicationContext . These inherited beans defined can be overridden in the servlet-specific scope, and new scope-specific beans can be defined local to a given servlet instance。
The framework will, on initialization of a DispatcherServlet , look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).
Consider the following DispatcherServlet servlet configuration (in the 'web.xml' file.)
下面是一个process a request goes through when handled by a DispatcherServlet :
-
The WebApplicationContext is searched for and bound in the request as an attribute in order for the controller and other elements in the process to use. It is bound by default under the key DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE .
-
The locale resolver is bound to the request to let elements in the process resolve the locale to use when processing the request (rendering the view, preparing data, etc.) If you don't use the resolver, it won't affect anything, so if you don't need locale resolving, you don't have to use it.
-
The theme resolver is bound to the request to let elements such as views determine which theme to use. The theme resolver does not affect anything if you don't use it, so if you don't need themes you can just ignore it.
-
If a multipart resolver is specified, the request is inspected for multiparts; if multiparts are found, the request is wrapped in aMultipartHttpServletRequest for further processing by other elements in the process. (See the section entitled Section 13.8.2, “Using the MultipartResolver” for further information about multipart handling).
-
An appropriate handler is searched for. If a handler is found, the execution chain associated with the handler (preprocessors, postprocessors, and controllers) will be executed in order to prepare a model (for rendering).
-
If a model is returned, the view is rendered. If no model is returned (which could be due to a pre- or postprocessor intercepting the request, for example, for security reasons), no view is rendered, since the request could already have been fulfilled.
3.Controllers
Spring's basis for the controller architecture is the org.springframework.web.servlet.mvc.Controller interface, the source code for which is listed below.spring中提供的默认的controller的接口是比较简单的,接口定义如下:
public interface Controller {
/**
* Process the request and return a ModelAndView object which the DispatcherServlet
* will render.
*/
ModelAndView handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
interface is quite abstract, Spring offers a lot of Controller implementations out of the box that already contain a lot of the functionality you might need. The Controller interface just defines the most basic responsibility required of every controller; namely handling a request and returning a model and a view.正如上面的接口所示的那样,十分的简单,spring提供了其他的功能更加强大的controller。
下面几个常见的controller的介绍:
3.1.AbstractController:a class offering caching support and, for example, the setting of the mimetype.
package samples;
public class SampleController extends AbstractController {
public ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "Hello World!");
return mav;
}
}
<bean id="sampleController" class="samples.SampleController">
<property name="cacheSeconds" value="120"/>
</bean>
3.2. ParameterizableViewController: remove the need to hard-code the viewname in the Java class
3.3 UrlFilenameViewController : inspects the URL and retrieves the filename of the file request and uses that as a viewname. For example, the filename of http://www.springframework.org/index.html request is index .
3.4MultiActionController:
Spring offers a multi-action controller with which you aggregate multiple actions into one controller, thus grouping functionality together.spring中提供了一个 multi-action controller。使用这个controller能够使你将功能相似的controller集合在一起。但是这里需要一个确定的函数签名:
// anyMeaningfulName can be replaced by any methodname
public [ModelAndView | Map | void] anyMeaningfulName(HttpServletRequest,
HttpServletResponse [, Exception | AnyObject]);
另外这个来需要一个 MethodNameResolver 来 resolving method names based on the request coming in。
-
ParameterMethodNameResolver - capable of resolving a request parameter and using that as the method name (http://www.sf.net/index.view?testParam=testIt will result in a method testIt(HttpServletRequest, HttpServletResponse) being called). The paramName property specifies the request parameter that is to be inspected).
-
InternalPathMethodNameResolver - retrieves the filename from the request path and uses that as the method name (http://www.sf.net/testing.view will result in a method testing(HttpServletRequest, HttpServletResponse) being called).
-
PropertiesMethodNameResolver - uses a user-defined properties object with request URLs mapped to method names. When the properties contain /index/welcome.html=doIt and a request to /index/welcome.html comes in, the doIt(HttpServletRequest, HttpServletResponse) method is called. This method name resolver works with the PathMatcher , so if the properties contained/**/welcom?.html , it would also have worked!
下面是一个简单的demo:
<bean id="paramResolver" class="org....mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="method"/>
</bean>
<bean id="paramMultiController" class="org....mvc.multiaction.MultiActionController">
<property name="methodNameResolver" ref="paramResolver"/>
</bean>
4.请求映射处理Handler mappings
Using a handler mapping you can map incoming web requests to appropriate handlers.
When a request comes in, theDispatcherServlet
will hand it over to the handler mapping to let it inspect the request and come up with an appropriate HandlerExecutionChain. Then the
DispatcherServlet
will execute the handler and interceptors in the chain (if any).下面是常见的handler mappings
4.1BeanNameUrlHandlerMapping: which maps incoming HTTP requests to names of beans,下面是一个简单的demo: we could map the HTTP request with the URLhttp://samples.com/editaccount.form to the appropriate form Controller as follows:
<beans>
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/editaccount.form" class="org.springframework.web.servlet.mvc.SimpleFormController">
<property name="formView" value="account"/>
<property name="successView" value="account-created"/>
<property name="commandName" value="account"/>
<property name="commandClass" value="samples.Account"/>
</bean>
<beans>
<web-app>
...
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- maps the sample dispatcher to *.form -->
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
...
</web-app>
4.2SimpleUrlHandlerMapping下面是一个简单的demo:
<web-app>
...
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- maps the sample dispatcher to *.form -->
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
<!-- maps the sample dispatcher to *.html -->
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
...
</web-app>
<beans>
<!-- no 'id' required, HandlerMapping beans are automatically detected by the DispatcherServlet -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*/account.form=editAccountFormController
/*/editaccount.form=editAccountFormController
/ex/view*.html=helpController
/**/help.html=helpController
</value>
</property>
</bean>
<bean id="helpController"
class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
<bean id="editAccountFormController"
class="org.springframework.web.servlet.mvc.SimpleFormController">
<property name="formView" value="account"/>
<property name="successView" value="account-created"/>
<property name="commandName" value="Account"/>
<property name="commandClass" value="samples.Account"/>
</bean>
<beans>
This handler mapping routes requests for 'help.html' in any directory to the 'helpController'。
5.Intercepting requests - the HandlerInterceptor interface
Spring's handler mapping mechanism has the notion of handler interceptors, that can be extremely useful when you want to apply specific functionality to certain requests, for example, checking for a principal.spring中存在请求拦截器的接口,能够实现对于特定的reqest的拦截。
作者:许强1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。