Spring和Hibernate整合

来源:互联网 发布:c4d r17 mac安装教程 编辑:程序博客网 时间:2024/06/05 19:38

创建Java Web项目

修改web.xml,如下:

<filter>    <filter-name>openSessionInViewFilter</filter-name>    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>    <init-param>      <param-name>singleSession</param-name>      <param-value>true</param-value>    </init-param>    <init-param>      <param-name>flushMode</param-name>      <param-value>AUTO</param-value>    </init-param>  </filter>  <filter-mapping>    <filter-name>openSessionInViewFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <filter>    <filter-name>sitemesh</filter-name>    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>sitemesh</filter-name>    <url-pattern>*.do</url-pattern>  </filter-mapping>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>WEB-INF/appCtx-*.xml</param-value>  </context-param>  <servlet>    <servlet-name>springmvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>springmvc</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>  <error-page>    <error-code>404</error-code>    <location>/WEB-INF/views/404.jsp</location>  </error-page>

WEB-INF下创建springmvc-servlet.xml文件,内容如下:

<!--?xml version="1.0" encoding="UTF-8"?--><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-2.5.xsd        http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd        http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <context:annotation-config></context:annotation-config>    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>    <context:component-scan base-package="com.ais.cms"></context:component-scan>    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <property name="defaultEncoding" value="utf-8"></property>           <property name="maxUploadSize" value="10485760000"></property>          <property name="maxInMemorySize" value="40960"></property>      </bean>     <bean id="exceptionResolver" class="com.ais.cms.controller.BaseController">        <property name="exceptionMappings">            <props>                <prop key="com.ais.cms.exception.BusinessException">error</prop>                <prop key="java.lang.Exception">error</prop>            </props>        </property>    </bean></beans>

WEB-INF下创建appCtx-ssi.xml文件,内容如下:

<!--?xml version="1.0" encoding="UTF-8"?--><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-2.5.xsd        http://www.springframework.org/schema/aop         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd        http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <!-- 配置sessionFactory -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <property name="configLocation">            <value>classpath:hibernate.cfg.xml</value>        </property>    </bean>    <!-- 配置事务管理器 -->    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory">            <ref bean="sessionFactory"></ref>        </property>    </bean>    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>    <!-- 定义Hibernate模板对象 -->    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean></beans>

WEB-INF下创建sitemesh3.xml文件,内容如下:

<sitemesh>  <mapping path="*.do" decorator="/layout/template.jsp"></mapping>  <mapping path="/login.do" exclue="true"></mapping>  <mapping path="/main.do" exclue="true"></mapping>  <mapping path="/error.do" exclue="true"></mapping></sitemesh>

WebRoot或WebContent下创建layout文件夹,并添加template.jsp文件,内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><meta charset="utf-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1">    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->    <meta name="description" content="">    <meta name="author" content="">    <title>&lt;sitemesh:write property='title'&gt;&lt;/sitemesh:write&gt;</title>    <sitemesh:write property="head"></sitemesh:write>    <!-- Fixed navbar -->    <nav class="navbar navbar-default">      <div class="container">        <div class="navbar-header">          <a class="navbar-brand" href="#">SB Admin v2.0 后台管理系统</a>        </div>      </div>    </nav>    <!-- Begin page content -->    <div class="container">      <div class="row">        <div class="col-xs-4">        </div>        <div class="col-xs-8">            <sitemesh:write property="body"></sitemesh:write>        </div>      </div>    </div>    <footer class="footer">      <div class="container">        Copyright © 2012-2015 All Rights Reserved      </div>    </footer>