隨筆-112  評(píng)論-73  文章-0  trackbacks-0

          JSF2.0 與Spring 3.0 集成

          同以前的JSF1.2Spring2.5集成類似,只是有一些類名的變化。

          web.xml 代碼如下:

          <context-param>
                  
          <param-name>contextConfigLocation</param-name>
                  
          <param-value>WEB-INF/applicationContext.xml</param-value>
              
          </context-param>
              
          <listener>
                  
          <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
              
          </listener>
              
          <listener>
                  
          <listener-class> org.springframework.web.context.ContextLoaderListener
                      
          </listener-class>
              
          </listener>
              
          <listener>
                  
          <listener-class>
                      org.springframework.web.context.request.RequestContextListener
          </listener-class>
              
          </listener>
              
          <servlet>
                  
          <servlet-name>Faces Servlet</servlet-name>
                  
          <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
                  
          <load-on-startup>1</load-on-startup>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>Faces Servlet</servlet-name>
                  
          <url-pattern>*.xhtml</url-pattern>
              
          </servlet-mapping>
              
          <servlet-mapping>
                  
          <servlet-name>Faces Servlet</servlet-name>
                  
          <url-pattern>*.jsf</url-pattern>
              
          </servlet-mapping>
              
          <context-param>
                  
          <param-name>javax.faces.PROJECT_STAGE</param-name>
                  
          <param-value>Development</param-value>
              
          </context-param>
              
          <welcome-file-list>
                  
          <welcome-file>index.html</welcome-file>
              
          </welcome-file-list>


          Faces-config.xml中加入:

          <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

          JSF1.21.2以前是加入

          <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>

          Spring 的配置文件就正常配置就可以了。

          ApplicationContext.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" xmlns:aop="http://www.springframework.org/schema/aop"
              xmlns:tx
          ="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
              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="propertyConfigurer"
                  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                  <property name="location" value="WEB-INF/jdbc.properties" /> </bean>
              
          -->
              
          <!-- hibernate sessionFactory -->
              
          <context:annotation-config/>
              
          <context:component-scan base-package="cn.xiangyunsoft" />
              
          <bean id="sessionFactory"
                  class
          ="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                  
          <property name="hibernateProperties" value="classpath:hibernate.properties" />
                  
          <property name="configLocations">
                      
          <list>
                          
          <!-- 使用hibernate.cfg.xml配置文件 -->
                          
          <value>classpath:hibernate.cfg.xml
                          
          </value>
                      
          </list>
                  
          </property>
              
          </bean>
              
          <!-- 配置事務(wù)管理 -->
              
          <!-- 事務(wù)通知類 -->
              
          <!--
                  <bean id="profiler"
                  class="cn.xiangyunsoft.business.service.SimpleProfiler"> order
                  值可以決定通知的先后順序 ,與后面的order的值的大小,決定了是先通知再執(zhí)行,還是先執(zhí)行再通知 <property
                  name="order" value="2" /> </bean>
              
          -->
              
          <bean id="transactionManager"
                  class
          ="org.springframework.orm.hibernate3.HibernateTransactionManager">
                  
          <property name="sessionFactory" ref="sessionFactory" />
              
          </bean>
              
          <aop:config>
                  
          <!-- 此處的IService 是表示對(duì)所有實(shí)現(xiàn)IService接口的類管理事務(wù) -->
                  
          <aop:advisor
                      
          pointcut="execution(* cn.xiangyunsoft.*.service..*ServiceImpl.*(..))"
                      advice-ref
          ="txAdvice" />
                  
          <!--
                      加入之后事務(wù)不起作用> <aop:aspect id="profilingAspect" ref="profiler">
                      <aop:pointcut id="serviceMethodWithReturnValue"
                      expression="execution(*
                      cn.xiangyunsoft.*.service..*ServiceImpl.*(..))" />
                      <aop:after-throwing method="profile"
                      pointcut-ref="serviceMethodWithReturnValue" /> </aop:aspect
                  
          -->
              
          </aop:config>
              
          <tx:advice id="txAdvice" transaction-manager="transactionManager">
                  
          <!-- the transactional semantics -->
                  
          <tx:attributes>
                      
          <!-- 以get、find、load開頭的方法是只讀事務(wù) -->
                      
          <tx:method name="*" read-only="true" />
                      
          <!--<tx:method name="find*" read-only="true" />-->
                      
          <!--<tx:method name="load*" read-only="true" />-->
                      
          <!-- 其他方法是默認(rèn),事務(wù)隔離級(jí)別為:保證一個(gè)事務(wù)修改的數(shù)據(jù)提交后才能被另外一個(gè)事務(wù)讀取 -->
                      
          <tx:method name="save*" isolation="REPEATABLE_READ"
                          propagation
          ="REQUIRED" />
                      
          <tx:method name="delete*" isolation="REPEATABLE_READ"
                          propagation
          ="REQUIRED" />
                          
                  
          </tx:attributes>
              
          </tx:advice>
          </beans>


          一個(gè)注入Spring bean 的 JSF bean 代碼如下:

          @ManagedBean(name = ClassItemBean.NAME)
          public class ClassItemBean {

              
          public static final String NAME = "classItemBean";

              
          /*
               *在spring 中配置的service.
               
          */
              @ManagedProperty(name 
          = "classItemService", value = "#{classItemService}")
              
          private ClassItemService classItemService;

              
          public void setClassItemService(ClassItemService classItemService) {
                  
          this.classItemService = classItemService;
              }

              
          public String hello() {
                  System.out.println(
          "hello." + classItemService);
                  Object obj 
          = classItemService.get(ClassItem.class"01");
                  System.out.println(
          "obj = " + obj);
                  
          return null;
              }
          }


          這樣集成就完畢了。很簡(jiǎn)單,很強(qiáng)大。




          posted on 2010-04-24 15:03 Libo 閱讀(2860) 評(píng)論(2)  編輯  收藏 所屬分類: SpringJSF 2

          評(píng)論:
          # re: JSF2.0 與Spring 3.0 集成 2010-07-19 17:53 | 任亮
          請(qǐng)問(wèn),博主用的什么服務(wù)器。怎么配置的啊。我現(xiàn)在在做一個(gè)jsf2.0的項(xiàng)目,想用jboss4.2服務(wù)器,但是配置不對(duì),希望博主幫忙。  回復(fù)  更多評(píng)論
            
          # re: JSF2.0 與Spring 3.0 集成 2010-07-26 17:24 | Libo
          @任亮
          我使用的是glassfish,tomcat6 也可以。jboss4.2沒(méi)用過(guò)。  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 子洲县| 新泰市| 金平| 库伦旗| 子长县| 白山市| 调兵山市| 临武县| 化隆| 中卫市| 建湖县| 如东县| 安图县| 卢龙县| 大名县| 天柱县| 丹阳市| 深水埗区| 新绛县| 阜城县| 巴马| 上杭县| 兴隆县| 洪湖市| 普兰店市| 延津县| 河北省| 博兴县| 高碑店市| 长兴县| 新兴县| 错那县| 右玉县| 洛扎县| 宜兰县| 尼木县| 新兴县| 内黄县| 饶河县| 延吉市| 和政县|