SpringMVC组件配置

web.xml 、 springmvc-servlet.xml 配置SpringMVC四大组件。

web.xml 配置前端控制器:前端控制器就是个servlet

 1 <!-- 配置前端控制器 -->
 2   <servlet>
 3       <servlet-name>springmvc</servlet-name>
 4       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 5       <init-param>
 6           <param-name>contextConfigLocation</param-name>
 7           <param-value>classpath:/springmvc-servlet.xml</param-value>
 8       </init-param>
 9   </servlet>
10   <servlet-mapping>
11       <servlet-name>springmvc</servlet-name>
12       <url-pattern>*.action</url-pattern>
13   </servlet-mapping>

<init-param>初始化参数适配SpringMVC核心配置文件的location。一般将该文件放置src下。

核心配置文件有个默认名称命名规则:根据servlet-name的值添加一个后缀“-servlet”---------- springmvc-servlet.xml(配置了初始化参数后可以自定义命名)

所有.action的请求都将会被控制器拦截并进入处理链进行处理;(处理链:HandlerMapper--->HandlerAdapter--->Handler<即Controller>

Springmvc-servlet配置:

  ---配置映射关系(用注解的方式更方便灵活)、视图解析器(拼接一个页面的真实路径)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" 
 3         xmlns:context="http://www.springframework.org/schema/context" 
 4         xmlns:mvc="http://www.springframework.org/schema/mvc" 
 5         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 6         xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
 8                             http://www.springframework.org/schema/context
 9                             http://www.springframework.org/schema/context/spring-context-3.2.xsd
10                             http://www.springframework.org/schema/mvc
11                             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
12     <!-- 配置映射关系 -->                        
13     <!-- <bean name="/addUser.action" class="Controller.controller"></bean> -->
14     
15     <context:component-scan base-package="Controller"/>
16     <!-- 开启mvc注解 -->
17     <mvc:annotation-driven/>
18     
19     
20     <!-- 视图解析器  -->                
21     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22         <!-- 前缀 -->
23         <property name="prefix" value="/WEB-INF/"/>
24         <!-- 后缀 -->
25         <property name="suffix" value=".jsp"/>
26     </bean>    
27                     
28                 
29 </beans>

mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter  实际开发时使用mvc:annotation-driven

原文地址:https://www.cnblogs.com/tongxuping/p/7088183.html