1.Web-INF下的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--serverlet配置 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name> <!-- 指定一个名字 -->
<!--这个很重要 springmvc中所有request都需要经过DispatcherServlet -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始参数 可以从param-value看到 初始加载类资源路径下的springmvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 这个值>=0时 表示启动时的加载这个serverlet的顺序,如果<0时表示访问时才加载-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 注意这列的servlet-name值需要与上面的一致,因为会根据
url =====> 找serverlet =====> 然后找serverlet-class的具体类
这里的url-pattern就是浏览器中输入的url,其中 / 表示http://localhost:8080/+工程名
-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置解决中文乱码的过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 这里表示任何访问该项目下的url都要经过这个过滤器
比如这里是访问 http://localhost:8080/下的jsp等文件都要过滤-->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 这里填写的应该是自己项目中的包-->
<context:component-scan base-package="nju.software"/>
<!-- 视图解析器对象 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--前端控制器,哪些静态资源不拦截-->
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<!--配置文件解析器对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760" />
</bean>
<!-- 开启SpringMVC框架注解的支持 -->
<mvc:annotation-driven />
</beans>
其实<mvc:>还有许多可以用的配置
3.mvc从发送请求到跳转页面的过程
(1)所有request请求都要经过mvc中的前端控制器DispatcherServlet
(2)然后查找Handler,可参考下列源码:
@Nullable
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
if (this.handlerMappings != null) {
Iterator var2 = this.handlerMappings.iterator();
while(var2.hasNext()) {
HandlerMapping hm = (HandlerMapping)var2.next();
if (this.logger.isTraceEnabled()) {
this.logger.trace("Testing handler map [" + hm + "] in DispatcherServlet with name '" + this.getServletName() + "'");
}
HandlerExecutionChain handler = hm.getHandler(request);
if (handler != null) {
return handler;
}
}
}
return null;
}
可以看到返回一串执行链HandlerExecutionChain
(3) 然后要经过适配器HanlderAdapter执行Hanlder,这时候一般要经过Controller,然后返回ModelAndView
(4)返回的ModelAndView依然要经过DisapatcherServlet,然后经过视图解析器ViewResolver
(5)最后经过view渲染,返回到request域中,然后我们就能看到相应的jsp等文件。
这里需要提一下为什么要经过适配器HanlderAdapter,而不具体执行Controller。
具体参考