SpringMVC教程
Spring MVC基础
框架概述
SpringMVC环境搭建
SpringMVC请求处理与注解
SpringMVC请求映射
SpringMVC注解详解
SpringMVC配置与执行流程
SpringMVC项目配置
Spring执行流程
SpringMVC数据提交与Ajax
SpringMVC数据提交
Spring MVC Ajax交互
Spring MVC拦截器
拦截器概念
Spring MVC权限验证
-
+
首页
SpringMVC项目配置
### 项目配置 在配置一个使用Spring MVC的Maven项目时,你需要完成以下几个步骤: #### 1. 创建Maven项目 在IDE中创建一个新的Maven项目,并选择Web应用作为项目类型。 #### 2. 配置`pom.xml` 在项目的`pom.xml`文件中添加Spring MVC和相关依赖。以下是一个基本的`pom.xml`配置示例: ```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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>springmvc-project</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.10</version> </dependency> <!-- Spring Context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.10</version> </dependency> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- JSTL for JSP --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> <configuration> <warSourceDirectory>src/main/webapp</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> ``` #### 3. 配置`web.xml` 在`src/main/webapp/WEB-INF`目录下创建或修改`web.xml`文件,配置`DispatcherServlet`: ```xml <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"> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` #### 4. 配置`springmvc.xml` 在`src/main/resources`目录下创建`springmvc.xml`文件,配置Spring MVC的相关组件: ```xml <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"> <!-- 开启Spring MVC的注解功能,完成请求和Controller方法的映射 --> <mvc:annotation-driven /> <!-- 静态资源默认servlet配置 --> <mvc:default-servlet-handler/> <!-- 扫描组件包路径 --> <context:component-scan base-package="com.example"/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans> ``` 在这个配置文件中,`<mvc:annotation-driven />`启用了Spring MVC的注解驱动功能,`<context:component-scan base-package="com.example"/>`指定了组件扫描的包路径,`<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">`配置了视图解析器。 完成以上步骤后,你的Spring MVC项目的基本配置就完成了。接下来,你可以添加Controller、Service、Repository等组件,并开始开发你的Web应用程序。
wwbang
2025年1月3日 16:52
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码