1、建立整合的項目
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
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
<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
<!-- 配置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;
}
}
<!-- 配置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.表示層設計
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/spring101503/add.jspjava