SSM框架之web.xml解析

web.xml配置详解
本文详细介绍了web.xml配置文件的关键组成部分,包括XML版本声明、schema引用、context-param设置、listener配置、servlet定义及其映射等内容。
一:<?xml version="1.0" encoding="UTF-8"?>
1. ?xml version="1.0": 这一行代码会告诉解析器和浏览器,这个文件应该按照1.0版本的XML规则进行解析。 2.encoding = "utf-8":表示此xml文件采用utf-8的编码格式。

二:<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" --xml遵循的标签规范
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"/> ----用来定义xmlschema的地址,也就是xml书写时需要遵循的语法

三:<context-param></context-param>
1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext = ServletContextEvent.getServletContext()

四:<listener></listener>
1.对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
2.通过<listener-class> ,listener元素指出事件监听程序类。
3.在启动的时候加载文件用。

五:<servlet> </servlet>
1.<servlet-name> 这个是我们要注册servlet的名字,一般跟Servlet类名有关.起名随意。
2.<servlet-class> 这个就是指向我们要注册的servlet 的类地址, 要带包路径。

六:<servlet-mapping> </ servlet-mapping>
1.<servlet-name> 这个要与 前面写的servlet—name 一致。
2. <url-pattern> 配置这个组件的访问路径。

七: <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

此段代码 作用:用来加载其他的 配置文件,此处加载的是:applicationContext.xml

注意:
1.contextConfigLocation 不要错单词哦,如果写错会报:BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext。 像这种参数,要么是web.xml中规定参数,要么是加载类中的参数(杨老师威武)

2.如果出现以下错误,请检查web.xml里,监听是否配置正确。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.lanqiao.ssm.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
### SSM框架整合中的`pom.xml`配置 在SSM框架整合过程中,`pom.xml`扮演着至关重要的角色。为了确保Spring、Spring MVC以及MyBatis能够无缝协作,合理的依赖管理至关重要[^3]。 #### Maven父工程与子模块结构 采用父工程加子模块的方式可以更好地管理和维护项目的各个部分。通常情况下,整个项目会被划分为多个子模块来分别处理不同的功能需求。对于SSM框架而言,主要涉及两个方面: - **Spring和Spring MVC的集成** 这一部分负责Web层逻辑控制及视图解析等功能实现。通过引入相应的坐标库,可轻松完成这部分工作。 - **Spring和MyBatis的融合** 主要用于持久化操作,即数据库交互层面的设计。同样借助于特定版本的JAR包支持得以达成目标。 具体的`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"> <!-- 父级POM定义 --> <parent> <groupId>com.example</groupId> <artifactId>ssm-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <dependencies> <!-- Spring核心组件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Web相关 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- MyBatis基础库 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- Spring与MyBatis桥接器 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <!-- 数据源连接池 --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>${hikaricp.version}</version> </dependency> <!-- MySQL驱动程序 (根据实际使用的数据库调整)--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 解决Mapper XML路径问题 --> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> </resources> </build> </dependencies> </project> ``` 上述代码展示了如何在一个典型的SSM应用中设置必要的依赖项,并解决了可能出现的`mapper.xml`文件加载失败的问题[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值