注解方式配置springMVC开发环境
- 用创建一个mavenWeb项目

- 构建项目目录

- 配置tomcat、添加构建


- 在maven中添加依赖
在pom.xml文件中配置:
4.1配置编译环境和编译字符集
<!--配置编译环境和编译字符集-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.8.RELEASE</spring.version>
</properties>
4.2配置spring基本依赖
<!--会向上检索并下载相关依赖(包括spring基本依赖)-->
<!--spring-webmvc会依赖spring-web-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
</dependencies>

- 导入需要的jar包:

添加依赖会自动下载导入
- 配置web.xml文件
6.1页面架构
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
</web-app>
6.2在web.xml中配置DispatcherServlet
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 也可不配置参数,默认加载 /WEB-INF/springmvc-servlet.xml -->
<!--配置springMVC的核心配置文件路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
6.3 配置DispatcherServlet访问路径
<!--配置处理器映射器路径-->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--/匹配所有路径,但是不包括html,jsp-->
<url-pattern>/</url-pattern>
</servlet-mapping>
如图:

7.配置springmMVC的核心配置文件
- 在resource目录下创建.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
- 配置注解映射器和适配器
<!--配置映射器和适配器-->
<mvc:annotation-driven />
- 编写注解处理器
①创建一个包存放处理器实例

② 处理器就是一个个类,通过注解(@Controller)将其识别为一个处理器

- 在spring容器中加载处理器
<!--配置处理器:将处理器管理进spring容器-->
<context:component-scan base-package="com.neusoft.springmvc1.controller" />


ok~
本文介绍如何使用注解方式配置Spring MVC开发环境,包括创建Maven Web项目、配置Tomcat及依赖、设置编译环境、配置核心文件及处理器等关键步骤。
&spm=1001.2101.3001.5002&articleId=121361479&d=1&t=3&u=137340cf05c44b2bb55c3a446da6ca77)
4125

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



