spring-mvc.xml配置
1.spring-mvc表头
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
</beans:beans>
2.<annotation-driven>会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能。
<annotation-driven />
3.扫描controller,同时controller类文件代码处应添加注解
<context:component-scan base-package="com.bookstore.controller"/>
4.配置静态资源。此处解释可以见web.xml配置篇
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/css/**" location="/css/" />
<resources mapping="/images/**" location="/images/" />
<resources mapping="/js/**" location="/js/" />
<resources mapping="/bootstrap/**" location="/bootstrap/" />
5.配置ViewResolver,我们可以看到下面两个value的值。当controller返回一个地址,例如index,ViewResolver会随之匹配,最终我们访问的实际url是/WEB-INF/views/index.jsp,因此你可以根据你存放的jsp目录自由修改。
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
让我们看下图springmvc的操作流程,我们已经明白我们完成了在2,3步完成了下图的处理器映射器和处理器适配器,第4步我们完成了视图解析器的过程。至于剩下的,前端控制器将在我们的web.xml完成,也就是三个配置文件的汇总里完成。处理器(Handler)我们将在controller类中完成,至于view则是前端的事情,我们后面再说。