标签:ssh整合使用struts-spring-pluing
一、创建整合的项目
1.项目名称:spring101503
2.在项目中创建conf目录
3.在项目中创建test目录
二,添加struts支持(struts版本2.3.7)
1.struts2 2.3.7.jar
1).使用核心jar文件
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
struts2-spring-plugin-2.3.7.jar
2).可能使用的插件说明
struts2-convention-plugin-2.3.7.jar:struts使用注解开发
struts2-json-plugin-2.3.7.jar:整合ajax
struts2-spring-plugin-2.3.7.jar:整合spring
2.添加配置文件
1).修改web.xml配置文件,添加filte配置
<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>
2).在conf目录下添加struts2的核心配置文件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"/>
<package name="default" namespace="/" extends="struts-default">
</package>
</struts>
3).测试struts支持是否可用
i.在test目录下创建测试用的Action
包名:cn.jbit.spring101503.test.action
Action名称:TestAction.java
Action内容:
public class TestAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("测试action");
return super.execute();
}
}
ii.在struts.xml文件中配置测试Action
<package name="default" namespace="/" extends="struts-default">
<!-- 测试struts是否可用 -->
<action name="testAction" class="cn.jbit.spring101503.test.action.TestAction">
<result>/WEB-INF/pages/test/test.jsp</result>
</action>
</package>
iii.在WEB-INF下创建pages目录
/WEB-INF/pages
iv.在pages目录下创建test目录
/WEB-INF/pages/test
v.在test目录下创建测试视图
/WEB-INF/pages/test/test.jsp
vi.访问
http://localhost:9527/spring101503进入index.jsp,在index.jsp中有
<a href="${pageContext.request.contextPath}/testAction.action">执行测试action</a>
3.添加spring支持(spring 3.2.jar)
1).基本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
2).依赖包
commons-logging.jar
log4j.jar
3).AOP开发jar
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
4).整合Hibernate
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
5).配置spring监听器
spring-web-3.2.0.RELEASE.jar
6).整合junit
spring-test-3.2.0.RELEASE.jar
7).添加spring核心配置文件applicationContext.jsp
<?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>
8).修改web.xml文件,添加监听配置
<!-- spring 监听配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring核心配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
9).测试spring支持
i.在test目录下创建spring测试类
包名:cn.jbit.spring101503.test.spring
类名:TestSpring.java
类内容:
public class TestSpring {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
ii.在applicationContext.xml文件中进行配置测试类
<!-- 配置测试spring的bean -->
<bean id="testspring" class="cn.jbit.spring101503.test.spring.TestSpring">
<property name="name" value="testspring成功"></property>
</bean>
iii.测试
@Test
public void testBean(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
TestSpring testSpring = (TestSpring) context.getBean("testspring");
System.out.println(testSpring.getName());
}
4.添加hibernate支持(Hibernate 3.6.jar)
1).基础jar
hibernate3.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
2).整合slf4j
slf4j-log4j12-1.6.1.jar
log4j.jar
3).使用c3p0数据源
c3p0-0.9.1.jar
4).数据库驱动
mysql-connector-java-5.1.10-bin.jar
5).二级缓存
backport-util-concurrent.jar
ehcache-1.5.0.jar
commons-logging.jar
6).hibernate核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/mysql
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 配置方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 输出sql -->
<property name="show_sql">true</property>
<!-- 格式化生成的sql -->
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置session与线程绑定 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 事务自动提交 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 数据连接提供 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 加载映射文件 -->
</session-factory>
</hibernate-configuration>
7).Hibernate工具类的设计
public class HibernateUtil {
private static Configuration configuration;
private static SessionFactory sessionFactory;
static{
configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
public static Session openSession(){
return sessionFactory.openSession();
}
public static Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
}
5.实体类设计
cn.jbit.spring8.domain
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Double price;
private String author;
//省略getter 和 setter
}
映射文件Book.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.jbit.spring8.domain">
<class name="Book" table="d_book">
<id name="id" type="integer">
<column name="id"></column>
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<property name="name" type="string">
<column name="name"></column>
</property>
<property name="price" type="double">
<column name="price"></column>
</property>
<property name="author" type="string">
<column name="author"></column>
</property>
</class>
</hibernate-mapping>
在hibernate.cfg.xml文件中进行配置
<mapping resource="cn/jbit/spring101503/domain/Book.hbm.xml"/>
6.持久层设计
接口
cn.jbit.spring101503.dao
/**
* 图书持久层接口
* @author Administrator
*
*/
public interface BookDao {
/**
* 添加图书信息
* @param book
*/
public void insert(Book book);
}
实现
cn.jbit.spring101503.dao.impl
public class BookDaoImpl implements BookDao {
public void insert(Book book) {
Session session = null;
Transaction transaction = null;
try {
//获取Session对象
session = HibernateUtil.getCurrentSession();
//开启事务
transaction = session.beginTransaction();
System.out.println("执行dao的save方法之前");
//调用保存方法
session.save(book);
System.out.println("执行dao的save方法之后");
//事务提交
transaction.commit();
} catch (HibernateException e) {
e.printStackTrace();
//事务回滚
transaction.rollback();
}
}
}
配置
修改applicationContext.xml,配置DAO
<!-- 1.配置Dao -->
<bean id="bookDao" class="cn.jbit.spring101503.dao.impl.BookDaoImpl">
</bean>
7.业务层设计
接口
cn.jbit.spring101503.service
/**
* 图书业务层
* @author Administrator
*
*/
public interface BookService {
/**
* 保存图书
* @param book
*/
public void save(Book book);
}
实现
cn.jbit.spring101503.service.impl
public class BookServiceImpl implements BookService {
private BookDao bookDao;
public void save(Book book) {
System.out.println("执行service层save方法");
//调用持久层添加方法
bookDao.insert(book);
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public BookDao getBookDao() {
return bookDao;
}
}
<!-- 2.配置Service -->
<bean id="bookService" class="cn.jbit.spring101503.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
8.Action设计
cn.jbit.spring101503.web.action
定义Action
public class BookAction extends ActionSupport implements ModelDriven<Book> {
private Book book = new Book();
public String add(){
System.out.println("执行add方法");
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
BookService bookService = (BookService) context.getBean("bookService");
bookService.save(book);
return "add";
}
public Book getModel() {
return book;
}
}
9.修改配置文件
1).修改applicationContext.xml文件,添加相应的配置,配置如下
<!-- 1.配置Dao -->
<bean id="bookDao" class="cn.jbit.spring101503.dao.impl.BookDaoImpl">
</bean>
<!-- 2.配置Service -->
<bean id="bookService" class="cn.jbit.spring101503.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
<!-- 3.配置Action -->
<bean id="bookAction" class="cn.jbit.spring101503.web.action.BookAction" scope="prototype">
<property name="bookService" ref="bookService"></property>
</bean>
2).修改struts.xml文件,添加相应的配置,配置如下
<package name="default" namespace="/" extends="struts-default">
<!-- 测试struts是否可用 -->
<action name="testAction" class="cn.jbit.spring101503.test.action.TestAction">
<result>/WEB-INF/pages/test/test.jsp</result>
</action>
<!-- 配置BookAction -->
<action name="bookAction_*" class="bookAction" method="{1}">
<result name="add">/WEB-INF/pages/book/list.jsp</result>
</action>
</package>
9.表示层设计
addBook.jsp
<h3>添加图书</h3>
<s:form action="bookAction_add" namespace="/" method="post" theme="simple">
名称:<s:textfield name="name"></s:textfield>
单价:<s:textfield name="price"></s:textfield>
作者:<s:textfield name="author"></s:textfield>
<s:submit type="submit" value="添加"></s:submit>
</s:form>
10.测试ssh
通过http://localhost:9527/spring101504/add.jsp
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1564319
标签:ssh整合使用struts-spring-pluing
原文地址:http://suyanzhu.blog.51cto.com/8050189/1564319