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>

相关推荐
Nazarick37 分钟前
从 fetch 到 then:Demo在线订单系统 Ajax 回调的逐帧拆解
java·javascript·ajax·web
智购科技自动售货机厂家1 小时前
自动售货机外设接口与总线设计实战:串口、CAN 与 GPIO 的分工~YH
java·大数据·服务器·数据库·人工智能·单片机·嵌入式硬件
路灯上の马陈1 小时前
Java File类2 练习 学习笔记
java·笔记·学习
Shaoshing1 小时前
LeetCode100 155.最小栈
java·数据结构·算法
SummerJokes1 小时前
Java 多线程专项实验|两种创建线程方式、synchronized 线程同步、银行存取款实战
java·开发语言·python
二宝哥2 小时前
07.Spring AI 接入智谱文生图:从配置到实战
java·人工智能·spring
秋天的怀念_David2 小时前
算法一千零一夜 · 第四十夜 · 错层的状态码
java·程序人生·算法
xyphf_和派孔明2 小时前
第九章: 上下文工程五大策略与六大模块交叉矩阵纵览
java·开发语言
device_Any3 小时前
线上CPU飙高,排查先分清三种形态
java·后端开发