zoukankan      html  css  js  c++  java
  • 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

     1 package com.talent.example.user.action;
     2 import java.io.InputStream;
     3 import com.opensymphony.xwork2.ActionSupport;
     4 import com.talent.example.user.service.UserService;
     5 /**
     6  * <p>Title:GenerateExcelAction</p>
     7  * <p>Description: 导出Exel</p>
     8  * <p>Copyright: Copyright (c) VISEC 2015</p>
     9  * <P>CreatTime: Mar 31 2015 </p>
    10  * @author  Dana丶Li
    11  * @version 1.0
    12  */
    13 public class GenerateExcelAction extends ActionSupport {
    14     private static final long serialVersionUID = 1L;
    15     
    16     private UserService service;
    17 
    18     public UserService getService() {
    19         return service;
    20     }
    21 
    22     public void setService(UserService service) {
    23         this.service = service;
    24     }
    25     
    26     public InputStream getDownloadFile()
    27     {
    28         return this.service.getInputStream();
    29     }
    30     
    31     @Override
    32     public String execute() throws Exception {
    33         
    34         return SUCCESS;
    35         
    36     }
    37     
    38 }
    View Code

    UpdateUserAction.java

     1 package com.talent.example.user.action;
     2 import com.opensymphony.xwork2.ActionSupport;
     3 import com.talent.example.user.bean.User;
     4 import com.talent.example.user.service.UserService;
     5 /**
     6  * <p>Title:UpdatePUserAction</p>
     7  * <p>Description:修改User信息Action</p>
     8  * <p>Copyright: Copyright (c) VISEC 2015</p>
     9  * <P>CreatTime: Mar 31 2015 </p>
    10  * @author  Dana丶Li
    11  * @version 1.0
    12  */
    13 public class UpdateUserAction extends ActionSupport {
    14     private static final long serialVersionUID = 1L;
    15     private User user;
    16     private UserService service;
    17 
    18     public User getUser() {
    19         return user;
    20     }
    21     public void setUser(User user) {
    22         this.user = user;
    23     }
    24     public UserService getService() {
    25         return service;
    26     }
    27     public void setService(UserService service) {
    28         this.service = service;
    29     }
    30 
    31     @Override
    32     public String execute() throws Exception {
    33 
    34         this.service.update(user);
    35 
    36         return SUCCESS;
    37     }
    38 }
    View Code

    User.hbm.xml

     1 <?xml version='1.0' encoding='UTF-8'?>
     2  <!DOCTYPE hibernate-mapping PUBLIC
     3            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping>
     7 
     8     <class name="com.talent.example.user.bean.User" table="users">
     9         <id name="id" type="java.lang.Integer" column="id">
    10             <generator class="increment"></generator>
    11         </id>
    12         <property name="firstname" type="string" column="firstname"
    13             length="50"></property>
    14         <property name="lastname" type="string" column="lastname"
    15             length="50"></property>
    16         <property name="age" type="java.lang.Integer" column="age"></property>
    17     </class>
    18 
    19 </hibernate-mapping>
    View Code

    UserDAOImpl.java

     1 package com.talent.example.user.dao.impl;
     2 import java.util.List;
     3 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
     4 import com.talent.example.user.bean.User;
     5 import com.talent.example.user.dao.UserDAO;
     6 /**
     7  * <p>Title:UserDAOImplr</p>
     8  * <p>Description:UserDAO实现类</p>
     9  * <p>Copyright: Copyright (c) VISEC 2015</p>
    10  * <P>CreatTime: Mar 31 2015 </p>
    11  * @author  Dana丶Li
    12  * @version 1.0
    13  */
    14 public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
    15 
    16     @SuppressWarnings("unchecked")
    17     public List<User> findAllUser() {
    18 
    19         String hql = "from User user order by user.id desc";
    20 
    21         return (List<User>)this.getHibernateTemplate().find(hql);
    22 
    23     }
    24 
    25     public User findUserById(Integer id) {
    26 
    27         User user = (User)this.getHibernateTemplate().get(User.class, id);
    28 
    29         return user;
    30     }
    31 
    32     public void removeUser(User user) {
    33 
    34         this.getHibernateTemplate().delete(user);
    35 
    36     }
    37 
    38     public void saveUser(User user) {
    39 
    40         this.getHibernateTemplate().save(user);
    41 
    42     }
    43 
    44     public void updateUser(User user) {
    45 
    46         this.getHibernateTemplate().update(user);
    47 
    48     }
    49 
    50 }
    View Code

    UserDAO.java

     1 package com.talent.example.user.dao;
     2 import java.util.List;
     3 import com.talent.example.user.bean.User;
     4 /**
     5  * <p>Title:UserDAO</p>
     6  * <p>Description:UserDAO接口</p>
     7  * <p>Copyright: Copyright (c) VISEC 2015</p>
     8  * <P>CreatTime: Mar 31 2015 </p>
     9  * @author  Dana丶Li
    10  * @version 1.0
    11  */
    12 public interface UserDAO {
    13     public void saveUser(User user);
    14     
    15     public void removeUser(User user);
    16     
    17     public User findUserById(Integer id);
    18     
    19     public List<User> findAllUser();
    20     
    21     public void updateUser(User user);
    22 }
    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下载地址:点击下载

  • 相关阅读:
    19 个必须知道的 Visual Studio 快捷键
    面试感悟:一名3年工作经验的程序员应该具备的技能
    来自开发者技术前线 高级程序员,你需要养成这7个习惯
    来自极客头条的 15个常用的javaScript正则表达式
    来自极客头条的 35 个 Java 代码性能优化总结
    [Visual studio] Visual studio 2017添加引用时报错未能正确加载ReferenceManagerPackage包的解决方法
    [Visual Studio] 未能完成操作 不支持此接口
    未能加载文件或程序集“Benlai.SOA.Framework.Common, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。系统找不到指定的文件。
    [Visual Studio] NuGet发布自定义包(Library Package)
    未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配。例如,当以动态方式添加控件时,在回发期间添加的控件必须与在初始请求期间添加的控件的类型和位置相匹配
  • 原文地址:https://www.cnblogs.com/visec479/p/4380210.html
Copyright © 2011-2022 走看看