在學習spring
的時候需要配置一個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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="****" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/WEB-INF/vm/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">utf-8</prop>
<prop key="output.encoding">utf-8</prop>
</props>
</property>
</bean>
</beans>
以前沒怎么學習過xml
文件屎慢,學過之后做個筆記。
參考文獻:
命名空間解決的問題
XML的元素名字是不固定的腻惠,當兩個不同的文檔使用同樣的名稱描述兩個不同類型的元素的時候,或者一個同樣的標記表示兩個不同含義的內(nèi)容的時候欲虚,就會發(fā)生命名沖突集灌。
命名空間(Namespace
),對于每一套特定應用的DTD复哆,給它一個獨一無二的標志來代表欣喧,如果在XML中使用DTD中定義的元素,需將DTD的標志和元素名梯找,屬性連在一起使用唆阿,相當于指明了元素來自什么地方,這樣就不會同其他同名元素混淆了锈锤。在XML中驯鳖,采用現(xiàn)成的闲询,在全球范圍唯一的域名作為Namespace
,即URL
作為XML的Namespace
浅辙。
命名空間的語法
xmlns:[prefix]="url"
其中xmlns:
是必須的屬性扭弧。prefix
是命名空間的別名,它的值不能為xml记舆。如果沒有別名鸽捻,則為默認默認命名空間。
- 默認
Namespace
xmlns="url" - 指定了父元素的命名空間泽腮,子元素希望用自己的命名空間御蒲,可以在子元素中指定命名空間的別名。
- 屬性也可以有自己的命名空間盛正。
這些命名空間都有一些規(guī)范删咱,包括含有什么元素、屬性等豪筝,這些規(guī)范寫在命名空間為http://www.w3.org/2001/XMLSchema-instance
(即xsi
)的schemaLocation
(xsi:"schemaLocation"
)元素中痰滋。
xsi:schemaLocation
XML Schema提供了兩個在實例文檔中使用的特殊屬性,用于指出模式文檔的位置续崖。這兩個屬性是:xsi:schemaLocation
和xsi:noNamespaceSchemaLocation
敲街,前者用于聲明了目標名稱空間的模式文檔,后者用于沒有目標名稱空間的模式文檔严望,它們通常在實例文檔中使用多艇。
它定義了XML Namespace
和對應的 XSD
(Xml Schema Definition)文檔的位置的關系。它的值由一個或多個URI引用對組成像吻,兩個URI之間以空白符分隔(空格和換行均可)峻黍。第一個URI是定義的 XML Namespace的值,第二個URI給出Schema文檔的位置拨匆,Schema處理器將從這個位置讀取Schema文檔姆涩,該文檔的targetNamespace
必須與第一個URI相匹配。
xsd
文件示例:
<!--?xml version="1.0" encoding="UTF-8"?-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.sunxin.org/book"
targetnamespace="http://www.sunxin.org/book"
elementformdefault="qualified">
<xs:element name="book" type="bookType"></xs:element>
<xs:complextype name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"></xs:element>
<xs:element name="author" type="xs:string"></xs:element>
</xs:sequence>
</xs:complextype>
</xs:schema>
對應xml
文件示例:
<!--?xml version="1.0" encoding="GB2312"?-->
<book xmlns="http://www.sunxin.org/book" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://www.sunxin.org/book http://www.sunxin.org/
book.xsd">
<title>《Struts 2深入詳解》</title>
<author>孫鑫</author>
</book>