Spring mvc 目前支持5个tag,分别是 mvc:annotation-driven,mvc:interceptors,mvc:view-controller, mvc:resources和mvc:default-servlet-handler
mvc:annotation-driven 注册 DefaultAnnotationHandlerMapping 和AnnotationMethodHandlerAdapter两个bean,及一系列缺省的messageconverter(需要classpath中有相应的lib包的支持。) 相当于以下配置的效果【mvc:annotation-driven的效果就相当于以下的配置】。
<bean
class&#61;"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name&#61;"order" value&#61;"0" />
bean>
<bean
class&#61;"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name&#61;"webBindingInitializer">
<bean
class&#61;"org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name&#61;"validator" ref&#61;"validator" />
bean>
property>
<property name&#61;"messageConverters">
<list>
<bean
class&#61;"org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean
class&#61;"org.springframework.http.converter.StringHttpMessageConverter" />
<bean class&#61;"org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class&#61;"org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean
class&#61;"org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
list>
property>
bean>
<bean id&#61;"validator"
class&#61;"org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id&#61;"conversion-service"
class&#61;"org.springframework.format.support.FormattingConversionServiceFactoryBean" />
注意在3.0.5版本中不支持在 mvc:annotation-driven中再注册新的messageconveter,在3.1版本中可以支持&#xff0c;写法如下
<mvc:annotation-driven>
<mvc:message-converters>
<bean class&#61;"org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class&#61;"org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean
class&#61;"org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
mvc:message-converters>
mvc:annotation-driven>
mvc:interceptors 注册HandlerInterceptors 或 WebRequestInterceptors 拦截器来拦截request请求。 例如
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path&#61;"/*" />
<bean class&#61;"net.zhepu.web.interceptor.Testinterceptor" />
mvc:interceptor>
mvc:interceptors>
拦截器实现代码如下
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
log.debug("afterCompletion");
}
&#64;Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.debug("postHandle");
}
&#64;Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
log.debug("preHandle");
log.debug(handler.getClass().getName());
return super.preHandle(request, response, handler);
}
mvc:default-servlet-handler 将spring mvc的DispatcherServlet替换当前web server的缺省的Servlet。这样做的好处是定义spring mvc处理请求时&#xff0c;不再需要定义*.do,*.action,/action/*之类的url-pattern&#xff0c;而是可以直接定义为
mvc:view-controller 将指定路径的请求直接转到对应的view上&#xff0c;而不需要特定的controller来处理请求。 例如
<mvc:view-controller path&#61;"/hello/*" view-name&#61;"helloWorld"/>
mvc:resources 将指定URL 的匹配模式来访问静态文件&#xff0c;如
<mvc:resources mapping&#61;"/js/**" location&#61;"/WEB-INF/js/" cache-period&#61;"3600"/>
表示将/WEB-INF/js/目录映射到/js/目录&#xff0c;cache超时时间为3600秒。 则jsp中引用 js/helloWorld.js实际上指向的是 /WEB-INF/js/helloWorld.js.