Spring MVC Learning Note Spring MVC schema-based Configuration

Source: Internet
Author: User

Spring MVC currently supports 5 tags, mvc:annotation-driven,mvc:interceptors,mvc:view-controller, mvc:resources and MVC, respectively: Default-servlet-handler
mvc:annotation-driveN registers defaultannotationhandlermapping and annotationmethodhandleradapter two beans, and a series of default Messageconverter (requires the support of the corresponding LIB package in the classpath. The effect of Mvc:annotation-driven is equivalent to the following configuration.

<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"/>

Note in the 3.0.5 version, it is not supported to re-register the new Messageconveter in Mvc:annotation-driven, which can be supported in version 3.1, as follows

    <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 Registers the handlerinterceptors or Webrequestinterceptors interceptor to intercept request requests. For example

    <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path= "/*"/>
<bean class= "Net.zhepu.web.interceptor.Testinterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

The interceptor Implementation code is as follows

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 replaces the Dispatcherservlet of spring MVC with the default servlet of the current Web server. The benefit of this is that when you define spring MVC processing requests, you no longer need to define url-pattern such as *.do,*.action,/action/*, but can be defined directly as <url-pattern>/</     Url-pattern> (note that not <url-pattern>/*</url-pattern>,/* will match all requests and cause all JSPs and static content to not display properly, and/only the default servlet). The default servlet for common web containers, name Spring MVC, is automatically recognized, but for uncommon web containers or the default servlet name is rewritten, you need to add parameters Default-servlet-name to specify the corresponding servlet name. Need to be used in conjunction with <mvc:annotation-driven/>

Mvc:view-controller The request for the specified path directly to the corresponding view without requiring a specific controller to process the request. For example

<mvc:view-controller path= "/hello/*" view-name= "HelloWorld"/>

mvc:resources will specify the URL's matching pattern to access the static file, as

<mvc:resources mapping= "/js/**" location= "/web-inf/js/" cache-period= "3600"/>

Indicates that the/web-inf/js/directory is mapped to the/js/directory, and the cache timeout is 3,600 seconds. The reference js/helloworld.js in the JSP actually points to/web-inf/js/helloworld.js.

Original: http://www.cnblogs.com/crazy-fox/archive/2012/02/18/2357669.html

Spring MVC Learning Note Spring MVC schema-based Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.