- [纯SpringMVC] - 快速搭建视频:https://www.bilibili.com/video/av49996848?p=30
一些整理:
官网 - http://projects.spring.io/spring-framework/
官方文档:
- Spring Framework 5.2.3.RELEASE API : https://docs.spring.io/spring/docs/current/javadoc-api/
- Spring Framework Documentation https://docs.spring.io/spring/docs/current/spring-framework-reference/
- <mark>架构图</mark> - 【推荐阅读⭐️⭐️⭐️】 ⇒ https://www.cnblogs.com/lyang-a/p/12171354.html
- 《深入理解Spring MVC 思想》 - https://www.cnblogs.com/baiduligang/p/4247164.html
正式开始:
参考:
- 导入 jar 包 / maven
commons-logging - Apache提供的第三方日志模块 - 自动搜索并使用Log4j
其他各包的解释 - spring各模块-俯视图
或 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring.mvc</groupId>
<artifactId>learn-springmvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency><!-- 日志 -->
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency><!-- AOP 动态*** -->
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency><!-- IOC模块 -->
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency><!-- Spring上下文,生命周期管理 -->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency><!-- 核心容器,提供Spring框架的基本功能 -->
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency><!-- SPEL -->
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency><!-- Web上下文模块 -->
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency><!-- springmvc -->
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
</dependencies>
</project>
- web.xml
粘贴文件头
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
在导入文件头后,能直接生成 DispatcherServlet 配置 - : 没有提示的话 可以设置本地 schemalocation,参考
最后改成这样:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 配置 DispatcherServlet -->
<servlet><!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置 DispatcherServlet 的一个初始化参数: 配置 SpringMVC 配置文件的位置和名称 -->
<!-- 也可以不通过 contextConfigLocation 来配置 SpringMVC 的配置文件,而使用默认的 默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 添加 springmvc.xml 文件
没有装 spring 插件的,直接复制下面、或 直接下载安装使用 sts
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">
</beans>
- 写请求处理器
创建类
springmvc.xml 添加
<!-- 配置指定扫描的包 -->
<context:component-scan base-package="spring.mvc" />
类添加注解
@Controller
public class HelloWorld {
@RequestMapping("/helloworld")
public String hello() {
System.out.println("hello world");
return "success" ;
}
}
springmvc.xml 添加
<!-- 配置视图解析器: 如何把handler方法返回解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
添加jsp 页面