搜索
您的当前位置:首页正文

SSH简单整合

来源:步旅网

注意点:

  如果需要依赖注入,及通过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文件为结果。

    (来自:)

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";
   }
}

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Top