• Struts2.0+Spring3+Hibernate3(SSH~Demo)


    Struts2.0+Spring3+Hibernate3(SSH~Demo)

    前言:整理一些集成框架,发现网上都是一些半成品,都是共享一部分出来(确实让人很纠结),这是整理了一份SSH的测试案例,完全可以用!

    言归正传,首先强调一点首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活、易于扩展的多层Web应用程序。

    集成SSH框架的系统从职责上分为四层:表示层业务逻辑层数据持久层和域模块层,以帮助开发人员在短期内搭建结构清晰、可复用性好、维护方便的Web应用程序。其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理struts和hibernate。具体做法是:用面向对象的分析方法根据需求提出一些模型,将这些模型实现为基本的Java对象,然后编写基本的DAO(Data Access Objects)接口,并给出Hibernate的DAO实现,采用Hibernate架构实现的DAO类来实现Java类与数据库之间的转换和访问,最后由Spring做管理,管理struts和hibernate。

    整个Demo的视图

    下面是主要的代码模块

    GenerateExcelAction.java

     View Code

    UpdateUserAction.java

     View Code

    User.hbm.xml

     View Code

    UserDAOImpl.java

     View Code

    UserDAO.java

     View Code

    下面是主要的配置文件

    hibernate.cfg.xml

    struts.xml

    复制代码
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    
        <package name="user" extends="struts-default">
    
            <action name="saveUser" class="saveUserAction">
                <result name="success" type="redirect">listUser.action</result>
                <result name="input">/save.jsp</result>
            </action>
            <action name="listUser" class="listUserAction">
                <result>/list.jsp</result>
            </action>
            <action name="deleteUser" class="removeUserAction">
                <result name="success" type="redirect">listUser.action</result>
            </action>
            <action name="updatePUser" class="updatePUserAction">
                <result name="success">/update.jsp</result>
            </action>
            <action name="updateUser" class="updateUserAction">
                <result name="success" type="redirect">listUser.action</result>
                <result name="input">/update.jsp</result>
            </action>
    
            <action name="generateExcel" class="generateExcelAction">
                <result name="success" type="stream">
                    <param name="contentType">application/vnd.ms-excel</param>
                    <param name="contentDisposition">filename="AllUsers.xls"</param>
                    <param name="inputName">downloadFile</param>
                </result>
            </action>
        </package>
    
    </struts>
    复制代码

    applicationContext.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/bkytest"></property>
            <property name="username" value="root"></property>
            <property name="password" value="123456"></property>
            <property name="maxActive" value="100"></property>
            <property name="maxIdle" value="30"></property>
            <property name="maxWait" value="500"></property>
            <property name="defaultAutoCommit" value="true"></property>
        </bean>
    
        <!-- Bean Mapping 映射 -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
            <property name="mappingResources">
                <list>
                    <value>com/talent/example/user/bean/User.hbm.xml</value>
                </list>
            </property>
        </bean>
    
    
    
        <bean id="userDao" class="com.talent.example.user.dao.impl.UserDAOImpl" scope="singleton">
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
        </bean>
    
        <bean id="userService" class="com.talent.example.user.service.impl.UserServiceImpl">
            <property name="userDao" ref="userDao"></property>
        </bean>
    
        <bean id="saveUserAction" class="com.talent.example.user.action.SaveUserAction"
            scope="prototype">
            <property name="service" ref="userService"></property>
        </bean>
    
        <bean id="listUserAction" class="com.talent.example.user.action.ListUserAction"
            scope="prototype">
            <property name="service" ref="userService"></property>
        </bean>
    
        <bean id="removeUserAction" class="com.talent.example.user.action.RemoveUserAction"
            scope="prototype">
            <property name="service" ref="userService"></property>
        </bean>
    
        <bean id="updatePUserAction" class="com.talent.example.user.action.UpdatePUserAction"
            scope="prototype">
            <property name="service" ref="userService"></property>
        </bean>
    
        <bean id="updateUserAction" class="com.talent.example.user.action.UpdateUserAction"
            scope="prototype">
            <property name="service" ref="userService"></property>
        </bean>
    
        <bean id="generateExcelAction" class="com.talent.example.user.action.GenerateExcelAction"
            scope="singleton">
            <property name="service" ref="userService"></property>
        </bean>
    </beans>
    复制代码

    以及整个项目的Web.xml配置文件

    web.xml

    复制代码
    <?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>SSHv1.0</display-name>
        <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.FilterDispatcher
                </filter-class>
          </filter>
          
          <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>
          
          <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
          </welcome-file-list>
          
          <listener>
            <listener-class>
                  org.springframework.web.context.ContextLoaderListener
              </listener-class>
          </listener>
    
    </web-app>
    复制代码

    以及简单的页面效果图

    Demo下载地址:点击下载

     
    分类: Java代码参考
  • 相关阅读:
    day006 数字类型和字符串类型的内置方法
    awk技巧 nginx access.log
    测试访问速度
    ELK Deployed
    jenkins权限控制
    linux 系统优化
    nginx 刷新显示404
    虚拟机安装的ubutun全屏
    在线生成二维码
    GIT 命令集
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4382398.html
Copyright © 2020-2023  润新知