这里就是做一下记录说明,防止以后忘记这些基础知识。
主要知识点 命名空间
命名空间用来解决 比如同一个xml文件里面,相同标签但代表不同含义的 场景问题。
<config>
<table> xxx <table> 这个代表表格意思
<table> bbb </table> 这个代表桌子意思
</config>
两个非要在一个文件里面,就只有给他们加一个标签隔离,这个标签约等于“命名空间” 也类似于编程语言里面的 包(package)概念。
怎么加标签?
<config xmlns:A="这里的字符串表示表格含义的标签,即表格命名空间"
xmlns:B="这里的字符串表示桌子含义的标签,即桌子命名空间">
<A:table> xxx <A:table> 这个代表表格意思
<B:table> bbb </B:table> 这个代表桌子意思
</config>
一般命名空间用唯一的URL值表示,
上面的 A 和 B 可以看成命名空间的 代表符号(namespace-prefix),
因为用后面的URL表示不太方便,如果不用 A 和B 则就会如下面:
<config xmlns:A="http://www.biaoge.cn/aaa"
xmlns:B="http://www.zhuozi.cn/bbb">
<http://www.biaoge.cn/aaa:table> xxx </http://www.biaoge.cn/aaa:table> 这个代表表格意思
<http://www.zhuozi.cn/bbb:table> bbb </http://www.zhuozi.cn/bbb:table> 这个代表桌子意思
</config>
当然每个XML文件一般还会在增加一个默认的命名空间定义,如下:
<config xmlns="http://www.moren.cn/111"
xmlns:A="http://www.biaoge.cn/aaa"
xmlns:B="http://www.zhuozi.cn/bbb">
<A:table> xxx </A:table> 这个代表表格意思
<B:table> bbb </B:table> 这个代表桌子意思
<free> 默认命名空间 </free>
</config>
上面第一行 xmlns=“http://www.moren.cn/111” 就是默认的命名空间,可以看到它没有代表符号(namespace-prefix)。
<free> 默认命名空间 </free>
等同于已经加了默认命名空间
<http://www.moren.cn/111:free> 默认命名空间 </http://www.moren.cn/111:free>
看具体的例子:
一般java开发者经常会看到如下xml文件内容:
<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
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="cn.cg.controller"/>
<!-- 全局异常处理 -->
<bean id="exceptionResolver" class="cn.guzt.exception.BizExceptionResolver">
<property name="defaultStatusCode" value="500" />
<property name="warnLogCategory" value="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" />
</bean>
<!-- 其他一些配置 -->
</beans>
- 第一行 表示 该文件为xml格式的文件,该文件编码为 UTF-8编码方式
- 第二行 表示默认命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
....>
- 第三行 声明XML Schema 实例,声明后就可以使用 schemaLocation 属性了
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
告知XML解析器根据某个 XSD文件来验证此XML文档里面的标签元素拼写是否正确,都有些什么属性可以智能提示等。
下面xsi:schemaLocation是指定某个命名空间具体的校验XSD文件位置。
xsi:schemaLocation="
http://www.springframework.org/schema/beans // 这一行对应上面的具体哪个命名空间,即URL地址,一定要一一对应
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd //规定了schema 的位置在这里
<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="
<!-- 默认空间的schema验证文件 -->
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
<!-- context空间的schema验证文件 -->
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
<!-- mvc空间的schema验证文件 -->
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">