SSM框架之web.xml解析

一:<?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)}

猜你喜欢

转载自greatexpectations.iteye.com/blog/2315543
今日推荐