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包的支持。)
相当于以下配置的效果。
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0" />
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator" ref="validator" />
</bean>
</property>
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean
class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversion-service"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
注意在3.0.5版本中不支持在 mvc:annotation-driven中再注册新的messageconveter,在3.1版本中可以支持,写法如下
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
mvc:interceptors
注册HandlerInterceptors 或 WebRequestInterceptors 拦截器来拦截request请求。
例如
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*" />
<bean class="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");
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.debug("postHandle");
}
@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处理请求时,不再需要定义*.do,*.action,/action/*之类的url-pattern,而是可以直接定义为<url-pattern>/</url-pattern> (注意不是<url-pattern>/*</url-pattern>,/*将匹配所有请求而导致所有jsp和静态内容都无法正常显示,而/只匹配缺省的servlet)。
常见web 容器的default servlet name spring mvc中都能自动识别,但对于不常见的web 容器或default servlet name 被改写过的,则需增加参数 default-servlet-name来指定对应的servlet name.
需与<mvc:annotation-driven/>一起使用
mvc:view-controller
将指定路径的请求直接转到对应的view上,而不需要特定的controller来处理请求。
例如
<mvc:view-controller path="/hello/*" view-name="helloWorld"/>
mvc:resources
将指定URL 的匹配模式来访问静态文件,如
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="3600"/>
表示将/WEB-INF/js/目录映射到/js/目录,cache超时时间为3600秒。
则jsp中引用 js/helloWorld.js实际上指向的是 /WEB-INF/js/helloWorld.js.
网友评论