注意點:
如果需要依賴注入,及通過get或者set方法來進行jsp頁面數據到後臺,需要縮小事務的範圍。
<aop:pointcut expression="execution(* _SSH.AdminService.*(..))" id="pt"/>
理解:
struts去執行瀏覽器和服務器之間的交互;
spring完成對象的創建和對象的依賴關係以及事務的處理
hibernate完成對數據的訪問
疑難點:
1. 通過訪問瀏覽器,使spring創建action,不需要自己創建
2. 將c3p0設置在spring中提高性能。
3. xml文件reference file contains error錯誤使得tomcat啓動失敗
解決方案:Preferences >XML File> Validation > XML中"Honour all XML schema locations"前的對號去掉。它將禁用指向不同 schema位置相同命名空間引用的驗證,僅以第一次找到的可驗證的XML文件爲結果。
(來自:https://blog.csdn.net/u013087513/article/details/70991422)
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<!-- 配置spring的OpenSessionInView模式 【目的:JSp頁面訪問懶加載數據】 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 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>
<!-- 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>
</web-app>
公共bean.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: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">
<!-- 1) 連接池實例 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///day16"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="6"></property>
</bean>
<!-- 2) SessionFactory實例創建 -->
<!-- 所有的配置都由spring維護(項目中不需要hibernate.cfg.xml啦) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- a. 連接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- b. hibernate常用配置: 方言、顯示sql、自動建表等 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- c. 映射配置 -->
<property name="mappingLocations">
<list>
<value>classpath:SSH/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 3) 事務配置 -->
<!-- # 事務管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- # 事務增強 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- # AOP配置 -->
<aop:config>
<aop:pointcut expression="execution(* SSH.*.*(..))" id="pt"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
bean-dao.xml:
<bean id = "adminDao" class="SSH.AdminDao">
<property name="sf" ref="sessionFactory"></property>
</bean>
dao層代碼:
package SSH;
import org.hibernate.SessionFactory;
public class AdminDao {
private SessionFactory sf;
public void setSf(SessionFactory sf) {
this.sf = sf;
}
public Admin query(int id){
Admin admin = (Admin) sf.getCurrentSession().get(Admin.class, id);
return admin;
}
}
Action代碼:
package SSH;
import org.apache.struts2.ServletActionContext;
public class AdminAction {
private AdminService service;
public void setService(AdminService service) {
this.service = service;
}
public String query(){
int id = 2;
Admin admin = service.query(id);
ServletActionContext.getRequest().setAttribute("admin", admin);
return "success";
}
}