最近一直在用这个框架,现在来就它们的整合要点来做一个总结。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 >
<!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 >
<!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 >
[
<!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 >
<!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 >
<!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,如需转载请自行联系原作者