日期:2014-05-16  浏览次数:20346 次

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 #IM