码迷,mamicode.com
首页 > 编程语言 > 详细

java利用myeclipse自带三大框架搭建三大框架(Hibernate+Struts2+Spring)过程详解

时间:2018-02-12 13:37:39      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:bubuko   注解   图片   oca   name   cdata   cal   3.1   exec   

搭建过程因人而异,我的搭建过程大致是这样的:

  1.创建一个javaweb项目;

  2.导入Spring框架,上图:

    2.1:技术分享图片

    2.2:技术分享图片

    2.3:技术分享图片

 

  3.导入struts2框架,上图:

    3.1:技术分享图片

    3.2:技术分享图片

  next:

    3.3:技术分享图片

 

   4.导入Hibernate框架,说明:由于hibernate属于持久层,和数据库密切相关,所以需要我们提前出创建好数据库对应视图,然后再开始下面的操做。上图:

    4.1:技术分享图片

    4.2:技术分享图片

    4.3:技术分享图片

    4.4:技术分享图片

    4.5:技术分享图片

    4.6:技术分享图片

    4.7:利用数据库相关表和hibernate的orm生成实体类和对应配置文件信息

      4.7.1:技术分享图片

      4.7.2:技术分享图片

      4.7.3:技术分享图片

      4.7.4:技术分享图片

      4.7.5:技术分享图片

      4.7.6:技术分享图片

  5.查看初步搭建结果:

  技术分享图片

  6.进一步对配置文件进行配置:

    6.1:配置struts.xml文件

      6.1.1:选择要修改的配置文件路径进行修改

      技术分享图片

  6.1.2:配置struts.xml文件

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--将struts的控制权交给spring -->
<constant name="struts.objectFactory" value="spring"></constant>
<!-- 修改默认路径为该项目跟路径 -->
<constant name="struts.convention.result.path" value="/"/>
</struts>    

  6.2.修改application.xml文件

    6.2.1原文件

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="oracle.jdbc.OracleDriver">
        </property>
        <property name="url"
            value="jdbc:oracle:thin:@localhost:1521:orcl">
        </property>
        <property name="username" value="gwb"></property>
        <property name="password" value="gwb"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
            </props>
        </property>
    </bean>
    <bean id="UsersDAO" class="UsersDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean></beans>

  6.2.2.修改后application.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!--添加注解支持  -->
<context:annotation-config></context:annotation-config>
<!-- 添加注解扫描支持 -->
<context:component-scan base-package="com.cn"></context:component-scan>
<!-- 添加事务注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName"
            value="oracle.jdbc.OracleDriver">
        </property>
        <property name="url"
            value="jdbc:oracle:thin:@localhost:1521:orcl">
        </property>
        <property name="username" value="gwb"></property>
        <property name="password" value="gwb"></property>
    </bean>
    <!--配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
           <ref bean="sessionFactory"/>
        </property>
    </bean>
    <!-- 添加HibernateTemplate模板 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory">
           <ref bean="sessionFactory"/>
        </property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.cn.entity.Users</value>
            </list>
        </property>
    </bean>
    <bean id="UsersDAO" class="UsersDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean></beans>

  6.3.修改web.xml文件

    6.3.1修改前

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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-app_3_0.xsd">
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping></web-app>

    6.3.2.修改后

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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-app_3_0.xsd">
<!-- 添加Spring上下文参数 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter>
  <filter-name>OpenSessionInView</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <!-- 添加Spring的上下文环境监听 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
   <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 </web-app>

 

java利用myeclipse自带三大框架搭建三大框架(Hibernate+Struts2+Spring)过程详解

标签:bubuko   注解   图片   oca   name   cdata   cal   3.1   exec   

原文地址:https://www.cnblogs.com/g177w/p/8443050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!