首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

JSF2.1+Spring3.1+JPA(Hibernate3.6)配备实例

2012-11-06 
JSF2.1+Spring3.1+JPA(Hibernate3.6)配置实例????? 首先搭建环境,就不具体说了,我用的是eclipse(不是myecl

JSF2.1+Spring3.1+JPA(Hibernate3.6)配置实例

????? 首先搭建环境,就不具体说了,我用的是eclipse(不是myeclipse),导入必要的jar包,需要特别注意,Spring的两个AOP的jar包也要倒进去,一开始我认为Aop跟我都应用无关,所以没有导入这两个包,带来不少麻烦。

????? 这里主要讲配置。有两种情况:Pure JPA + Spring和Spring JpaDaoSupport。这两种情况大同小异,主要是细节上有些差别。

????? 一、Pure JPA + Spring 四个配置文件分别如下:

???? 1、applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="dataSource" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/testcases"/>
<property name="username" value="root"/>
<property name="password" value="beijing123"/>
</bean>

<!-- 这里不需要配置 entityFactory -->

<bean id="entityManagerFactory" ref="dataSource" />
<property name="persistenceUnitName" value="testcasemanager"/>
</bean>

<bean ref="entityManagerFactory"/>
</bean>
<!--
<tx:annotation-driven transaction-manager="transactionManager" />
-->

<bean id="atcDao" ref="atcDao"/>
</bean>

</beans>

??? 2、face-config.xml

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-facesconfig_2_0.xsd"
version="2.0">

<application>

<variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
<!--
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
-->
</application>


<managed-bean>
<managed-bean-name>LoginBean</managed-bean-name>
<managed-bean-class>com.semc.conversation.control.LoginBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
<managed-bean-name>coBean</managed-bean-name>
<managed-bean-class>com.semc.conversation.control.CaseOperatorBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>caseManager</property-name>
<value>#{caseManager}</value>
</managed-property>
</managed-bean>
</faces-config>

??? 在这里直接引用了Spring Context中的bean。

??? 3、persistence.xml(在META-INF路径下)

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="testcasemanager" transaction-type="RESOURCE_LOCAL">

<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.semc.conversation.model.Auto_cases</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

?? 4.创建工程的时候选择支持JSF,可以在web.xml中自动产生关于JSF的配置,然后在web.xml中加入以下配置:

</bean>

?

?

热点排行
Bad Request.