图解MyEclipse配置struts+hibernate+spring+FreeMarker
2008-07-03 14:54
465 查看
这几天把hibernate和spring好好看了下,再想想struts的一些东西,决定把3个整合一下一起用,表现层用struts+freemarker,业务层用spring,hibernate处理持久层。在struts中,利用委托,将action的处理委托给spring进行,struts只负责页面逻辑。 前些日子下了Eclipse 3.2+MyEclipse5.0M2,感觉MyEclipse一些东西还是不错的,就用它来做个整合。 首先,建立一个web project:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/01.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/02.jpg)
然后,给新建立的项目附加上struts的一些文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/03.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/04.jpg)
完成后项目结构如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/05.jpg)
接着加入hibernate需要的文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/06.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/07.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/08.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/09.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/10.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/11.jpg)
完成后项目结构为:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/12.jpg)
再加入spring的文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/13.jpg)
注意下面的选择:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/14.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/15.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/16.jpg)
完成后可以看到:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/17.jpg)
打开那个applicationContext.xml,将
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/18.jpg)
改为:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/19.jpg)
现在还需要做一些修改,才能在运行的时候不报错,我以前也写过struts+hibernate的时候,会抛一个错,之所以是由于hibernate包含的那个xerces.jar,所以现在直接把它给删掉,没什么问题。
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/20.jpg)
在spring和hibernate那,有2个log4j.jar,老版本不好用,所以删除,换一个新版本。 删除
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/21.jpg)
和
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/22.jpg)
,换上
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/23.jpg)
,然后,在src下加入一个log4j.properties:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/24.jpg)
内容如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/25.jpg)
这样log4j就配好了。 接下来是加入FreeMarker,jsp比不上,个人感觉。 先下一个freemarker.jar
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/26.jpg)
,加到WEB-INF下的lib目录,然后,打开web.xml,加入如下内容:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/27.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/28.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/29.jpg)
下面来点实战演习。 切换到MyEclipse的hibernate视图。
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/30.jpg)
打开数据库连接:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/31.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/32.jpg)
然后,执行
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/33.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/34.jpg)
自动生成几个文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/35.jpg)
并且,spring的applicationContext.xml自动加入
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/36.jpg)
然后,建立ftl页面,根据前面web.xml配置的freemarker设置,需要建立如下目录
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/37.jpg)
先把页面逻辑处理下,打开struts-config.xml,然后进行如下操作
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/38.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/39.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/40.jpg)
继续加
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/41.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/42.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/43.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/44.jpg)
完成后效果如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/45.jpg)
下面是3个页面:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/46.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/47.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/48.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/49.jpg)
这里注意下前2行,在freemarker中用struts标签。 前面说过action委托给spring做,许要在struts那加入
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/50.jpg)
然后,action类需要修改下
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/51.jpg)
下面的setter是为spring准备的。 struts配置那的action声明需要修改一下,说明用代理:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/59.jpg)
然后在spring的applicationContext.xml加上:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/52.jpg)
注意是用name,不是id。 发布,运行,结果抛了个错
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/53.jpg)
还好,问题不大,找到
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/54.jpg)
加到WEB-INF/lib下就可以了。 开始测试
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/55.jpg)
成功的话:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/56.jpg)
否则:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/57.jpg)
然后去数据库一看,郁闷,数据没进来,试了N久,最后发现spring里要定义个dataSource。原来sessionFactory的定义变为如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/58.jpg)
ok,一切正常。到这里,就写完了,只是为说明怎样用MyEclipse进行快速开发,毕竟平时做项目要的是进度,还要监管质量,有了MyEclipse,确实方便不少。
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/01.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/02.jpg)
然后,给新建立的项目附加上struts的一些文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/03.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/04.jpg)
完成后项目结构如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/05.jpg)
接着加入hibernate需要的文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/06.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/07.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/08.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/09.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/10.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/11.jpg)
完成后项目结构为:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/12.jpg)
再加入spring的文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/13.jpg)
注意下面的选择:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/14.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/15.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/16.jpg)
完成后可以看到:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/17.jpg)
打开那个applicationContext.xml,将
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/18.jpg)
改为:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/19.jpg)
现在还需要做一些修改,才能在运行的时候不报错,我以前也写过struts+hibernate的时候,会抛一个错,之所以是由于hibernate包含的那个xerces.jar,所以现在直接把它给删掉,没什么问题。
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/20.jpg)
在spring和hibernate那,有2个log4j.jar,老版本不好用,所以删除,换一个新版本。 删除
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/21.jpg)
和
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/22.jpg)
,换上
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/23.jpg)
,然后,在src下加入一个log4j.properties:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/24.jpg)
内容如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/25.jpg)
这样log4j就配好了。 接下来是加入FreeMarker,jsp比不上,个人感觉。 先下一个freemarker.jar
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/26.jpg)
,加到WEB-INF下的lib目录,然后,打开web.xml,加入如下内容:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/27.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/28.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/29.jpg)
下面来点实战演习。 切换到MyEclipse的hibernate视图。
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/30.jpg)
打开数据库连接:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/31.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/32.jpg)
然后,执行
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/33.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/34.jpg)
自动生成几个文件:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/35.jpg)
并且,spring的applicationContext.xml自动加入
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/36.jpg)
然后,建立ftl页面,根据前面web.xml配置的freemarker设置,需要建立如下目录
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/37.jpg)
先把页面逻辑处理下,打开struts-config.xml,然后进行如下操作
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/38.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/39.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/40.jpg)
继续加
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/41.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/42.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/43.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/44.jpg)
完成后效果如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/45.jpg)
下面是3个页面:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/46.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/47.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/48.jpg)
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/49.jpg)
这里注意下前2行,在freemarker中用struts标签。 前面说过action委托给spring做,许要在struts那加入
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/50.jpg)
然后,action类需要修改下
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/51.jpg)
下面的setter是为spring准备的。 struts配置那的action声明需要修改一下,说明用代理:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/59.jpg)
然后在spring的applicationContext.xml加上:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/52.jpg)
注意是用name,不是id。 发布,运行,结果抛了个错
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/53.jpg)
还好,问题不大,找到
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/54.jpg)
加到WEB-INF/lib下就可以了。 开始测试
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/55.jpg)
成功的话:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/56.jpg)
否则:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/57.jpg)
然后去数据库一看,郁闷,数据没进来,试了N久,最后发现spring里要定义个dataSource。原来sessionFactory的定义变为如下:
![](http://www.host01.com/UpLoadFiles/UpPic/2006-9/1/58.jpg)
ok,一切正常。到这里,就写完了,只是为说明怎样用MyEclipse进行快速开发,毕竟平时做项目要的是进度,还要监管质量,有了MyEclipse,确实方便不少。
相关文章推荐
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker【原创】
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker http://www.cnblogs.com/zxub/archive/2006/08/03/466682.html
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker
- 图解MyEclipse配置struts+hibernate+spring+FreeMarker【原创】
- 图解MyEclipse配置struts+hibernate+spring
- 图解MyEclipse配置struts+hibernate+spring
- 图解MyEclipse配置struts+hibernate+spring
- 图解MyEclipse配置struts+hibernate+spring
- MyEclipse配置struts+hibernate+spring
- JAVA SSH框架的配置(myeclipse(9)+tomcat(6.0.35)+struts(2.2.3)+Spring(3.0)+Hibernate(3.0))