Default-servlet-handler and Annotation-driven configuration for Spring MVC

Source: Internet
Author: User

Using spring typically configures Url-pattern AS/, instead of a Web server servlet becomes the default servlet, static resources are inaccessible, add <MVC: Default-servlet-handler/> The servlet for the specified Web server is the default servlet, and the static resources are accessible. Access to the @RequestMapping("/model") was not accessible at that time.

The reason is: when < /> , the framework registers annotaitonmethodhandleradapter This bean, can handle ; configure <mvc:default-servlet-handler/> , the framework registered Handleradapter cannot process @RequestMapping , the controller cannot be found. There are several solutions to the solution:

One, add <MVC:ANNOTATION-DRIVEN/> configuration, after you add this configuration, The framework automatically registers requestma Span style= "Color:rgb (57,51,255); Font-family:monaco;" >ppinghandlermapping and (spring3.1 later version), you can process @RequestMapping .

Two, do not use @requestmapping, directly specify the control class, manual <   = "/hello"   = "Com.niuwolfs.controller.HelloController " /> Registration

Public class Hellocontroller implements controller{

@Override

Public Modelandview HandleRequest (httpservletrequest req, HttpServletResponse resp) throws Exception {

Modelandview mv = new modelandview ();

MV . AddObject ("msg", "Hello Spring mvc!" );

mv. Setviewname ("Hello");

return mv;

}

}

Iii. Manual registration of requestmappinghandlermapping and requestmappinghandleradapter

<!--Spring3.1 After using this type of configuration handler Mapping-

< Bean class=" Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping "/>

<!--Spring3.1 After using this type of configuration handler Adapter-

< Bean class=" Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "/>

Four, do not configure the <mvc:default-servlet-handler/>, the spring Url-pattern configured as. Do, will not replace the Web service's default servlet, However, access to the controller must be matched with the configured. Do and so on.



Spring MVC Default-servlet-handler and Annotation-driven 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.