SSH整合
整合步骤:
1) 引入SSH Jar文件
Struts 核心jar
Hibernate 核心jar
Spring
Core 核心功能
Web 对web模块支持
Aop aop支持
Orm 对hibernate支持
2)配置
Web.xml
初始化struts功能、spring容器
Struts.xml 配置请求路径与映射action的关系
Spring.xml IOC容器配置
bean.xml 【公用信息】
web.xml <?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"> <display-name></display-name> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> Spring配置,在服务器启动时创建 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> Struts2配置 <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> </web-app>
struts2常量配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 0,修改接收参数和页面编码 (默认为:UTF-8, 所以这项可以不用设置) --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 1,修改struts的默认访问后缀 struts.action.extension=action,, --> <constant name="struts.action.extension" value="action,do,"></constant> <!-- 2,当struts配置文件修改时是否自动重新加载该文件,开发条件下开启,生产阶段关闭 struts.configuration.xml.reload=false 默认关闭 --> <constant name="struts.configuration.xml.reload" value="true"></constant> <!-- 3,设置浏览器是否自动缓存静态内容,生产模式开启,开发模式关闭 struts.serve.static.browserCache=false --> <constant name="struts.serve.static.browserCache" value="false"></constant> <!-- 4,设置上传文件大小限制 struts.multipart.maxSize=2097152 --> <constant name="struts.multipart.maxSize" value="100000000"></constant> </struts>
Action配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="emp" extends="struts-default"> <action name="test" class="action.UserAction" method="insert"> <result name="success">/success.jsp</result> </action> </package> <include file="struts.xml"></include> </struts>
Spring自动装配和hibernate的SessionFactory配置(其中连接池交给spring)
<?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:p="http://www.springframework.org/schema/p" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- IOC容器的配置: 要创建的所有的对象都配置在这里 --> <context:component-scan base-package="dao,service,action"/> <!-- 1,连接池参数配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"/> <property name="password" value="7758"/> <property name="maxPoolSize" value="5"/> <property name="maxIdleTime" value="6000"/> <property name="maxStatements" value="100"/> <property name="minPoolSize" value="2"/> <property name="acquireIncrement" value="3"/> <property name="jdbcUrl" value="jdbc:mysql:///day18_login"/> </bean> <!-- ####################spring与hibernate整合######################## --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="dataSource" ref="dataSource"/> <property name="mappingDirectoryLocations"> <list> <value>classpath:bean</value> </list> </property> <!-- <property name="mappingResources"> <list> <value>bean/User.hbm.xml</value> </list> </property> --> <!-- <property name="mappingLocations"> <list> <value>classpath:bean/User.hbm.xml</value> </list> </property> --> </bean> <!-- ####################spring与hibernate整合######################## --> <!-- 4,事务管理配置 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false"/> <tx:method name="get*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* service..*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
dao service 和action使用注解自动装配