Spring MVC配置文件

在Spring MVC项目中,配置文件是至关重要的,它们帮助Spring框架正确地初始化并管理应用。通常,在web.xml中配置Spring的DispatcherServlet,并在applicationContext.xml和spring-mvc.xml中配置Spring的Bean定义和MVC相关设置。

  1. web.xml配置

web.xml是Web应用的部署描述符,用于配置Servlet容器启动时需要加载的Servlet、监听器等。对于Spring MVC应用,通常需要在这里配置DispatcherServlet。

‌示例配置:‌

<?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">

<display-name>Spring MVC Application</display-name>

<!-- Spring DispatcherServlet Configuration -->

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring-mvc-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!-- Spring Listener -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

</web-app>

  1. applicationContext.xml配置

applicationContext.xml通常包含Spring的全局配置,例如数据源、事务管理器、业务层Bean等。这个文件通常不包含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"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- DataSource configuration -->

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://localhost:3306/yourdatabase"/>

<property name="username" value="root"/>

<property name="password" value="password"/>

</bean>

<!-- Transaction manager -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<!-- Other beans -->

</beans>

  1. spring-mvc.xml配置(或使用Java Config)

对于MVC特定的配置,可以在spring-mvc.xml中定义,或者使用Java配置类(推荐方式)。spring-mvc.xml应该包含视图解析器、控制器、拦截器等MVC相关的配置。

‌XML配置示例:‌

<?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:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 扫描 Controller 层 -->

<context:component-scan base-package="com.example.controller" />

<!-- 启用注解驱动(@RequestMapping, @ResponseBody 等) -->

<mvc:annotation-driven />

<!-- 静态资源处理:避免 DispatcherServlet 拦截 /static/ 下的资源 -->

<mvc:resources mapping="/static/**" location="/static/" />

<!-- 视图解析器:将逻辑视图名解析为 JSP 文件 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/views/"/>

<property name="suffix" value=".jsp"/>

</bean>

<!-- 文件上传配置(如需) -->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="maxUploadSize" value="5242880"/> <!-- 5MB -->

</bean>

</beans>

相关推荐
w_zero_one16 分钟前
算法--模拟
java·数据结构·算法
Javatutouhouduan24 分钟前
阿里1658页Java面试题总结(含答案)
java·高并发·java面试·java面试题·后端开发·java程序员·java八股文
秋天的怀念_David41 分钟前
算法一千零一夜 · 第十三夜 幻方,武侠世界里的数学迷宫
java·程序人生·算法
专业程序开发源1 小时前
springboot欧缇蔓月子中心管理系统82697-计算机课程设计、毕业设计
java·spring boot·后端·python·spring·django·课程设计
秋天的怀念_David1 小时前
算法一千零一夜 · 第十四夜 幻方与奇妙的二进制
java·程序人生·算法
thefool1122661 小时前
网络编程^
java
Thinker QAQ2 小时前
并发编程(十一):Shared Memory 与 Message Passing——关系与组合使用
java·python·go·并发编程·shared memory·message passing
齐 飞2 小时前
idea中git误操作撤回git reset --hard ‘HEAD@{1}‘
java·git·intellij-idea
你的保护色3 小时前
多AS互联BGP4综合实验设计
java
某不知名網友3 小时前
ROS2进阶:服务通信、自定义SRV接口、参数服务器与基于服务的参数通信详解
java·开发语言·microsoft