最近在学SSH三大框架的整合,在此对他们整合时相关配置文件做一简单的模板总结,方便以后复用!
首先是web.xml配置文件,这里面就配置一些简单的监听器、过滤器,包括spring核心配置文件applicationContext.xml的路径:
web.xml
- <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app></span>
接着就是spring的配置文件:applicationContext.xml,可能很多人更习惯命名为beans.xml,由于习惯使用注解方式开发,所以这个是基于annotation的配置,相对xml比较简单!由于这里面已经完成了hibernate配置文件的功能,比如配置数据库等等,所以hibernate.cfg.xml就没必要“苟活”了。
applicationContext.xml
- <span style="font-size:18px;"><?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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <context:annotation-config />
- <context:component-scan base-package="com.zzw" />
- <context:property-placeholder location="classpath:jdbc.properties" />
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="${jdbc.driverClassName}" />
- <property name="jdbcUrl" value="${jdbc.url}" />
- <property name="user" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- <!-- 指定连接数据库连接池的初始化连接数 -->
- <property name="initialPoolSize" value="1" />
- <!-- 指定连接数据库连接池的最大连接数 -->
- <property name="maxPoolSize" value="40" />
- <!-- 指定连接数据库连接池的最小连接数 -->
- <property name="minPoolSize" value="1" />
- <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
- <property name="maxIdleTime" value="20" />
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="packagesToScan">
- <list>
- <value>com.zzw.entity</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.show_sql">true</prop>
- <prop key="javax.persistence.validation.mode">none</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- <bean id="txManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <aop:config>
- <aop:pointcut id="service"
- expression="execution(public * com.zzw.service..*.*(..))" />
- <aop:advisor pointcut-ref="service" advice-ref="txAdvice" />
- </aop:config>
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true" />
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="add*" propagation="REQUIRED" />
- <tx:method name="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
- </beans></span>
由于上面的配置文件涉及到运用DataSource得起数据库的连接,关系到jdbc.properties文件,所以在此举一个mysql的简单示例:
jdbc.properties
- <span style="font-size:18px;">jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/ssh
- jdbc.username=root
- jdbc.password=root</span>
关于log4j的日志记录,没什么可说的,根据需要配置,简单示例如下:
log4j.properties
- <span style="font-size:18px;">### direct log messages to stdout ###
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target=System.out
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
- ### direct messages to file hibernate.log ###
- #log4j.appender.file=org.apache.log4j.FileAppender
- #log4j.appender.file.File=hibernate.log
- #log4j.appender.file.layout=org.apache.log4j.PatternLayout
- #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
- ### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ###
- log4j.rootLogger=warn, stdout
- #log4j.logger.org.hibernate=info
- #log4j.logger.org.hibernate=debug
- ### log HQL query parser activity
- #log4j.logger.org.hibernate.hql.ast.AST=debug
- ### log just the SQL
- #log4j.logger.org.hibernate.SQL=debug
- ### log JDBC bind parameters ###
- #log4j.logger.org.hibernate.type=info
- #log4j.logger.org.hibernate.type=debug
- ### log schema export/update ###
- log4j.logger.org.hibernate.tool.hbm2ddl=debug
- ### log HQL parse trees
- #log4j.logger.org.hibernate.hql=debug
- ### log cache activity ###
- #log4j.logger.org.hibernate.cache=debug
- ### log transaction activity
- #log4j.logger.org.hibernate.transaction=debug
- ### log JDBC resource acquisition
- #log4j.logger.org.hibernate.jdbc=debug
- ### enable the following line if you want to track down connection ###
- ### leakages when using DriverManagerConnectionProvider ###
- #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
- </span>
至于struts.xml文件的配置就根据具体action的要求来了,比如我这次的雇员管理系统里面的简单配置如下:
struts.xml
- <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <constant name="struts.enable.DynamicMethodInvocation" value="true" />
- <constant name="struts.devMode" value="true" />
- <package name="default" namespace="/" extends="struts-default">
- <action name="employee" class="com.zzw.action.EmployeeAction">
- <result name="loginSuccess">/showAllEmployee.jsp</result>
- <result name="loginFail">/loginFail.jsp</result>
- <result name="showAll">/showAllEmployee.jsp</result>
- <result name="addSuccess">/add_success.jsp</result>
- <result name="deleteSuccess">/delete_success.jsp</result>
- <result name="goUpdatePage">/updateEmployee.jsp</result>
- <result name="updateSuccess">/updateSuccess.jsp</result>
- </action>
- </package>
- </struts>
- </span>
以上基本都是这次雇员管理系统的相关配置实例,很多地方简单化,实际上还可优化,所以仅作为参考,具体根据项目需要进行修改!
文章来自http://blog.csdn.net/qq799499343/article/details/7835477
三大框架SSH(struts2+spring+hibernate)整合时相关配置文件的模板,布布扣,bubuko.com