springhibernatejsf集成

来源:互联网 发布:java ee 源码 编辑:程序博客网 时间:2024/05/05 01:45

 spring+hibernate+jsf

  现在各种各样的框架层出不穷

  首先说一下spring  hibernate jsf  的优势

  可以说jsf 是javaee 的一种规范

  可是由于 jsf 比其他表示层框架 入门有点 难上手

  所以很多人和公司都选择用struts 这种开源的框架

  如果我们 熟悉jsf 用它来做表示层

  我们将 不用那么烦琐

  下面把这个三个框架如何集成谈一下

  首先我们  把所有的jar  导入近来

  在这个  我们使用注释 来映射hibernate

  只需要导入spring 中的两个注释包

  hibernate-annotations.jar 和hibernate-commons-annotations.jar

  就不在写配置文件了

  接下来配置web.xml

 配置一个监听器

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

来 加载contextConfigLacation

然后在配置一个

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/cn/accp/adon/cfg/app.xml</param-value>
 </context-param>

这样服务一启动就加载了我们的配置文件

    接下来 需要配置就是app.xml


配子hibernate 模版类 
 <bean id="ht" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref="sf"></property>
 </bean>

配置sessionFactory  但是需要注意的是 这个工厂 不在以前哪个SessionBeanFactory  而是

spring 提供一个注释注入的一个类

<bean id="sf"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

<bean>

配置实体类  记住他的属性名是annotatedClasses(注释实体类)

<property name="annotatedClasses">
  <list>
  <value>adon.entity.User</value>
  <value>adon.entity.Role</value>
  <value>adon.entity.Module</value>
  <value>adon.entity.RoleModule</value>
  </list>
  </property>

数据源和其他没有区别

 <property name="dataSource" ref="ds"></property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
   </props>
  </property>

 

下面 需要对jsf包添加

 在eclipse 中  我们选种window --->preferences--web  --javaserver faces Tool 下的librares

包你的jsf jar 添加进来

下面只要在工程的属性上添加使用就可以

 

 

 

 

 

原创粉丝点击