2008年12月30日于办公室内完成 作者:黄颢鹏
参考文献(无)
作者联系QQ:25950329 联系Email:anh3000@qq.com
注:转载前请注明作者名和出处,如违反该款作者保留追究权利
细说如何整合spring+hibernate +jsf
原创作者:黄颢鹏
(注:转载前请注明作者名和出处,如违反该款作者保留追究权利
作者QQ:25950329、 Email:anh3000@qq.com)
摘 要 本文从java web开发中的web.xml配置进行详细讲解,并结合实际代码讲述了如何整合spring+hibernate+jsf
关键词 spring hibernate jsf 整合
我平时习惯使用MyEclipse 6.5 Blue开发JAVA EE的Web应用,因此在下面讲述的代码和文件配置都是采用MyEclipse 6.5 Blue撰写的。其中本文所用到的类包版本是jsf1.2、spring2.0、hibernate3.1。
1、 整合前的准备
1.1、新建项目
启动MyEclipse 6.5 Blue,选择菜单项目上的File->Web Project,在打开的对话框中选择J2EE Specification Level中的JAVA EE5.0,新建立一个Web开发项目名为Test。本文所用到的类包全部都放在项目的WebRoot->WEB-INF->lib下。
如果你整和后发现出现以下错误异常:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Line 4 in XML document from ServletContext resource [/WEB-INF/spring-hibernate-beans.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
导致这样的异常多数是spring应用缺少了必要的类包,以下是必需包:
Spring包(9个):
commons-dbcp.jar、commons-pool.jar、spring.jar、spring-beans.jar、
spring-context.jar、spring-core.jar、spring-dao.jar、
spring-hibernate3.jar、spring-web.jar。
至于其它的JSF和Hibernate的类包比较简单,这里就不多说了。
1.2、配置JSF应用,实现JSF第一个的Hello World程序
1.2.1、基本的JSF配置
打开目录下WebRoot->WEB-INF的web.xml,可以看到如下xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
其中<welcome-file>index.jsp</welcome-file>是网站欢迎的首页,这里不做改动,然后把以下代码放在<web-app></web-app>之间,至此整个项目的JSF配置完成了,
<!--jsf的基本配置-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
现在,在MyEclipse 6.5 Blue中选中项目Test右击弹出彩单中选择New->JSP(Advanced Templates),新建立一个JSF文档,在新建文件对话框中的Template to use中选择Default JSF template,文件名默认为MyJsp.jsp,启动tomcat,打开游览器,输入地址http://127.0.0.1:8080/Test/MyJsp.faces,如看到This is my JSF JSP page.字样就说明JSF框架基本配置成功,当然这只是JSF 配置成功的第一步。
1.2.2、JSF中的托管Bean配置
JSF中的托管Bean都是写在faces-config.xml中的,下面我们新建立第一个jsf的托管bean。 在项目的src文件夹中建立包com.test.jsf(注:该包是专门放jsf使用的类),在该包中新建一个类名为T1`,代码如下
package com.test.jsf;
public class T1 {
private String msg;
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
this.msg="Hello,World";
return this.msg;
}
}
在项目目录WebRoot->WEB-INF中新建faces-config.xml文件,把以上代码写入faces-config.xml文件,
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<!-- managed-bean就是托管Bean了 -->
<managed-bean>
<managed-bean-name>t1</managed-bean-name>
<!-- com.test.jsf.T1就是上面新建立的类 -->
<managed-bean-class>com.test.jsf.T1</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
至于<managed-bean-scope>request</managed-bean-scope>中的request参数,还有session参数等等的选择,根据我的理解和jsp中的useBean中的参数类型的含义是差不多的,但这只是我个人理解,具体的含义请参看有关jsf的书籍。
然后我们改写前面的MyJsp.jsp文件,具体代码如下:
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSF 'MyJsp.jsp' starting page</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText escape="false" rendered="true" value="#{t1.msg}"/>
</h:form>
</f:view>
</body>
</html>
启动tomcat,打开游览器,输入地址http://127.0.0.1:8080/Test/MyJsp.faces,如看到Hello,World错误!未找到引用源。字样就说明JSF托管Bean配置成功了。
2、整和JSF和Spring
2.1、配置spring的配置文件路径和文件名
接着把以下xml代码拷贝到web.xml中,意思是,告诉整个项目spring的配置文件是在项目的WebRoot/WEB-INF/all-config.xml
<!--明确spring的配置文件all-config.xml的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/all-config.xml</param-value>
</context-param>
2.2、开始整和JSF和Spring
2.2.1、JSF和Spring整和的具体配置
从配置代码中可以知道spring配置文件名为all-config.xml,而我们还没有建立这个文件,在建立这个文件之前,我们先说说jsf的托管Bean,大家可以从第一节可以学到jsf的托管Bean也是采用依赖注入的,但是spring的依赖注入功能要强大的多,所以我们将把jsf的托管Bean全部依靠spring的注入来实现。在实现之前,我们得先把以下xml代码:
<!-- 把jsf和spring结合起来 -->
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
拷贝到faces-config.xml文件中的<faces-config></faces-config>中间,并把上面的 <!-- managed-bean就是托管Bean了 -->
<managed-bean>
<managed-bean-name>t1</managed-bean-name>
<!-- com.test.jsf.T1就是上面新建立的类 -->
<managed-bean-class>com.test.jsf.T1</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
删除掉,具体xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<!-- 把jsf和spring结合起来 -->
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
</faces-config>
该段代码就是要把jsf的托管Bean移植到spring的配置上。
接着还要把以下代码:
<!--spring整合jsf的web.xml配置-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
拷贝到web.xml的<web-app> </web-app>中间。整个web.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--spring整合jsf的web.xml配置-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--明确spring的配置文件all-config.xml的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/all-config.xml</param-value>
</context-param>
<!--jsf的基本配置-->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
2.2.2、编写整和后的具体代码
先把原先的com.test.jsf.T1类中的
public String getMsg() {
this.msg="Hello,World";
return this.msg;
}
this.msg="Hello,World";这段代码删除掉,然后在WebRoot/WEB-INF/下新建spring的配置文件all-config.xml,输入以下xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="t1" class="com.test.jsf.T1">
<!--注入hibernate操作类-->
<property name="msg">
<value>测试成功啦</value>
</property>
</bean>
</beans>
再把然后我们改写前面的MyJsp.jsp文件中的 <%@ page language="java" pageEncoding="ISO-8859-1"%>改写成<%@ page language="java" pageEncoding="gbk"%>
启动tomcat,打开游览器,输入地址http://127.0.0.1:8080/Test/MyJsp.faces,如看到“测试成功啦”错误!未找到引用源。错误!未找到引用源。字样就说明JSF托管Bean配置成功了。
3、整和spring和hibernate
3.1、建立项目中使用数据库
本文中的数据库采用的是mysql数据库,项目中自然需要mysql数据库的java驱动,把驱动拷贝到项目的WebRoot/WEB-INF/lib下。建立数据库test1,在test1下建立表myuser,表结构如下:
Myuser表 | |||
列名 | 类型 | 长度 | 备注 |
id | Char | 35 | 主键 |
uname | Char | 50 |
|
建立好该表后,输入下面一些数据:
id列 | uname列 |
A1 | hello |
A2 | world |
A3 | good |
A4 | idea |
3.2、把all-config.xml配置分层次存放在几个xml文件中
因为现在又有jsf的托管Bean,又将有hibernate整和配置等xml代码一起存放在all-config.xml中,这样项目代码大起来的时候将会非常混乱难看。所以我们将把all-config.xml配置分层次存放在几个xml文件中。
3.2.1、转移jsf的托管Bean到新建立的xml文件中
在WebRoot/WEB-INF/下新建spring-jsf.xml文件,把all-config.xml里面的文件全部拷贝到spring-jsf.xml中,spring-jsf文件就是存放jsf的托管Bean的xml文件。然后把按照以下代码改写all-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 以下spring-hibernate.xml是hibernate操作基础类,等下创建,详见3.2节 -->
<import resource="spring-hibernate.xml" />
<!-- 以下spring-hibernate-beans.xml是hibernate操作实例,等下创建,详见3.6小节 -->
<import resource="spring-hibernate-beans.xml" />
<!-- 以下spring-jsf.xml是jsf操作类,上面创建了,详见本节和2.2节 -->
<import resource="spring-jsf.xml" />
</beans>
3.3、开始整合spring和hibernate
在WebRoot/WEB-INF/下新建spring-hibernate.xml文件,然后把按照以下代码改写spring-hibernate.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">
<!--把spring和hibernate整合的首要条件,设定sessionFactory-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
这里讲下三点说明:
①、<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">是spring中操作hibernate的关键类,我们这里先配置它,等下要用上hibernate的时候我就注入它
②、配置中:
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
这里指出hibernate数据库配置文件名和位置,当运行项目时候它的具体位置为WebRoot/WEB-INF/classes/hibernate.cfg.xml,当编写代码的时候它的具体位置在项目/src/ hibernate.cfg.xml中,这个不用理它,我们还没有创建,等下再说。
③、<prop key="hibernate.format_sql">true</prop>配置它要格式化hibernate中生成sql语句,一般都是选择true,启动用这个功能。
<prop key="hibernate.show_sql">true</prop>配置了要在控制它中输出hibernate中的类查询语句hql,一般在调试和开发的时候都需要它,我选择了true,当发布了这个项目的时候我就选择false关闭这个功能
3.4、配置hibernate中连接数据库的文件hibernate.cfg.xml
在项目src下新建hibernate.cfg.xml,代码如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--connection.username 为mysql数据库的用户名-->
<property name="connection.username">root</property>
<!-- jdbc:mysql://127.0.0.1:3306/test1中的test1 为本文中要使用的数据库-->
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/test1
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">MySql</property>
<!-- connection.password为mysql数据库的用户密码-->
<property name="connection.password">sa</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- mapping resource 指出hibernate的类映射文件位置,等下创建,详见3.5小节-->
<mapping resource="com/test/hibernate/Myuser.hbm.xml" />
</session-factory>
</hibernate-configuration>
3.5、编写hibernate的O/R映射代码
新建com.test.hibernate包,在包下建立Myuser.java类文件和Myuser.hbm.xml文件。
Myuser.java中代码如下:
package com.test.hibernate;
public class Myuser {
private String uid;
private String uname;
public Myuser() {
}
public Myuser(String uid, String uname) {
this.uid = uid;
this.uname = uname;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUname() {
return this.uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
Myuser.hbm.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.hibernate.Myuser" table="myuser" catalog="myblog">
<id name="uid" type="java.lang.String">
<column name="uid" length="35" />
<generator class="assigned" />
</id>
<property name="uname" type="java.lang.String">
<column name="uname" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
好了,hibernate的O/R映射代码就这样完成了,下面接着说在spring中如何操作hibernate。
3.6、编写spring操作hibernate的代码
新建com.test.hibernateDaoBean包,在包下建立MyuserDao.java类文件。
MyuserDao中代码如下:
package com.test.hibernateDaoBean;
import java.util.List;
import com.test.hibernate.Myuser;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class MyuserDao extends HibernateDaoSupport {
public List<Myuser> findAll() {
String hql = "from Myuser";
return getHibernateTemplate().find(hql);
}
}
这个MyuserDao类,稍后我将会注入com.test.jsf.T1类中。从代码中可以看出MyuserDao继承了org.springframework.orm.hibernate3.support.HibernateDaoSupport类,该类有个重要的方法就是getHibernateTemplate().find(String hql);负责把数据库的每一行都实例化一个类然后把这些实例化的类存放到List表中,方便用户操作,操作这个getHibernateTemplate().find(String hql)方法,还需要注入前面3.3节配置好的org.springframework.orm.hibernate3.LocalSessionFactoryBean,id名为sessionFactory,详见3.3小节,所以我们要做以下工作:
①、在WebRoot/WEB-INF/下新建spring-hibernate-beans.xml文件;
②、把以下代码写入spring-hibernate-beans.xml文件,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">
<!—把前面创建的sessionFactory 注入到com.test.hibernateDaoBean.MyuserDao 中,这样才能操作前面配置好的数据库-->
<bean id="myuserDao" class="com.test.hibernateDaoBean.MyuserDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
3.7、把MyuserDao注入到前面的创建的T1类中(注:T1类是将数据表现在JSF的界面类)
打开com.test.jsf.T1,代码修改如下:
package com.test.jsf;
import com.test.hibernateDaoBean.MyuserDao;
import java.util.List;
public class T1 {
private String msg;
private MyuserDao myuserDao;
private List dbList;
public List getDbList() {
this.dbList = this.myuserDao.findAll();
return dbList;
}
public void setDbList(List dbList) {
this.dbList = dbList;
}
public MyuserDao getMyuserDao() {
return myuserDao;
}
public void setMyuserDao(MyuserDao myuserDao) {
this.myuserDao = myuserDao;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return this.msg;
}
}
然后打开前面的spring-jsf.xml修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="t1" class="com.test.jsf.T1">
<!--注入hibernate操作类-->
<property name="msg">
<value>测试成功啦</value>
</property>
<!--注入hibernate操作类-->
<property name="myuserDao">
<ref bean="myuserDao" />
</property>
</bean>
</beans>
呼,透口气先,好,我们做完最后一步就可以了,新建一个jsf格式的MyJsp2.jsp文件,修改代码如下:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<f:view>
<h:form>
<h:dataTable id="mylist" value="#{t1.dbList}" var="user">
<h:column>
<f:facet name="header">
<h:outputText value="编号" />
</f:facet>
<h:outputText value="#{user.uid}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="用户名" />
</f:facet>
<h:outputText value="#{user.uname}" />
</h:column>
</h:dataTable>
</h:form>
</f:view>
</body>
</html>
启动tomcat,打开游览器,输入地址http://127.0.0.1:8080/Test/MyJsp2.faces,如看到
编号 姓名
A1 hello
A2 world
A3 good
A4 idea
字样就说明spring+hibernate +jsf整和成功了。好了,本文到此为止,谢谢大家观看和学习本文,如果大家有什么疑问,可以加我QQ或者Email我。
2008年12月30日于办公室内完成 作者:黄颢鹏
参考文献(无)
作者联系QQ:25950329 联系Email:anh3000@qq.com
注:转载前请注明作者名和出处,如违反该款作者保留追究权利