spring+hibernate+operamask搭建

来源:互联网 发布:网络与新媒体好就业吗 编辑:程序博客网 时间:2024/05/29 21:29
步骤:
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字样的包放如路径。
原创粉丝点击