JSF+Spring+Hibernate整合要点

简介:
最近一直在用这个框架,现在来就它们的整合要点来做一个总结。jsf与spring结合的时候在service层spring对jsf进行注入,下面来看看配置文件。
web.xml
<? xml  version ="1.0" ?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 
< web-app > 
  //关联jsf 
         < listener > 
                 < listener-class >org.apache.myfaces.webapp.StartupServletContextListener </ listener-class > 
         </ listener > 
  //关联spring 
         < listener > 
                 < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class > 
         </ listener > 
        //关联jsf的核心配置文件 
         < context-param > 
                 < param-name >javax.faces.CONFIG_FILES </ param-name > 
                 < param-value > 
                         /WEB-INF/tiles-faces-config.xml, 
                        /WEB-INF/conf/jsf/user-faces-config.xml 
                 </ param-value > 
  //关联页面上要用到的下拉菜单 
         < context-param > 
                 < param-name >tiles-definitions </ param-name > 
                 < param-value > 
      /WEB-INF/conf/jsf/user-tiles.xml 
                 </ param-value > 
         </ context-param > 
  //关联spring配置文件 
         < context-param > 
                 < param-name >contextConfigLocation </ param-name > 
                 < param-value > 
                        /WEB-INF/conf/spring/*.xml 
                 </ param-value > 
         </ context-param > 
  //从地址栏匹配符合条件的内容,用Faces Servlet进行控制 
   < servlet > 
                 < servlet-name >Faces Servlet </ servlet-name > 
                 < servlet-class >javax.faces.webapp.FacesServlet </ servlet-class > 
                 < load-on-startup >0 </ load-on-startup > 
         </ servlet > 

         < servlet-mapping > 
                 < servlet-name >Faces Servlet </ servlet-name > 
                 < url-pattern >*.jsf </ url-pattern > 
         </ servlet-mapping > 

   < welcome-file-list > 
                 < welcome-file >Login.jsf </ welcome-file > 
         </ welcome-file-list > 
</ web-app >
tiles-faces-config.xml这个文件里面的内容是写在face-config.xml文件中的,如果就一个face-config.xml文件,可以将以下内容写入face-config.xml文件中,但是当有多个时,为了避免重复的代码就单独提了出来
<? xml  version ="1.0"  encoding ="UTF-8" ?> 
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" 
                                                            "http://java.sun.com/dtd/web-facesconfig_1_0.dtd"> 
< faces-config > 
< application > 
     < variable-resolver >org.springframework.web.jsf.DelegatingVariableResolver </ variable-resolver > 
</ application > 
</ faces-config >
user-tiles.xml
<!DOCTYPE tiles-definitions 
                [ 
                                <!ELEMENT component-definitions (definition+)> 
                                <!ELEMENT tiles-definitions (definition+)> 
                                <!ELEMENT definition (put*, putList*)> 
                                <!ATTLIST definition 
                                                name CDATA #REQUIRED 
                                                pages CDATA #IMPLIED 
                                                path CDATA #IMPLIED 
                                                extends CDATA #IMPLIED 
                                                role CDATA #IMPLIED 
                                                template CDATA #IMPLIED 
                                                controllerClass CDATA #IMPLIED 
                                                controllerUrl CDATA #IMPLIED> 
                                <!ELEMENT put (#PCDATA)> 
                                <!ATTLIST put 
                                                name CDATA #REQUIRED 
                                                value CDATA #IMPLIED 
                                                type (string | pages | template | definition) #IMPLIED 
                                                content CDATA #IMPLIED 
                                                direct (true | false) #IMPLIED> 
                                <!ELEMENT putList ( (add* | item* | bean* | putList*)+) > 
                                <!ATTLIST putList 
                                                name CDATA #REQUIRED> 
                                <!ELEMENT putListElements (add | item | bean)> 
                                <!ELEMENT add (#PCDATA)> 
                                <!ATTLIST add 
                                                value CDATA #IMPLIED 
                                                type (string | pages | template | definition) #IMPLIED 
                                                content CDATA #IMPLIED 
                                                direct (true | false) #IMPLIED> 
                                <!ELEMENT bean (#PCDATA)> 
                                <!ATTLIST bean 
                                                classtype CDATA #REQUIRED> 
                                <!ELEMENT item (#PCDATA)> 
                                <!ATTLIST item 
                                                value CDATA #REQUIRED 
                                                link CDATA #REQUIRED 
                                                classtype CDATA #IMPLIED 
                                                icon CDATA #IMPLIED 
                                                tooltip CDATA #IMPLIED> 
                                ]> 

< tiles-definitions >//这个标签和web.xml中的param-name相对 
         < definition  name ="/Login.tiles"  extends ="layout.busitemplateLogin" > 
                 < put  name ="body"  value ="/pages/Login.jsp" /> 
         </ definition > 

         < definition  name ="/user/UserManager.tiles"  extends ="layout.busitemplate" > 
                 < put  name ="body"  value ="/pages/user/UserManager.jsp" /> 
         </ definition > 
        
</ tiles-definitions >
user-faces-config.xml在这个文件里面页面与类就要实现关联了,而且在这里实现jsf与spring的注入整合
<? xml  version ="1.0"  encoding ="UTF-8" ?> 
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" 
                "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> 
< faces-config > 
         < managed-bean > 
                 < managed-bean-name >user_loginBB </ managed-bean-name > 
                 < managed-bean-class >com.user.web.LoginBackingBean </ managed-bean-class > 
                 < managed-bean-scope >session </ managed-bean-scope > 
                 < managed-property > 
                         < property-name >userucc </ property-name > 
                         < value >#{user_userManageUCC} </ value >//与spring进行注入 
                 </ managed-property > 
         </ managed-bean > 

         < managed-bean > 
                 < managed-bean-name >user_userManagerBB </ managed-bean-name > 
                 < managed-bean-class >com.user.web.UserManagerBackingBean </ managed-bean-class > 
                 < managed-bean-scope >request </ managed-bean-scope > 
                 < managed-property > 
                         < property-name >usermanageucc </ property-name > 
                         < value >#{user_userManageUCC} </ value >//与spring关联进行注入 
                 </ managed-property > 
         </ managed-bean > 
         
         < navigation-rule > 
                 < from-view-id >/user/UserInfoEdit.jsp </ from-view-id >//从哪个页面来 
                 < navigation-case > 
                         < from-outcome >userlist </ from-outcome >//根据bean中return的字符串跳向指定的页面 
                         < to-view-id >/user/UserManager.jsf </ to-view-id > 
                 </ navigation-case > 
         </ navigation-rule > 
         
</ faces-config >
user-applicationContext.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 ="user_loginlogdao"  class ="com.user.dao.LoginLogDAO" > 
                 < property  name ="hibernatetemplate" > 
                         < ref  bean ="hibernateTemplate" /> 
                 </ property > 
         </ bean > 

         < bean  id ="user_logservice"  class ="com.user.service.LoginLogService" > 
                 < property  name ="loginlogdao" > 
                         < ref  bean ="user_loginlogdao" /> 
                 </ property > 
          </ bean > 

         < bean  id ="user_userManageService"  class ="com.user.service.UserManageService" > 
                ...可以在这里写入要注入的东西 
         </ bean > 

        <!-- ucc--> 
         < bean  id ="user_userManageUccImpl"  class ="com.user.ucc.impl.UserManageUCC" > 
                 < property  name ="usermanageservice" > 
                         < ref  bean ="user_userManageService" /> 
                 </ property > 
                 < property  name ="logservice" > 
                         < ref  bean ="user_logservice" /> 
                 </ property > 
         </ bean > 
                
        <!-- proxy-->//实现注入 
         < bean  id ="user_userManageUCC"  class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" > 
                 < property  name ="proxyInterfaces" > 
                         < list > 
                                 < value >com.user.ucc.IUserManageUCC </ value > 
                         </ list > 
                 </ property > 
                 < property  name ="target"  ref ="user_userManageUccImpl" /> 
                 < property  name ="transactionManager"  ref ="transactionManager" /> 
                 < property  name ="transactionAttributeSource"  ref ="namingTransactionAttribute" /> 
         </ bean > 
</ beans >

关于所涉及到的类和页面就不多写了,这里主要是看看jsf和spring的配置文件是如何进行关联的。



本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/354177,如需转载请自行联系原作者

相关文章
|
4月前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
96 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
4月前
|
Java 数据库连接 数据库
Spring Data JPA 与 Hibernate 之区别
【8月更文挑战第21天】
98 0
|
6月前
|
前端开发 Java 关系型数据库
在Spring3 MVC中五步配置集成注解方式Hibernate3
在Spring3 MVC中五步配置集成注解方式Hibernate3
43 3
|
5月前
|
Java 数据库连接 数据库
如何在Spring Boot中集成Hibernate
如何在Spring Boot中集成Hibernate
|
6月前
|
XML Java Apache
必知的技术知识:HHS整合(Struts2+Spring+Hibernate)
必知的技术知识:HHS整合(Struts2+Spring+Hibernate)
40 0
|
7月前
|
SQL Java 数据库连接
jpa、hibernate、spring-data-jpa、jdbcTemplate
jpa、hibernate、spring-data-jpa、jdbcTemplate
|
7月前
|
Java 数据库连接 Spring
spring配合hibernate报错:sessionFactory or hibernateTemplate is required
根据你的具体情况,检查上述步骤中的一个或多个,以确定为何出现“sessionFactory or hibernateTemplate is required”错误,并采取相应的措施进行修复。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
59 0
|
7月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——updateEmp.jsp
ssh(Spring+Spring mvc+hibernate)——updateEmp.jsp
|
7月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——updateDept.jsp
ssh(Spring+Spring mvc+hibernate)——updateDept.jsp
|
JavaScript Java 数据库连接
spring对hibernate的集成中的回调(CallBack)机制
spring对hibernate的集成中的回调(CallBack)机制
221 0
spring对hibernate的集成中的回调(CallBack)机制