S2SH整合步骤
1.导jar包
struts2 , hibernate , spring ,struts-spring-plugin.jar, commons-dbcp.jar,commons-pool.jar,jdbc驱动包
如果提示ClassNotFoundException可以将相关jar包再加入。
2.copy web.xml 配置 <context-param> <listener>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
3.copy applicationContext.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.bjsxt" >
</context:component-scan>
<!-- 与数据库mysql建立连接 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
<property name="url" value="jdbc:mysql://localhost:3306/test2"></property> //此处创建数据库
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">//使用注解
<property name="dataSource" ref="dataSource" ></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>//自动生成表
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.bjsxt.po</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
4.copy struts.xml
<?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>
<constant name="struts.objectFactory" value="spring" ></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="" class="studentAction" >
<result name="">路径</result>
</action>
</package>
</struts>
5.注意表是自动生成的,在PO类上加上注解 (注解导的包都是持久状态的) @Entity @Table(name="表名") @Id @GeneratedValue(strategy=GenerationType.IDENTITY) //id自动递增
6.注意Action类名上加 @Component @Scope("prototype") ,属性@Resource
7.service层 定义一个接口,一个实现类impl 实现 该接口,实现类调用Dao层
8.Dao层 继承BaseDaoImpl实现类 BaseDaoImpl 实现 BaseDao接口
浙公网安备 33010602011771号