Spring mvc 目前支持5個tag,分別是web
mvc:annotation-driven,mvc:interceptors,mvc:view-controller, mvc:resources和mvc:default-servlet-handlerspring
mvc:annotation-drivenjson
註冊 DefaultAnnotationHandlerMapping 和AnnotationMethodHandlerAdapter兩個bean,及一系列缺省的messageconverter(須要classpath中有相應的lib包的支持。)mvc
至關於如下配置的效果。app
<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版本中能夠支持,寫法以下jsp
<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:interceptorside
註冊HandlerInterceptors 或 WebRequestInterceptors 攔截器來攔截request請求。svg
例如post
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/*" /> <bean class="net.zhepu.web.interceptor.Testinterceptor" /> </mvc:interceptor> </mvc:interceptors>
攔截器實現代碼以下url
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.