struts2.0+spring2.0+hibernate3.1多层模型整合完整过程 常见错误(转)
适合入门级看,但其中的错误解决适合大家参考。
在此使用MyEclipse6.0作为编程环境,真正领悟ssh后,就会知道MyEclipse添加spring与hibernate支持的用意只是让MyEclipse自动添加spring与hibernate的包,还有产生一些配置文件代码,在此为节省时间,使用MyEclipse来添加,但是其实自己添加也行。
要填的东西将会指出,其他都是默认
?1/*
?2SQLyog?Enterprise?-?MySQL?GUI?v6.06
?3Host?-?5.0.27-community-nt?:?Database?-?ssh
?4*********************************************************************
?5Server?version?:?5.0.27-community-nt
?6*/
?7;
?8
?9;
10
11createdatabaseifnotexists?`ssh`;
12
13USE?`ssh`;
14
15/*!40014?SET?@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,?FOREIGN_KEY_CHECKS=0?*/;
16/*!40101?SET?@OLD_SQL_MODE=@@SQL_MODE,?SQL_MODE='NO_AUTO_VALUE_ON_ZERO'?*/;
17
18/*Table?structure?for?table?`user`?*/
19
20DROPTABLEIFEXISTS?`user`;
21
22CREATETABLE?`user`?(
23int(11)?NOTNULL?auto_increment,
24varchar(20)?collate?utf8_unicode_ci?NOTNULL,
25varchar(20)?collate?utf8_unicode_ci?NOTNULL,
26varchar(20)?collate?utf8_unicode_ci?NOTNULL,
27PRIMARYKEY??(`id`),
28UNIQUEKEY?`name`?(`name`)
29=InnoDB?DEFAULT?CHARSET=utf8?COLLATE=utf8_unicode_ci;
30
31/*Data?for?the?table?`user`?*/
32
33insertinto?`user`(`id`,`name`,`password`,`purview`)?values?(1,'admin','admin','MANAGER');
34
35/*!40101?SET?SQL_MODE=@OLD_SQL_MODE?*/;
36/*!40014?SET?FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS?*/;
37
使用上面SQL语句在MySQL中建立表格,最好使用SQL yog Enterprise工具
安装MySQL与SQL yogEnterprise请自己掌握。
file-->web project(习惯使用,貌似dynamic web project也行)
?project name:ssh
J2EE Specification Level选择Java EE5.0(使用新一点的吧)
finish->
菜单Wiindows-->Open Perspective-->MyEclipse Java Enterprise 视图
工程名上右键-->MyEclipse-->Add Spring Capabilities
a).在框内选上:
Spring2.0 AOP Libraries(对于面向切面有用,后面会用到配置面向切面的)
Spring2.0 Core Libraries(Spring核心库)
Spring2.0 Persistence Core Libraries(持久层库,要Spring与Hibernate集成,当然就添加啦)
Spring2.0 J2EE Library
Spring2.0 Web Libraries
暂时就只添加这5个
Copy checked Library contents to project folder 选上,这样就可以将库添加到工程内,否则这个工程编译的时候会引用本机的文件系统内的库,工程转移到其它机器就需要重新将每个包改对路径,当然如果只需要本机开发的就不用copy啦。
next->
b).new applicationContext.xml 将路径改为在WebRoot/WEB-INF下面,其实放在src下也行,但是工程编译的时候将src下的东西编译到WebRoot/WEB-INF下的classes目录下,配置applicationContext路径的时候就相应修改就行。
finish->
菜单Wiindows-->Open Perspective-->MyEclipse Database Explorer
DB Browser中右键-->new,新建一个数据库连接,打开一个对话框
Driver template 选择MySQL connector/J(可以不选,选着只是为了下面显示出连接字符串的模板,让不熟悉的人可以照这模式来填)
Driver name:任意,用于标识这个连接,对我们工程没有任何关系的。
Connection URL:jdbc:mysql://localhost:3306/ssh (意思是本地地址IP的数据库ssh,如果是远程数据库,请写远程地址,当然要确保远程地址是可达的,例如防火墙阻隔等)
User name:你的数据库登录名
password:登录名密码
Add Jars-->打开对话框寻找本地文件系统的MySQL连接驱动包,(提供下载,解压取里面那个jar)
save password 打上钩,方便点
可以test 测试一下是否能够连接
这步骤的实际意义是在MyEclipse IDE工具上建立一个数据库连接,来管理,对于我们工程其实没有一点影响的,只是这个连接,以后引用或者管理的时候就较方便。
finish-->
切换回原来的视图,其实有多种切换视图的方法,快捷键等,自己学吧。
a).工程名上右键-->MyEclipse-->add Hibernate Capabilities
同样Copy checked Library Jars to project folder and add to build-path
next-->
b).选择Spring?configuration file
next->
c).因为原来已经有一个spring配置文件,所以就选择Existing Spring configuration file
sessionFactory ID:sessionFactory(可以自定义名称,使用惯例的吧,随便啦)
next-->
d).DB Driver??选择在MyEclipse DB Explorer中建立的数据库连接名称(这里就体现到同一个IDE工具的好处,方便地引用那个连接的配置)
Bean ID:dataSource(又是惯例的名称)
next-->
取消掉create SessionFactory class
finish-->
由于MyEclipse没有集成Struts2.0的包,所以自己添加Struts的五个包
由于空间有限,可以上网搜索一个叫struts2-blank的工程包,war文件也可以,可以用rar打开,取其里面lib下的这五个包
struts暂且只添加这5个包,多包反而会导致冲突(后面有说),至于struts想集成meshsite等的话,就另外加包吧。
顺便将原来的那个mysqlConnector的jar包也拿来吧,一起拖到WEB-INF/lib目录下,可以直接拖到IDE工具上的工程浏览框(即Package视图)的lib图标上,MyEclipse支持拖放功能。
还有commons-pool-1.4.jar和commons-dbcp-1.2.2.jar和struts2-spring-plugin-2.0.6.jar也是必须自己加进lib中的
如果有包重复的话,覆盖就行。
?
?1<?xml?version="1.0"?encoding="UTF-8"?>
?2<web-app?version="2.5"?xmlns="http://java.sun.com/xml/ns/javaee"
?3="http://www.w3.org/2001/XMLSchema-instance"
?4="http://java.sun.com/xml/ns/javaee?
?5>
?6<!--?网站默认首页?-->
?7<welcome-file-list>
?8<welcome-file>index.jsp</welcome-file>
?9</welcome-file-list>
10<!--?spring环境配置?-->
11<context-param>
12<param-name>contextConfigLocation</param-name>
13<param-value>/WEB-INF/applicationContext*.xml</param-value>
14</context-param>
15<listener>
16<listener-class>
17????????????org.springframework.web.context.ContextLoaderListener
18</listener-class>
19</listener>
20<!--?这个过滤器负责管理hibernate的数据库访问session,session的关闭将在action完毕的时候关闭,这样既不迟也不早
21-->
22<filter>
23<filter-name>lazyLoadingFilter</filter-name>
24<filter-class>
25????????????org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
26</filter-class>
27</filter>
28<filter-mapping>
29<filter-name>lazyLoadingFilter</filter-name>
30<url-pattern>*.action</url-pattern>
31</filter-mapping>
32<!--?struts2过滤器,对任何请求都添加这个过滤器?-->
33<filter>
34<filter-name>struts2</filter-name>
35<filter-class>
36????????????org.apache.struts2.dispatcher.FilterDispatcher
37</filter-class>
38</filter>
39<filter-mapping>
40<filter-name>struts2</filter-name>
41<url-pattern>/*</url-pattern>
42</filter-mapping>
43</web-app>
44
struts的过滤器配置是有顺序的:
按照Struts2的API,filter的顺序是
struts-cleanup filter
SiteMesh filter
FilterDispatcher
如果需要配置字符转换过滤器的话,那就安排在FilterDispatcher之前
spring配置文件可以多个,从/WEB-INF/applicationContext*.xml的通配符就可以看出来了
在src下建立一个struts.xml的文件:
?1<!DOCTYPE?struts?PUBLIC?"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"?"http://struts.apache.org/dtds/struts-2.0.dtd"?>
?2<struts>
?3<!--?类似struts1中的spring代理管理struts,这里的constant也是将struts交给spring管理?-->
?4<constant?name="struts.objectFactory"?value="spring"/>
?5?6?7<!--?默认的struts-default.xml文件,一定要写的。如果希望分开多个xml文件来配置action,可以再include几个struts的xml文件
?8例如:自己写的struts-login.xml等,经典的就是这个文件不写package,在其他分文件中归类action?package,在将个模块的xml包含进来?-->
?9<include?file="struts-default.xml"/>
1011
12<!--?真正开始编写action,使用package括住,下面前两个action是示例样板
13141516-->
17
18<!--?package1里面的action调用方法:
191.在根目录的jsp页面:form方式:<form?action="hello">?href方式:<a?href="网站根目录/hello.action></a
202.在/folder里面的jsp调用这个action:form方式:<form?action="hello"?namespace="/">
2122-->
23<!--<package?name="package1"?extends="struts-default">
24????????<action?name="hello"?width="469" src="/img/2012/09/23/18274211462.gif" height="376">
->finish:这样会产生三个文件,数据库user映射的pojo实体类对象User.java,数据库访问对象UserDAO.java,数据库映射文件User.hbm.xml
将User.java 与 hbm文件移到pojo包下
上面搞错了点,将UserDAO移到dao.hibernate包下吧
这里的东西涉及到applicationContext.xml的改变,没有关系,前面已经详尽讲解了各xml文件的意义。
UserDAO已经自动生成很多数据库访问函数,从字面上可以看出是什么意思,大部分函数是不一定用到的,自己想使用的方法写在下面说的接口上,再实现他吧。
我们为其写个UserDAO接口,在dao包下编写一个IUserDAO.java的接口,并且使UserDAO实现这个接口
10.编写服务
参照代码吧!

(略)
13.常见错误
?
appllicationConotext.xml中的org.apache.commons.dbcp.BasicDataSource not found错误,可能是没有添加commons-pool.jar和commons-dbcp.jar和commons-collections.jar
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: entity class not found: ssh.dao.User
因为自己在移动产生的User.java类的时候,忘记修改User.hbm.xml文件的映射实体类,需要改成
?<class table="user" catalog="ssh">
Action class [loginAction] not found - action - file:/D:/workspace2/.metadata/.plugins/com.genuitec.eclipse.easie.tomcat.myeclipse/tomcat/webapps/ssh/WEB-INF/classes/struts.xml
因为login action的class我指定为loginAction,所以在spring中的login Bean的id="loginAction"来对应,但是我却写成了login,所以找不到。
org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: model near line 1, column 56 [from ssh.pojo.User as model where model.name= adminand model.password= admin]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: model near line 1, column 56 [from ssh.pojo.User as model where model.name= adminand model.password= admin]
可以从from字符串中看出,我忘记写空格了。
javax.servlet.ServletException: org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Cannot create PoolableConnectionFactory (Unknown database 'ssh1'); nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Unknown database 'ssh1')
因为spring中的dataSource的??<property value="jdbc:mysql://localhost:3306/ssh"></property>,我原来指定了ssh1的数据库,我并没有创建这个数据,所以改回ssh
javax.servlet.ServletException: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
这个是因为我忘记把sql字符串中的值用''括起来,例如:
from ssh.pojo.User as model where model.name = admin and model.password = admin
应该要变成:
from ssh.pojo.User as model where model.name = 'admin' and model.password = 'admin'
自己改UserDAO中的字符串吧。
No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath:....
如果有这个问题的话,那就参看:http://www.blogjava.net/jianyue/archive/2008/08/28/225415.html
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
java.lang.NoClassDefFoundError: org/objectweb/asm/commons/EmptyVisitor
java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit
CGLIB Enhancement failed
主要是hibernate与spring包冲突问题,可能有多种情况,都参照一下吧,但是最关键的原则是一个同样功能的包,
不要将多个版本都放进来。根据这个原则,自己也能够知道应该删去什么包。
解决方案:参看:http://www.blogjava.net/jianyue/archive/2008/08/28/225246.html
No configuration found for the specified action:? in namespace:
那么一定是action配置得不对,不是包的问题,自己详细参照一下我的那份action配置说明。
代码工程
由于空间有限,去掉了包。包的列表如下: