通常Java Web 开发的时候,我们一般会采用三大框框架的整合来架构,这样做主要是为方便维护。但是有时候会忘了一些配文件(*.xml)的格式头,这里主要就三大框架Spring、struts、hibernate开发过程中用到的集中配置文件注明,方便以后查看。
1.hibernate中的映射描述文件*.hbm.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.yeeku.model.User" table="app_user">
<id name="username" column="username" length="20" unsaved-value="version">
<!-- The generator-class attribute of @hibernate.id is deprecated, use the @hibernate.generator tag instead -->
<generator class="assigned">
</generator>
</id>
<property name="password" column="password" not-null="true">
</property>
</class>
</hibernate-mapping></span>
2.spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置hibernate datasource数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=dbtemp"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
<property name="maxActive" value="20"/>
<property name="maxIdle" value="10"/>
<property name="initialSize" value="1"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
<property name="logAbandoned" value="true"/>
</bean>
<!-- 配置Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>org/yeeku/model/User.hbm.xml</value>
<value>org/yeeku/model/News.hbm.xml</value>
<value>org/yeeku/model/NewsReview.hbm.xml</value>
<value>org/yeeku/model/Category.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<!-- 配置事务Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- service manager Template-->
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- service manager-->
<bean id="facadeManager" parent="txProxyTemplate">
<property name="target">
<bean class="org.yeeku.service.impl.FacadeManagerImpl">
<property name="newsDAO" ref="newsDAO"/>
<property name="newsReviewDAO" ref="newsReviewDAO"/>
<property name="categoryDAO" ref="categoryDAO"/>
<property name="userDAO" ref="userDAO"/>
</bean>
</property>
</bean>
</beans>
3.Struts配置文件struts.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="guessGame" extends="struts-default">
<!-- 登录拦截 -->
<interceptors>
<interceptor name="logincheck" class="action.LoginCheck"/>
<interceptor-stack name="demoStack">
<interceptor-ref name="logincheck"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<!-- 定义全局的Result -->
<global-results>
<result name="error">/error.jsp</result>
<result name="login">/login.jsp</result>
</global-results>
<!-- 登录 -->
<action name="login" class="action.Login">
<result name="success" type="redirect">/menu.jsp</result>
<result name="exit" type="redirect">/login.jsp</result>
</action>
<!-- 注册 -->
<action name="regist" class="action.RegistAction">
<result name="add" type="redirect">/login.jsp</result>
<result name="wrong">/add_player.jsp</result>
</action>
<action name="guess" class="action.GuessAction">
<interceptor-ref name="demoStack"/>
<result name="success">/success.jsp</result>
<result name="wrong">/guess.jsp</result>
<result name="numerror">guess.jsp</result>
<result name="init" type="redirect">/guess.jsp</result>
</action>
<action name="option" class="action.Option">
<interceptor-ref name="demoStack"/>
<result name="show">/showInfo.jsp</result>
</action>
</package>
</struts></span>
4.web.xml配置文件<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 启动加载spring配置,创建容器实例 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts过滤器 -->
<filter>
<filter-name>strutsaction</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>strutsaction</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app></span>
5需要用到的jar包
其中mysql-connector-java-5.1.6-bin.jar和sqljdbc4.jar都是连接数据库的驱动包,只需要一个就行