1.搭建项目:
项目名称:ss1
2.添加jar包
1).struts 2.3.7
--基础
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.7.jar
xwork-core-2.3.7.jar
--整合spring
struts2-spring-plugin-2.3.7.jar
--使用注解
struts2-convention-plugin-2.3.7.jar
2).spring 3.2
--AOP
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
--日志
commons-logging.jar
log4j.jar
--核心
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
--持久层
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
--整合junit4
spring-test-3.2.0.RELEASE.jar
--使用监听
spring-web-3.2.0.RELEASE.jar
3).hibernate 3.6.10
--基础
\lib\hibernate3.jar
\lib\required\*.jar
--整合log4j
slf4j-log4j12-1.6.1.jar
--数据库驱动
mysql-connector-java-5.1.10-bin.jar
--二级缓存
ehcache-1.5.0.jar
backport-util-concurrent.jar
3.添加配置文件
a.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.devMode" value="true"/>
<!-- 整合spring -->
<constant name="struts.objectFactory" value="spring"></constant>
</struts>
b.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
c.过滤器配置
修改web.xml文件添加struts核心过滤器
<!-- struts核心控制器配置 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
修改web.xml文件添加Session生命周期开发到web层
<filter>
<filter-name>openSessionFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
d.修改web.xml文件添加spring监听器
<!-- spring 监听配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
e.实体类的创建
@Entity
@Table(name ="d_book")
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private Double price;
private String author;
//省略getter and setter
}
f.在spring核心配置文件applicationContext.xml中添加Hibernate配置信息
<!-- 1.加载属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.自动描述注解 -->
<context:annotation-config/>
<context:component-scan base-package="cn.jbit"></context:component-scan>
<!-- 3.配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 4.配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate属性 -->
<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.current_session_context_class">thread</prop>
</props>
</property>
<!-- 扫描注解po类所在的包 -->
<property name="packagesToScan">
<list>
<value>cn.jbit.spring9.domain</value>
</list>
</property>
</bean>
<!-- 5.配置Hibernater模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 6.声明事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 7.事务驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
4.持久层
1).接口
public interface IBookDao {
public void insert(Book book);
public Book select(int id);
}
2).实现
@Repository
public class BookDaoImpl implements IBookDao {
@Autowired
private HibernateTemplate hibernateTemplate;
public void insert(Book book) {
System.out.println("dao save");
this.hibernateTemplate.save(book);
}
}
public Book select(int id) {
return this.hibernateTemplate.load(Book.class, id);
}
5.业务层
1).接口
public interface IBookService {
public void save(Book book);
public Book find(int id);
}
2).实现
@Service
@Transactional
public class BookServiceImpl implements IBookService {
@Autowired
private IBookDao bookDao;
public void save(Book book) {
bookDao.insert(book);
}
public Book find(int id) {
return bookDao.select(id);
}
}
6.控制器
1).创建BookAction类继承ActionSupport
@Namespace("/")
@Controller
public class BookAction extends ActionSupport implements ModelDriven<Book>{
private Book book = new Book();
//业务逻辑层
private IBookService bookService;
@Action(value="bookAction_add",results={@Result(name="add",location="/index.jsp")})
public String add(){
System.out.println("添加信息");
//bookService.save(book);
return "add";
}
public Book getModel() {
return book;
}
public void setBookService(IBookService bookService) {
this.bookService = bookService;
}
@Action(value="bookAction_load",results={@Result(name="load",location="/index.jsp")})
public String load(){
book = bookService.find(1);
System.out.println(book);
return "load";
}
}
7.表示层
以表单方式提交数据,此处省略!
使用注解实现ssh整合