Spring MVC framework[1] Configuration

来源:互联网 发布:java 空格 显示问号 编辑:程序博客网 时间:2024/06/05 16:53

All contents are referenced from Spring In Action

servlet-context.xml specified in web.xml is used by DispatcherServlet to defines its request-processing context.

web.xml:

<servlet><servlet-name>appServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring/appServlet/<strong><span style="color:#ff0000;">servlet-context.xml</span></strong></param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>appServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>

in servlet-context.xml , Spring’s mvc namespace includes a new <mvc:resources> element that handles requests for static content.

servlet-context.xml:

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory--> <resources mapping="/resources/**" location="/resources/" /> <!-- ** is Ant-style wildcard -->
As configured here, any requests whose paths begin with /resources will be automatically served from the /resources folder at the root of the application

DispatcherServlet consults one or more handler mappings in order to know which controller to dispatch a request to.

 one of the mappings, DefaultAnnotationHandlerMapping maps requests to controller methods that are annotated with @RequestMapping.

we’ll also use annotations to bind request parameters to handler method parameters, perform validation, and perform message conversion. 

To turn on annotation-driven features of Spring MVC, put <mvc:annotation-driven> in servlet-context.xml.

servlet-context.xml:

<!-- Enables the Spring MVC @Controller programming model --><annotation-driven />

<mvc:annotation-driven> registers several features, including JSR-303 validation support, message conversion, and support for field formatting.

0 0
原创粉丝点击