1. 环境搭建
导入包:junit, servlet-api, jsp-api, jstl, spring-context(5.3.29)
早期版本回顾 @ https://docs.spring.io/spring-framework/docs/4.3.24.RELEASE/spring-framework-reference/ , 里面介绍了很多基本和基础概念
所有版本下载 @ https://repo.spring.io/release/org/springframework/spring
常见问题:可能遇到访问出现404,排查步骤为:
1. 查看控制台输入,看看是不是缺少什么jar包;
2. 如果jar包存在,显示无法输出,就在IDEA的项目发布中,添加lib依赖;
3. 重启Tomcat即可解决。
2. 配置文件
2.1 Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--注册Servlet,配置DispatchServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--DispatcherServlet 要绑定SpringMVC的配置文件 --> <!--通过初始化参数指定SpringMVC 配置文件的位置,进行关联--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!--启动级别:1,数字越小,启动越早--> <load-on-startup>1</load-on-startup> </servlet> <!--所有请求都会被SpringMVC拦截--> <!-- 在SpringMVC中 / 和 /* 的区别 /: 只匹配所有的请求,不会去匹配jsp页面,应该选择这个 /*: 匹配所有的请求,包括jsp页面,不要选择这个 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2.2 springmvc-servlet.xml
最初的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--处理器适配器--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--视图解析器: 模版引擎 Thymeleaf Freemarker ... --> <!--视图解析器:DispatcherServlet给他的ModelAndView 1. 获取了ModelAndView的数据 2. 解析ModelAndView的视图名字 3. 拼接视图名字,找到对应的视图 /WEB-INF/jsp/hello 4. 将数据渲染到这个视图上 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> <!-- BeanNameUrlHandlerMapping Bean视图解析器--> <bean id="/hello" class="com.crevew.controller.HelloController"/> </beans>
注解方式配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理--> <context:component-scan base-package="com.crevew.controller"/> <!--让Spring MVC不处理静态资源 .css .js .html .mp3 .mp4--> <mvc:default-servlet-handler/> <!--支持mvc注解驱动 在spring中一般采用@RequestMapping注解来完成映射关系 要想使@RequestMapping注解生效 必须向上下文注册DefaultAnnotationHandlerMapping 和一个AnnotationMethodHandlerAdatper实例 这两个实例分别在类级别和方法级别处理。 而annotation-driven配置帮助我们自动完成上述两个实例的注入。 --> <mvc:annotation-driven/> <!--处理器映射器--> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>--> <!--处理器适配器--> <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>--> <!--视图解析器: 模版引擎 Thymeleaf Freemarker ... --> <!--视图解析器:DispatcherServlet给他的ModelAndView 1. 获取了ModelAndView的数据 2. 解析ModelAndView的视图名字 3. 拼接视图名字,找到对应的视图 /WEB-INF/jsp/hello 4. 将数据渲染到这个视图上 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> </beans>
优化后的注解方式配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理--> <!-- <context:component-scan base-package="com.crevew.controller"/>--> <!--让Spring MVC不处理静态资源 .css .js .html .mp3 .mp4--> <!-- <mvc:default-servlet-handler/>--> <!--支持mvc注解驱动 在spring中一般采用@RequestMapping注解来完成映射关系 要想使@RequestMapping注解生效 必须向上下文注册DefaultAnnotationHandlerMapping 和一个AnnotationMethodHandlerAdatper实例 这两个实例分别在类级别和方法级别处理。 而annotation-driven配置帮助我们自动完成上述两个实例的注入。 --> <!-- <mvc:annotation-driven/>--> <!--处理器映射器--> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>--> <!--处理器适配器--> <!-- <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>--> <!--视图解析器: 模版引擎 Thymeleaf Freemarker ... --> <!--视图解析器:DispatcherServlet给他的ModelAndView 1. 获取了ModelAndView的数据 2. 解析ModelAndView的视图名字 3. 拼接视图名字,找到对应的视图 /WEB-INF/jsp/hello 4. 将数据渲染到这个视图上 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <!--前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后缀--> <property name="suffix" value=".jsp"/> </bean> //该行仅为测试,应该删除 <bean name="/t1" class="com.crevew.controller.ControllerTest1"/> </beans>