SSH整合(配置方式)

工程目录:

/SSH1/src/cn/itcast/domain/Book.java


package cn.itcast.domain;

/**
* 图书
*/
public class Book {
private Integer id; // 图书编号
private String name; // 图书名称
private Double price; // 图书价格

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

}

/SSH1/src/cn/itcast/domain/Book.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.itcast.domain.Book" table="book">
<id name="id">
<generator class="identity"></generator>
</id>
<property name="name"></property>
<property name="price"></property>
</class>
</hibernate-mapping>

/SSH1/src/cn/itcast/dao/BookDAO.java


package cn.itcast.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.itcast.domain.Book;

/**
* 数据层代码
*/
public class BookDAO extends HibernateDaoSupport {

public void save(Book book) {
System.out.println("添加图书 dao 执行...");
this.getHibernateTemplate().save(book);
}
}

/SSH1/src/cn/itcast/service/BookService.java


package cn.itcast.service;

import org.springframework.transaction.annotation.Transactional;
import cn.itcast.dao.BookDAO;
import cn.itcast.domain.Book;

/**
* 图书管理 业务层
*/
@Transactional
public class BookService {
private BookDAO bookDAO;

public void addBook(Book book) {
System.out.println("添加图书 service执行 ...");
bookDAO.save(book);
}

public void setBookDAO(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
}

/SSH1/src/cn/itcast/web/action/BookAction.java


package cn.itcast.web.action;

import cn.itcast.domain.Book;
import cn.itcast.service.BookService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
* 添加图书
*/
public class BookAction extends ActionSupport implements ModelDriven<Book> {
private static final long serialVersionUID = 1L;
private Book book = new Book();

public Book getModel() {
return book;
}

@Override
public String execute() throws Exception {
System.out.println("添加图书 action 执行... ");
bookService.addBook(book);
return NONE;
}

private BookService bookService;

public void setBookService(BookService bookService) {
this.bookService = bookService;
}

}

/SSH1/src/applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置Action -->
<bean id="bookAction" class="cn.itcast.web.action.BookAction"
scope="prototype">
<property name="bookService" ref="bookService"/>
</bean>

<!-- 配置Service -->
<bean id="bookService" class="cn.itcast.service.BookService">
<property name="bookDAO" ref="bookDAO"/>
</bean>

<!-- 配置DAO -->
<bean id="bookDAO" class="cn.itcast.dao.BookDAO">
<!-- 注入sessionFactory, 构造hibernateTemplate -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 整合hibernate 方式一 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

<!-- 事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>

/SSH1/src/hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- JDBC基本连接参数 -->
<session-factory> <!-- 理解为连接池 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 常见其它配置 -->
<property name="hibernate.show_sql">true</property> <!-- 控制台上打印SQL -->
<property name="hibernate.format_sql">true</property> <!-- 控制台输出时,对SQL语句格式化 -->
<!-- 测试环境 create/ create-drop 正式环境 update validate -->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 自动建表 -->

<property name="hibernate.connection.autocommit">true</property>

<!-- 在核心配置文件中 引用 mapping 映射文件 -->
<mapping resource="cn/itcast/domain/Book.hbm.xml"/>

</session-factory>
</hibernate-configuration>

/SSH1/src/log4j.properties


### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ###

log4j.rootLogger=info, stdout

/SSH1/src/struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">
<!-- 方式一,自动向Action 装配 Service -->
<!-- <action name="addbook" class="cn.itcast.web.action.BookAction"></action> -->
<!-- 方式二,将Action 交给Spring 管理 , class属性 编写伪类名 -->
<action name="addbook" class="bookAction"></action>
</package>
</struts>

/SSH1/WebRoot/WEB-INF/web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name></display-name>
<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>
<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>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.do,*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>

/SSH1/WebRoot/book.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>addBook</title>
</head>
<body>
<h1>添加图书</h1>
<s:form action="addbook" namespace="/" method="post" theme="simple">
图书名称 <s:textfield name="name"></s:textfield>
<br />
图书价格 <s:textfield name="price"></s:textfield>
<br />
<s:submit type="submit" value="提交"></s:submit>
</s:form>
</body>
</html>

/SSH1/WebRoot/success.jsp


<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>success</title>
</head>
<body>
<h1>ADDBOOK SUCCESS!</h1>
</body>
</html>

/SSH1/WebRoot/welcome.jsp


<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加图书</title>
</head>
<body>
<a href="/SSH1/addbook">显示</a>
<a href="book.jsp">book.jsp</a>
</body>
</html>

SSH整合(配置方式),布布扣,bubuko.com

时间: 2024-11-06 14:06:25

SSH整合(配置方式)的相关文章

SSH整合配置二级缓存

一.了解 Hibernate的session提供了一级缓存,每个session,对同一个id进行两次load,不会发送两条sql给数据库,但session关闭时,一级缓存失效. 二级缓存是SessionFactory级别的全局缓存,它底下可以使用不同的缓存类库,比如ehcache.oscache等 对缓存若想进步了解可参考以下网址http://www.360doc.com/content/10/0917/17/2560742_54412898.shtml 二.配置 1.在applicationC

JBPM4.4+SSH 整合配置及完整实例

整合jBPM4.4+ssh过程(spring接管struts2和hibernate,例中都整合在application.xml中,没有单独的jbpm.hibernate.cfg.xml): 1.在sessionFactory的mappingLocations属性加入以下几个jbpm.*.hbm.xml由jBPM自带 <value>classpath:jbpm.repository.hbm.xml</value> <value>classpath:jbpm.executi

java SSH整合配置

web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:

spring的使用-ssh整合

ssh整合-xml方式: 1.需要记住的三个jar包: spring-web-4.2.4.RELEASE.jar           ---保证项目启动时就实例化spring配置的对象(通过一个servletContext监听器ContextLoaderListener实现),保证整个项目只有一个工厂. struts2-spring-plugin-2.3.24.jar ---解决了struts2和spring的整合问题,将struts2中的action交给spring创建 spring-orm-

spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)

spring,springmvc,mybatis基本整合(一)–xml文件配置方式(2)之mapper接口 一,整合结构 二,所需jar包 如上图. 三,整合配置 1,web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://j

SSH整合中正确配置以及实例

mysql数据库表已经字段: 项目文件结构: 调试过程中要注意的事项: ①:applicationContext.xml文件要放在src目录下 ②:容易出错的地方如下代码中字体为红色处 常见错误请参看:http://blog.163.com/jxguo_05/blog/static/71940100201023185653156/   &  http://blog.sina.com.cn/s/blog_6757442e0100xumy.html & http://blog.csdn.net

SSH整合开发时Scope为默认时现象与原理

1.前提知识 1)scope默认值 进行SSH整合开发时,Struts2的action需要用spring容器进行管理,只要涉及到类以bean的形式入到spring容器中,不管是xml配置还是使用注解方式进行配置,都会涉及到spring管理bean的scope,其一共有五种取值,而其默认值为singleton,也就是单例模型,所有对此bean引用为同一个对象. 2)action应为多例 struts2作为MVC中视图(View)层框架,其最主要任务就是接收用户请求,然后调用业务逻辑层进行处理,这种

基于注解的SSH将配置精简到极致(1)

很早之前就想写一篇关于SSH整合的博客了,但是一直觉得使用SSH的时候那么多的配置文件,严重破坏了我们代码整体性,比如你要看两个实体的关系还得对照*.hbm.xml文件,要屡清一个Action可能需要对照applicationContext*.xml和struts*.xml文件.总之过多的配置文件坏破坏代码的整体性,会打乱代码的连续性,因为很多情况下你需要一边看Java代码,一边看xml的配置,采用注解就能很好的解决这个问题. 当然,即使采用注解的方式,也不能完全的丢掉配置文件,因为配置文件是程

ssh整合例子

今天给大家介绍一下最新版本的SSH(struts2.2.1+ hibernate3.6+spring3.0.5)组合.注意本讲解为手工搭建! 一.为SSH做好准备 struts2-2.2.1-all.zip       hibernate-distribution-3.6.0.Final-dist.zip spring-framework-3.0.5.RELEASE.zip spring-framework-2.5.6-with-dependencies.zip slf4j-1.6.1.zip