Spring学习笔记(八)-Spring+Struts2+Hibernate的整合

1.Spring2.5+Hibernate3.3+struts2整合

1.首先引入jar文件:

2.工程的目录结构:

Spring学习笔记(八)-Spring+Struts2+Hibernate的整合_第1张图片

3.spring的beans.xml文件的配置(这里面就包含了对hibernate的配置,将hibernate的sessionFactory交给了spring去管理):

"1.0" encoding="UTF-8"?>

"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"

       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">

   

      


       

      "com.gaoyuan"/>

     

      "classpath:jdbc.properties"/> 

       

        "dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    "driverClassName" value="${driverClassName}"/>

    "url" value="${url}"/>

    "username" value="${username}"/>

    "password" value="${password}"/>

     

 "initialSize" value="${initialSize}"/>

 

 "maxActive" value="${maxActive}"/>

 

 "maxIdle" value="${maxIdle}"/>

 

 "minIdle" value="${minIdle}"/>

   


   

      "sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

     "dataSource" ref="dataSource"/>

 "mappingResources">

    

      

      com/gaoyuan/bean/Person.hbm.xml

    

 

     "hibernateProperties">

     

    

        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

        hibernate.hbm2ddl.auto=update

        hibernate.show_sql=false

        hibernate.format_sql=false

        

        

        hibernate.cache.use_second_level_cache=true

        

                hibernate.cache.use_query_cache=false

                

             hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider

      

     

 

 

 "txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

   "sessionFactory" ref="sessionFactory"/>

 

 

 "txManager"/>

 "personList" class="com.gaoyuan.action.PersonAction">


4.struts的struts.xml的配置:

"1.0" encoding="UTF-8"?>

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

    

    "struts.i18n.encoding" value="UTF-8"/>

    

    "struts.action.extension" value="do"/>

    

    "struts.serve.static.browserCache" value="false"/>

    

    "struts.configuration.xml.reload" value="true"/>

    

    "struts.devMode" value="true" />

     

    "struts.ui.theme" value="simple" />

    

    "struts.objectFactory" value="spring" />


  "person" namespace="/" extends="struts-default">

 

 

  "message">/WEB-INF/page/message.jsp

 

 

"action_*" class="personList" method="{1}">

"list">/WEB-INF/page/personlist.jsp

    

5.上面的几个xml文件都配置好之后,需要将上配置的配置信息告诉服务器,因此现在就开始配置web.xml文件,让服务器在启动的时候,就将spring容器该加载的内容都加载上,还有对struts的一个拦截,将这三个框架融合到一起。Web.xml文件的配置如下:

"1.0" encoding="UTF-8"?>

"http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  SSH2


  

   contextConfigLocation

   classpath:beans.xml

      org.springframework.web.context.ContextLoaderListener

        struts2

        org.apache.struts2.dispatcher.FilterDispatcher

    

    

        struts2

        /*

   

encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encoding

/*

        OpenSessionInViewFilter

        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

        OpenSessionInViewFilter

        /*



  

    index.html

    index.htm

    index.jsp

    default.html

    default.htm

    default.jsp

  

6.编写PersonServiceimpl.java,PersonService.java和Person.java,person.hbm.xml都和spring和hibernate整合时写的一样,此处省略;

7.编写action

package com.gaoyuan.action;

import java.util.List;

import javax.annotation.Resource;

import com.gaoyuan.bean.Person;

import com.gaoyuan.service.PersonService;

public class PersonAction {

@Resource

private PersonService personService;

private String message;

private List persons;

//编写的list方法

public String list(){

this.persons = personService.getPersons();

return "list";

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public List getPersons() {

return persons;

}

public void setPersons(List persons) {

this.persons = persons;

}

}

8.编写return之后的jsp页面

Spring学习笔记(八)-Spring+Struts2+Hibernate的整合_第2张图片

你可能感兴趣的:(Spring学习笔记(八)-Spring+Struts2+Hibernate的整合)