所谓SSH,即struts+spring+hibernate的J2EE架构。
1.环境下载:Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1
http://struts.apache.org/download.cgi
http://www.springsource.org/download
http://in.relation.to/Bloggers/HibernateORM412Release
重要配置:
Web.xml
- <?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"
- id="WebApp_ID" version="3.0">
- <display-name>Eriloan_com</display-name>
- <session-config>
- <session-timeout>30</session-timeout>
- </session-config>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/spring-config/applicationContext-*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>
- </listener>
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>struts-cleanup</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts-cleanup</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <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>
- <filter>
- <filter-name>openSessionInViewFilter</filter-name>
- <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
- <init-param>
- <param-name>sessionFactoryBeanName</param-name>
- <param-value>sessionFactory</param-value>
- </init-param>
- <init-param>
- <param-name>singleSession</param-name>
- <param-value>true</param-value>
- </init-param>
- <init-param>
- <param-name>flushMode</param-name>
- <param-value>AUTO</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>openSessionInViewFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <error-page>
- <error-code>404</error-code>
- <location>/WEB-INF/errorPage/404.jsp</location>
- </error-page>
- <error-page>
- <error-code>500</error-code>
- <location>/WEB-INF/errorPage/500.jsp</location>
- </error-page>
- </web-app>
Spring配置文件(applicationContext-common.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:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
- xmlns:tool="http://www.springframework.org/schema/tool" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/jee
- http://www.springframework.org/schema/jee/spring-jee.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util.xsd
- http://www.springframework.org/schema/tool
- http://www.springframework.org/schema/tool/spring-tool.xsd"
- default-lazy-init="true" default-autowire="byName">
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:/dataBaseInfo.properties</value>
- </list>
- </property>
- </bean>
- <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
- p:jndiName="java:comp/env/jdbc/MySSH" /> -->
- <!-- BoneCP -->
- <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
- p:driverClass="${jdbc.driver}" p:jdbcUrl="${jdbc.url}" p:username="${jdbc.username}"
- p:password="${jdbc.password}" p:idleConnectionTestPeriodInMinutes="${idleConnectionTestPeriodInMinutes}"
- p:idleMaxAgeInMinutes="${idleMaxAgeInMinutes}"
- p:maxConnectionsPerPartition="${maxConnectionsPerPartition}"
- p:minConnectionsPerPartition="${minConnectionsPerPartition}"
- p:partitionCount="${partitionCount}" p:acquireIncrement="${acquireIncrement}"
- p:statementsCacheSize="${statementsCacheSize}"
- p:disableConnectionTracking="${disableConnectionTracking}"
- p:releaseHelperThreads="${releaseHelperThreads}" destroy-method="close" />
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
- p:dataSource-ref="dataSource">
- <property name="mappingDirectoryLocations">
- <list>
- <value>classpath:/com/eriloan/web/test/bo/</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
- <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
- <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
- <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
- <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
- <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
- <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
- <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
- <prop key="hibernate.order_updates">${hibernate.order_updates}</prop>
- <prop key="hibernate.query.factory_class">${hibernate.query.factory_class}</prop>
- <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
- <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
- <!-- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> -->
- </props>
- </property>
- </bean>
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate4.HibernateTransactionManager" />
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor"
- p:transactionManager-ref="transactionManager">
- <property name="transactionAttributes">
- <props>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="sync*">PROPAGATION_REQUIRED</prop>
- <prop key="finish*">PROPAGATION_REQUIRED</prop>
- <prop key="add*">PROPAGATION_REQUIRED</prop>
- <prop key="insert*">PROPAGATION_REQUIRED</prop>
- <prop key="edit*">PROPAGATION_REQUIRED</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="save*">PROPAGATION_REQUIRED</prop>
- <prop key="remove*">PROPAGATION_REQUIRED</prop>
- <prop key="delete*">PROPAGATION_REQUIRED</prop>
- <prop key="*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
- </props>
- </property>
- </bean>
- <bean id="ProxyCreator"
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
- p:beanNames="*Service,*ServiceImpl" p:interceptorNames="transactionInterceptor" />
- <!-- 数据库操作Bean -->
- <bean id="dao" class="dao.DaoImpl" scope="singleton" />
- <!--Service 原始Bean -->
- <bean id="baseService" class="service.BaseServiceImpl" scope="singleton" />
- <!--Action 原始Bean -->
- <bean id="baseAction" class="action.BaseAction" scope="prototype" />
- </beans>