java中xml配置文件异同总结
java中默认的一个web项目中会有两个xml文件,一个是sys-config.xml,位于src跟目录下;一个是web层的WEB-INF下的web.xml。
两个配置文件有什么区别?
故名思议:sys-config.xml用于配置系统中的基本参数,通常将数据库访问这些数据放入到这里。另外,还可以在src目录下,建立一个新的xml文件,用于javaBean的反射。如,新建一个beanConfig.xml文件配合,工厂+配置文件+反射的设计模式解耦。
web.xml文件则是记录一些application、session、filter、servlet、error等这类的配置。下面是两类配置文件的示例:
sys-config.xml:
[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@localhost:1521:bjpowern</url>
<user-name>drp1</user-name>
<password>drp1</password>
</db-info>
</config></SPAN>
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@localhost:1521:bjpowern</url>
<user-name>drp1</user-name>
<password>drp1</password>
</db-info>
</config>
beanConfig.xml:
[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="UTF-8"?>
<beans>
<service-class>
<service id="com.bjpowernode.drp.basedata.manager.ItemManager" class="com.bjpowernode.drp.basedata.manager.ItemManagerImpl"/>
</service-class>
<dao-class>
<dao id="com.bjpowernode.drp.basedata.dao.ItemDao" class="com.bjpowernode.drp.basedata.dao.ItemDao4OracleImpl" />
</dao-class>
</beans></SPAN>
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<service-class>
<service id="com.bjpowernode.drp.basedata.manager.ItemManager" class="com.bjpowernode.drp.basedata.manager.ItemManagerImpl"/>
</service-class>
<dao-class>
<dao id="com.bjpowernode.drp.basedata.dao.ItemDao" class="com.bjpowernode.drp.basedata.dao.ItemDao4OracleImpl" />
</dao-class>
</beans>
web.xml:
所有的节点都在这里配置:
[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
<SPAN style="WHITE-SPACE: pre"> </SPAN>xmlns="http://java.sun.com/xml/ns/j2ee"
<SPAN style="WHITE-SPACE: pre"> </SPAN>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<SPAN style="WHITE-SPACE: pre"> </SPAN>xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
<SPAN style="WHITE-SPACE: pre"> </SPAN>http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<SPAN style="WHITE-SPACE: pre"> </SPAN>.........
<SPAN style="WHITE-SPACE: pre"></SPAN></web-app></SPAN>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
.........
</web-app>1、配置servlet:因为涉及到jsp的跳转访问,所以有<servlet-mapping>节点
[html]
<SPAN style="FONT-SIZE: 18px"> <servlet>
<servlet-name>ShowItemUploadServlet</servlet-name>
<servlet-class>com.bjpowernode.drp.basedata.web.ShowItemUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowItemUploadServlet</servlet-name>
<url-pattern><STRONG>/servlet/item/</STRONG>ShowItemUploadServlet</url-pattern>
</servlet-mapping></SPAN>
<servlet>
<servlet-name>ShowItemUploadServlet</servlet-name>
<servlet-class>com.bjpowernode.drp.basedata.web.ShowItemUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowItemUploadServlet</servlet-name>
<url-pattern>/servlet/item/ShowItemUploadServlet</url-pattern>
</servlet-mapping> 所有的<servlet>节点的作用是将servlet-name和sevlet-class包装关联;<servlet-mapping>节点的作用是将<servlet-name>和<url-pattern>关联,一个完整的servlet是通过<servlet-name>将<sevlet-class>和<url-pattern>映射。
<url-pattern>可以设置虚拟路径,比如这里的/servlet/item就是为了给系统划分模块,人为添加的,访问的时候需要与之匹配。
另外,我们可以在servlet中配置一些参数,在该servlet初始化的时候,来访问这些数据。如,可以这样配置一个带有默认page-size的关于分页的servlet。这个参数的作用范围只限于该servlet。
[html]
<SPAN style="FONT-SIZE: 18px"> <servlet>
<servlet-name>SearchItemServlet</servlet-name>
<servlet-class>com.bjpowernode.drp.basedata.web.SearchItemServlet</servlet-class>
<STRONG><init-param>
<param-name>page-size</param-name>
<param-value>3</param-value>
</init-param></STRONG>
</servlet></SPAN>
<servlet>
<servlet-name>SearchItemServlet</servlet-name>
<servlet-class>com.bjpowernode.drp.basedata.web.SearchItemServlet</servlet-class>
<init-param>
<param-name>page-size</param-name><
补充:软件开发 , Java ,