SSH2集成实用实例步骤

SSH2集成
1、使用MyEclipse导入Struts2框架
在Struts2包中加入Spring集成插件:
struts2-spring-plugin-2.1.8.1.jar

准备让Spring框架管理Struts2。
在struts.xml中加入:


2、使用MyElipse导入Hibernate框架:
导入过程选择copy checked library jars to project folder and to build path

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://www.360docs.net/doc/e68849338.html,/hibernate-configuration-3.0.dtd">






org.hibernate.dialect.Oracle9Dialect


jdbc:oracle:thin:@localhost:1521:orcl10

scott
tiger

oracle.jdbc.driver.OracleDriver


mystudentdemo

true
true







使用Hibenrate Reverse Enginnering生成项目实体类及其映射文件。

3、使用MyEclipse导入Spring框架。
导入过程选择copy checked library jars to project folder and to build path
1)、 在web.xml中加入


contextConfigLocation
classpath:applicationContext.xml



org.springframework.web.context.ContextLoaderListener


2)、在三大框架包全部导入后,删除asm-2.2.3.jar包.
原因:这是因为Spring和Hibernate都用到
这个Asm框架进行字节码增强,然而
发布Web项目后,一些包的加载次序
发生了冲突!而asm-2.2.3中包含的
是老版本的类库,这些类库在Spring
中使用会出错.

4、制作数据层:
1)、DAO:
public class TicketDAO extends HibernateDaoSupport{
//要得到session,必须注入SessionFactory
//查询全部门票
public List getAllTickets()
{
List tickets = null;

HibernateTemplate template = this.getHibernateTemplate();

tickets = template.find("from TbTickets");
return tickets;
}
}

2)、将DA

O类交给Spring管理:
applicationContext.xml
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
value="classpath:hibernate.cfg.xml">












5、制作业务层TicketServiceImpl:
1)、将DAO类由Spring注入
public class TicketServiceImpl implements ITicketService {
private TicketDAO ticketDAO = null;

public TicketDAO getTicketDAO() {
return ticketDAO;
}

public void setTicketDAO(TicketDAO ticketDAO) {
this.ticketDAO = ticketDAO;
}

/* (non-Javadoc)
* @see https://www.360docs.net/doc/e68849338.html,.impl.ITicketService#getAllTickets()
*/
public List getAllTickets() {
// TODO Auto-generated method stub
return ticketDAO.getAllTickets();
}

}

2)、由业务类抽象业务接口 refactor-->extract interface
public interface ITicketService {

public abstract List getAllTickets();

}

3)、将业务类交给Spring管理 applicationContext.xml:







Transaction =
Session.beginTransaction

https://www.360docs.net/doc/e68849338.html,mit
4)、配置业务类执行添加/删除/修改时使用事务(自动提交commit):
applicationContext.xml

class="org.springframework.orm.hibernate3.
HibernateTransactionManager">






class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">






PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED












6、制作MVC表现层:
1)、控制器:Action:
public class TicketAction extends ActionSupport {
private ITicketService iTicketService = null;

public ITicketService getiTicketService() {
return iTicketService;
}

public void setiTicketService(ITicketService iTicketService) {
this.iTicketService = iTicketService;
}

public String list() thr

ows Exception
{
// TODO Auto-generated method stub
List tickets = iTicketService.getAllTickets();

ActionContext.getContext().put("ticketlist", tickets);

return "searchticket";
}
}

2)、配置struts.xml中的Action:


/TicketSearch.jsp



3) 、配置applicationContext.xml由Spring管理Struts2的Action:
注意:Struts2配置文件中Action的name属性必须和Spring配置文件bean的id属性一致!
并向Action注入业务层。
scope="prototype">





4)、页面:
首页:index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>




简易奥运门票销售管理系统



简易奥运门票销售管理系统






查询所有门票信息:
TicketSearch.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>




无标题页





门票详细信息






 












<

a href="TicketModify.htm" style="cursor:hand;">修改 删除

















相关主题
相关文档
最新文档