SpringMVC中spring-config.xml和web.xml配置

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 初始化加载配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-config3.xml</param-value>
  	</init-param>
  	<!-- 在服务器启动的时候加载Sevlet -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 配置编码过滤器 -->
  <filter>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<filter-class>
  		org.springframework.web.filter.CharacterEncodingFilter
  	</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>utf-8</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>CharacterEncodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

spring-config.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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">
		<!-- 开启注解扫描,指定扫描的包 -->
    	<context:component-scan base-package="con.wx.controller"></context:component-scan>
		<!-- 配置注解驱动 -->
		<mvc:annotation-driven />
		<!-- 配置静态资源映射,不被前端控制器拦截 -->
		<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
		<!-- 配置视图解析器 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/jsp/"></property>
			<property name="suffix" value=".jsp"></property>
		</bean>
</beans>

 

# 题目重述 如何在 IntelliJ IDEA 中完成“配置 SpringMVC”的操作,即创建并正确设置 `spring-mvc.xml` `web.xml` 文件? --- # 详解 这一步是 SSM 项目的核心配置环节,目的是让 Spring MVC 框架能够接管请求处理。以下是**详细、可执行的操作步骤**: --- ## ✅ 第一部分:创建 `spring-mvc.xml` 文件 ### 步骤 1:进入 `resources` 目录 - 打开项目结构,定位到: ``` src/main/resources ``` ### 步骤 2:创建 `spring-mvc.xml` 文件 1. 右键点击 `resources` 文件夹 → `New` → `XML Configuration File` → `Spring Config` 2. 在弹出窗口中: - 输入文件名:`spring-mvc.xml` - 点击“Create” > 如果没有 Spring Config 选项,请选择 `File` → 输入名称 `spring-mvc.xml` ### 步骤 3:编辑 `spring-mvc.xml` 内容 将以下内容复制进文件中(注意使用正确的命名空间): ```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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 扫描 Controller 包 --> <context:component-scan base-package="com.itheima.controller"/> <!-- 视图解析器:前缀 + viewName + 后缀 = 完整路径 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 开启注解驱动,支持 @Controller、@RequestMapping 等 --> <mvc:annotation-driven/> <!-- 允许静态资源(如 HTML、CSS、JS)被默认 Servlet 处理 --> <mvc:default-servlet-handler/> </beans> ``` ✅ 保存文件。 --- ## ✅ 第二部分:配置 `web.xml` 文件 ### 步骤 1:确认 `web.xml` 存在 - 路径应为: ``` src/main/webapp/WEB-INF/web.xml ``` > 如果使用 Maven archetype 创建的 webapp 项目,通常已自动生成一个基础版本。 #### 若不存在,请手动创建: 1. 进入目录: ``` src/main/webapp/WEB-INF/ ``` 2. 右键 → `New` → `XML Configuration File` → `web.xml` 3. 选择版本 **4.0**(对应 Servlet 4.0),点击 OK --- ### 步骤 2:编辑 `web.xml` 内容 将以下完整内容替换原文件内容: ```xml <?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"> <!-- 设置 Spring 配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-*.xml</param-value> </context-param> <!-- 启动时加载 Spring 容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 前端控制器 DispatcherServlet --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定 Spring MVC 的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!-- 项目启动时立即初始化 Servlet --> <load-on-startup>1</load-on-startup> </servlet> <!-- 映射所有请求给 DispatcherServlet --> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 统一编码过滤器,解决中文乱码 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 可选:JSTL 标签库支持(防止 JSP 报错) --> <jsp-config> <taglib> <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri> <taglib-location>/WEB-INF/tld/c.tld</taglib-location> </taglib> </jsp-config> </web-app> ``` > 💡 注意:`<url-pattern>/</url-pattern>` 表示拦截所有请求,由 Spring MVC 统一处理。 --- ## ✅ 第三步:验证配置是否生效 ### 方法一:检查是否有错误提示 - 打开 `web.xml` `spring-mvc.xml` - 确保没有红色波浪线或 schema 报错 - 如果有报错,可能是网络未下载 DTD/XSD,可忽略或改为离线模式 ### 方法二:运行项目测试 1. 配置 Tomcat 或使用插件运行: ```bash mvn tomcat7:run ``` 2. 访问页面(如 `/index.jsp`) 3. 观察是否能正常跳转、控制器能否接收请求 --- # 知识点 1. **DispatcherServlet 的作用** Spring MVC 的前端控制器,接收所有 HTTP 请求并分发给对应的 `@Controller` 类处理。 2. **ContextLoaderListener 与上下文关系** 加载 `applicationContext.xml` 等业务层 Bean;而 `DispatcherServlet` 加载 MVC 相关组件。 3. **视图解析器工作原理** 将逻辑视图名(如 `"userlist"`)拼接前缀 `/WEB-INF/pages/` 后缀 `.jsp`,得到实际路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值