学习目标:
学习Spring MVC的配置
学习内容:
<?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_3_1.xsd"
version="3.1">
<!-- DispatchServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<!-- DispatcherServlet是Spring MVC最核心的对象
DispatcherServlet用于拦截Http请求,
并根据请求的URL调用与之对应的Controller方法,来完成Http请求的处理-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- applicationContext.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!-- 在Web应用启动时自动创建Spring IOC容器,
并初始化DispatchServlet
-->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- "/"代表拦截所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<?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" xmlns:mv="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">
<!--
context:component-scan 标签作用
在Spring IOC初始化过程中,自动创建并管理com.imooc.springmvc及子包中
拥有以下注解的对象.
@Repository 通常Dao类
@Service 业务逻辑类
@Controller 控制器类
@Component 组件,无法确定的类
-->
<context:component-scan base-package="com.imooc.springmvc"></context:component-scan>
<!-- 启用Spring MVC的注解开发模式-->
<mvc:annotation-driven/>
<!-- 将图片/JS/CSS等静态资源排除在外,可提高执行效率-->
<mvc:default-servlet-handler/>
</beans>
学习时间:
13:00-15:00
学习产出:
初步掌握了SpringMVC的环境配置。
SpringMVC配置
本文介绍了SpringMVC的基本配置过程,包括DispatcherServlet的设置及其映射规则,并通过context:component-scan实现了组件扫描,同时启用了SpringMVC的注解驱动模式。


被折叠的 条评论
为什么被折叠?



