通过近两天时间的学习,对JSF+SPRING+HIBERNATE整合简单完成,现在分享如下:
平台及版本:
eclipse-SDK-3.2.1-win32.zip、MyEclipseEnterpriseWorkbenchInstaller_5.1.0GA_E3.2.1.exe、Hibernate 3.1、MyFaces 1.1、spring 1.2。
注意:hibernate-annotations.jar的版有问题,最简单就是从类型去掉,如果不去掉会报java.lang.NoClassDefFoundError: org/hibernate/cfg/HbmBinder$SecondPass 错误。
1、在WEB.XML中加入以下内容:
xml 代码
- <context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>/WEB-INF/applicationContext.xml</param-value>
-
</context-param>
-
<servlet>
-
<servlet-name>context</servlet-name>
-
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
2、在faces-config.xml中加入以下内容
xml 代码
- <application>
-
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
-
</application>
下面的是一个例子,引用了spring中的BEAN
xml 代码
- <managed-bean>
-
<managed-bean-name>userBean</managed-bean-name>
-
<managed-bean-class>com.UserBean</managed-bean-class>
-
<managed-bean-scope>request</managed-bean-scope>
-
<managed-property>
-
<property-name>userDAO</property-name>
-
<value>#{UserDAO}</value>
-
</managed-property>
-
</managed-bean>
3、applicationContext.xml内容如下:
xml 代码
- <beans>
-
-
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
-
<property name="driverClassName">
-
<value>com.mysql.jdbc.Driver</value>
-
</property>
-
<property name="url">
-
<value>jdbc:mysql://NB-ZZZ:3306/y366</value>
-
</property>
-
<property name="username">
-
<value>root</value>
-
</property>
-
<property name="password">
-
<value>pacs3i</value>
-
</property>
-
</bean>
-
-
-
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
-
<property name="dataSource">
-
<ref local="dataSource" />
-
</property>
-
<property name="mappingResources">
-
<list>
-
<value>com/User.hbm.xml</value>
-
</list>
-
</property>
-
<property name="hibernateProperties">
-
<props>
-
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
-
<prop key="hibernate.show_sql">true</prop>
-
</props>
-
</property>
-
</bean>
-
-
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
-
<property name="sessionFactory">
-
<ref local="sessionFactory" />
-
</property>
-
</bean>
-
-
<bean id="UserDAO" class="com.UserDAO">
-
<property name="sessionFactory">
-
<ref bean="sessionFactory" />
-
</property>
-
</bean></beans>
-
搞定!!!!!!!!
分享到:
评论