第一步:创建一个Moudle,添加web支持!建立包结构com.damin.controller
第二步:Maven可能会存在静态资源过滤问题,可以在pom.xml中添加配置
<!-- 在build中配置resources,来防止我们资源导出失败的问题 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
第三步:引入相关依赖
第四步:配置web.xml
- 注册DispatcherServlet
- 关联SpringMVC的配置文件
- 起动级别为1
- 映射路径为 / 【注意不要用 /*】
<?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">
<!-- 1.注册DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动顺序,数字越小,启动越早-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- /: 匹配所有请求,不包括.jsp-->
<!-- /*:匹配所有请求,包括 .jsp -->
<!-- 所有的请求都会被springmvc拦截-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第五步:添加Spring MVC配置文件
- 让IOC注解生效
- 静态资源过滤
- MVC的注解驱动
- 配置视图解析器
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描包,让指定包下的注解生效,有IOC容器统一管理-->
<context:component-scan base-package="com.damin.controller"/>
<!-- 不让SpringMVC处理静态资源 .css .js .html .mp3 .mp4 ...-->
<mvc:default-servlet-handler/>
<!--支持mvc注解驱动
在spring中一般采用@RequstMapper注解来完成映射关系
要想使用@RequestMapper注解生效
必须向上下文中注册DefaultAnnotationHandlerMapper(默认的注释处理程序映射器)
和一个AnnotationMethodHandlerAdapter(处理器适配器)实例
这两个实例分别在类级别和方法级别处理
而annotation-driven配置帮助我们自动完成上述两个实例的注入
-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
在视图解析器中,我们把所有的视图都放在 /WEB-INF/ 目录下,这样可以保证视图安全。因为这个目录下的文件,客户端不能直接访问。
第六步:编写一个Java控制类:com.damin.controller.HelloController
package com.damin.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //让Spring IOC容器初始化时自动扫描到
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
//封装数据
model.addAttribute("msg","SpringMVC annotation");
return "hello";//会被视图层解析处理
}
//重定向 redirect:
@RequestMapping("/test2")
public String test2(Model model){
model.addAttribute("msg","返回值为:");
return "redirect:/index.jsp";
}
}
第七步:创建视图层
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>无敌敏</title>
</head>
<body>
${msg}
</body>
</html>
第八步:配置Tomcat运行
运行成功,Over!
使用SpringMVC必须配置的三大组件:
- 处理器映射器
- 处理器适配器
- 视图解析器
通常,我们只需要手动配置视图解析器,处理器映射器和处理器适配器只需要开启注解驱动即可。