zoukankan      html  css  js  c++  java
  • java利用myeclipse自带三大框架搭建三大框架(Hibernate+Struts2+Spring)过程详解

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

      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>
  • 相关阅读:
    初步了解软件工程的概念
    实验十四 团队项目评审&课程学习总结
    201671030125 曾佳+《英文文本统计分析》结对项目报告
    201671030125+词频统计软件项目报告
    201671030125 曾佳 + 实验三作业互评与改进报告
    初识 软件工程
    Enjoy Markdown!
    实验十四 团队项目评审&课程学习总结
    201671030127赵津莹 《英文文本统计分析》结对项目报告
    201671030127词频统计软件项目报告
  • 原文地址:https://www.cnblogs.com/g177w/p/8443050.html
Copyright © 2011-2022 走看看