spring+hibernate+operamask搭建

步驟:
1.建立web工程
2.增加spring支持

3.增加herbernate支持
4.加入AOM jar包
5.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext.xml
        </param-value>
    </context-param>

    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>org.operamasks.faces.SKIN</param-name>
        <param-value>aqua</param-value>
    </context-param>
    <context-param>
        <param-name>org.operamasks.faces.FORM_RICH</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <param-name>
            org.operamasks.faces.IMPLICIT_NAVIGATION
        </param-name>
        <param-value>true</param-value>
    </context-param>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
            org.operamasks.faces.application.ManagedBeanLifecycleListener
        </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>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>ResourceServlet</servlet-name>
        <servlet-class>
            org.operamasks.faces.render.resource.ResourceServlet
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ResourceServlet</servlet-name>
        <url-pattern>/_global/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>999999</session-timeout>
    </session-config>
    <!-- This security constraint illustrates how XHTML pages
        with JavaServer Faces components can be protected from
        being accessed without going through the Faces Servlet.
        The security constraint ensures that the Faces Servlet will
        be used or the pages will not be processed. -->
    <security-constraint>
        <display-name>Restrict access to XHTML pages</display-name>
        <web-resource-collection>
            <web-resource-name>Facelet pages</web-resource-name>
            <url-pattern>*.xhtml</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description>
                With no roles defined, no access granted.
            </description>
        </auth-constraint>
    </security-constraint>
</web-app>
    spring參數的配置(可以配置多個文件,不寫爲默認文件)
    aom參數配置(可以配置多個文件,不寫爲默認文件)
    監聽器配置
    sevlet配置(jsf和aom)
    過濾器配置(字符過濾)
    會話配置  (會話時間)
    xhtml安全
6.建立數據庫
7.創建數據訪問對象
8.創建jsf頁面和對應的bean
    operamasks.xml將xhtml和bean聯繫到一塊也就說將模型和視圖關聯
<operamasks-config xmlns="http://www.operamasks.org/IoVC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.operamasks.org/IoVC http://www.operamasks.org/schema/operamasks.xsd ">
  <view-mapping>
    <url-pattern>*</url-pattern>
    <model-bean>#{~view}Bean</model-bean>
  </view-mapping>
</operamasks-config>


faces-config.xml是JSF規範中的一個標準的和JSF相關的配置文件。在傳統的JSF實現中,所有 ManagedBean 的配置、導航規則的配置等等,都需要在此文件中進行配置。AOM對其做了大量的擴展與簡化,因此,在 AOM 中,這個配置文件非常簡單,內容基本都是固定的:

<faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" version="1.2">

  <application>
    1<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
  </application>
</faces-config>

OpenSessionInViewFilter是Spring提供的一個針對Hibernate的一個支持類,其主要意思是在發起一個頁面請求時打開Hibernate的Session,一直保持這個Session,直到這個請求結束,具體是通過一個Filter來實現的。

由於Hibernate引入了Lazy Load特性,使得脫離Hibernate的Session週期的對象如果再想通過getter方法取到其關聯對象的值,Hibernate會拋出一個 LazyLoad的Exception。所以爲了解決這個問題,Spring引入了這個Filter,使得Hibernate的Session的生命週期變長。
注意事項:
1.如果選中spring-AOP包,可以會有衝突,刪除asm-x.xx.jar即可
2.如果選j2ee5可能會跟jsf-impl.jar衝突,建議使用j2ee1.4,或者刪除該包(operamask2.1)。
3.operamask下的lib下的不需要全放進去,spring的不必再放,但是如果是使用舊版本要把包下帶operamask字樣的包放如路徑。
發佈了8 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章