本项目采用Struts+Spring+Hibernate技术实现。
项目运行软件环境:
eclipse-SDK- 3.3.2 -win32+MyEclipse_6.0.1GA_E3.3.1_Installer+JDK1.6.0以上软件均可在官方网站上下载得到,
项目运行硬件环境:
首先我将介绍关于项目环境的搭建,这环境的搭建大家一定要注意,因为Hibernate和Spring在创建中经常会出现问题,我将截图给大家展示
我们现在Eclipse中Fileànewàweb project
首先我们想搭建Spring环境,为了以后不会产生莫名其妙的错误我们把spring中所有的.jar文件全部导入。
当我们把jar都导入到项目中的后,我们再对Hibernate进行添加。
在Hibernate里面也要把Spring的Persistence导入
接着倒入Struts框架这布不需要你做其他任何操作最多就是改改名,其他一切next即可。
我现在完成了项目框架的导入,下面我将把我项目会用到的包建好
我现在来介绍下每个包的用处:
comm..westsec.ch.realty.common:里面存放公用的类如一些抽象的类,或是bean等等,能够提供公用的文件
comm..westsec.ch.realty.dao : 里面存放相关接口
comm..westsec.ch.realty.dao.impl 里面存放接口的实现
comm..westsec.ch.realty.hibernate里面存放映射的数据库表对象和HibernateSessinFactory
comm..westsec.ch.realty.service 里面存放的也是接口而且和dao 里面的接口完全一样
comm..westsec.ch.realty.service.impl 里面存放的是service接口的实现,为什么这里要要两个接口呢?目的是在为以后做多表操作的时候能够更方便,如:
public String insertTrans(Demo1 demo1, Demo2 demo2) { // TODO 自动生成方法存根 String tmpId1 = demo1DAO.insertDemo1(demo1); String tmpId2 = demo2DAO.insertDemo2(demo2);
return tmpId1+tmpId2; } |
这不难理解吧!呵呵
comm..westsec.ch.realty.struts里面存放Struts文字转化文件(支持国际化),或一些用于转换的小工具,
comm..westsec.ch.realty.struts.action.comm里面存放公用的action 方法,如分页或到底实现什么操作的判断等
comm..westsec.ch.realty.struts.action 里面存放每个功能的具体操作
comm..westsec.ch.realty.struts.form.comm里面存放公用的form方法
comm..westsec.ch.realty.struts.form 里面存放每个功能的具体操作
comm..westsec.ch.realty.until 工具包
src里面的东西基本上就这些
webroot下面的文件:
css: 存放样式
jsp: 存放全部的JSP文件
images 存放相关图片
js: 存放全部的*.js文件
inc: 存放全部的导入文件
下面我将介绍下起项目的基本框架
public
class
HouseForm
extends
BaseForm {
private
List
houses
=
new
ArrayList();
private
House
house
=
new
House();
public
List getHouses() {
return
houses
;
}
public
void
setHouses(List houses) {
this
.
houses
= houses;
}
public
House getHouse() {
return
house
;
}
public
void
setHouse(House house) {
this
.
house
= house;
}
}
|
public
abstract
class
BaseForm
extends
ValidatorForm {
/**
*
特别指定行为
*/
private
String
action
;
protected
Log
log
= LogFactory.getLog(BaseForm.
class
);
public
String getAction() {
return
action
;
}
public
void
setAction(String action) {
this
.
action
= action;
}
}
|
public
final
ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
this
.
mapping
= mapping;
this
.
form
= form;
this
.
request
= request;
this
.
response
= response;
messages
.clear();
boolean
success =
false
;
forward
= getDefaultForward();
try
{
executeCore();
//
执行子类中的此方法
System.
out
.println(
"**********ChAction***********"
);
success =
true
;
}
catch
(Throwable e) {
if
(
debug
) {
log
.error(
"CH Action
出错:
"
, e);
}
else
{
log
.error(
"CH Action
出错:
/r/n"
+ e);
}
if
(
operAction
!=
null
&&
operContent
!=
null
) {
String exMessage =
null
;
if
(e.getMessage() ==
null
) {
exMessage = e.toString();
}
else
{
exMessage = e.getMessage();
}
StringBuffer buffer =
new
StringBuffer(exMessage);
buffer.append(
"/r/n"
).append(
"------------------------/r/n"
).append(
operContent
);
operContent
= buffer;
}
//addMessages(e);
}
finally
{
//log(success);
}
if
(!
messages
.isEmpty()) {
saveErrors(request,
messages
);
}
return
forward
;
}
|
public
void
executeCore()
throws
Throwable {
//
TODO
这个是自动生成的方法
//
时间是
: Apr 1, 2008 -- 2:58:08 PM
//
修改人
:杜振宇
HouseForm houseForm = (HouseForm)
form
;
if
(
request
.getParameter(
"add"
) !=
null
) {
log
.info(
"
添加
Demo1..."
);
add(houseForm);
forward
=
mapping
.findForward(IConstants.
FORWARD_EDIT
);
}
else
if
(
request
.getParameter(
"edit"
) !=
null
) {
log
.info(
"
编辑
Demo1..."
);
edit(houseForm);
forward
=
mapping
.findForward(IConstants.
FORWARD_EDIT
);
}
else
if
(
request
.getParameter(
"save"
) !=
null
) {
log
.info(
"
保存分公司
..."
);
forward
=
mapping
.findForward(IConstants.
FORWARD_EDIT
);
save(houseForm);
}
else
if
(
request
.getParameter(
"delete"
) !=
null
) {
log
.info(
"
删除分公司
..."
);
delete(houseForm);
query(houseForm);
}
else
{
log
.info(
"
分公司列表
..."
);
query(houseForm);
}
}
|
private
void
query(HouseForm houseForm)
throws
Exception {
PageSetBean pageBean=
new
PageSetBean();
DatabaseTable dt =
new
DatabaseTable();
//
显示从第几条开始取数据第一页是从那开始
int
firstPage =
this
.firstPage(
request
) ;
pageBean.setItems_per_page(pageBean.getItems_per_page());
pageBean.setCurrent_page(firstPage);
try
{
List houses =
null
;
setFactoryService();
//
由于这里用了
Spring
管理事务所以就交给它去处理
System.
out
.println(
"**********HouseAction.java-->query***********"
);
PuppisResult pr =
factoryService
.getPaged(dt.
house
.getClass().getSimpleName(), pageBean);
if
(pr!=
null
){
request
.setAttribute(
"pagenum"
,firstPage);
request
.setAttribute(
"pr"
, pr);
}
houseForm.setHouses(houses);
}
catch
(Exception ex){
ex.printStackTrace();
}
}
|
public
void
setFactoryService() {
Object serviceObj = ContextUtil.
getContext
()
.getBean(
"factoryService"
);
if
(serviceObj!=
null
){
factoryService
=(FactoryService)serviceObj;
}
this
.
factoryService
=
factoryService
;
}
|
private
static
String[]
SPRING_CONFIGS
= {
"applicationContext-dataSource.xml"
,
"applicationContext-dao.xml"
,
"applicationContext-service.xml"
,
"applicationContext-action.xml"
};
public
static
ApplicationContext getContext() {
if
(
context
==
null
) {
context
=
new
ClassPathXmlApplicationContext(
SPRING_CONFIGS
);
}
return
context
;
}
|
<
beans
default-lazy-init
=
"true"
>
<
bean
id
=
"factoryService"
parent
=
"txProxyTemplate"
>
<
property
name
=
"target"
>
<
bean
class
=
"com.westsec.test.service.impl.FactoryServiceImpl"
singleton
=
"false"
>
<
property
name
=
"factoryDAO"
ref
=
"factoryDAO"
/>
</
bean
>
</
property
>
</
bean
>
</
beans
>
|
<
beans
default-lazy-init
=
"true"
>
<
bean
id
=
"factoryDAO"
class
=
"com.westsec.test.dao.impl.FactoryDAOImpl"
>
<
property
name
=
"sessionFactory"
>
<
ref
bean
=
"sessionFactory"
/>
</
property
>
</
bean
>
</
beans
>
|
<
beans
>
<!--
配置数据源
-->
<
bean
id
=
"sessionFactory"
class
=
"org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
=
"configLocation"
>
<
value
>
classpath:hibernate.cfg.xml
</
value
>
</
property
>
</
bean
>
<!--
配置事务管理
-->
<
bean
id
=
"transactionManager"
class
=
"org.springframework.orm.hibernate3.HibernateTransactionManager"
>
<
property
name
=
"sessionFactory"
>
<
ref
bean
=
"sessionFactory"
/>
</
property
>
</
bean
>
<!--
定义事务代理模板
-->
<
bean
id
=
"txProxyTemplate"
abstract
=
"true"
class
=
"org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
<
property
name
=
"transactionManager"
>
<
ref
bean
=
"transactionManager"
/>
</
property
>
<
property
name
=
"transactionAttributes"
>
<
props
>
<
prop
key
=
"insert*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
=
"update*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
=
"delete*"
>
PROPAGATION_REQUIRED
</
prop
>
<
prop
key
=
"get*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
<
prop
key
=
"*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
</
props
>
</
property
>
</
bean
>
|