SSH框架的搭建和测试(Spring + Struts2 + Hibernate)

SSH框架实现了视图、控制器、和模型的彻底分离,同时还实现了业务逻辑层与持久层的分离。

Spring实现了MVC中的 Controller的功能,Struts实现Web视图的功能,Hibernate则实现数据模型层的功能对数据进行持久化。

搭建流程:

实现对Spring的支持
实现对hibernate的支持
  --测试Spring+hibernate

实现对struts的支持
  --导包:struts2-spring-plugin-2.3.16.3.jar
  --在web.xml配置文件里配置监听器,服务器一旦启动,就加载Spring容器

  <!-- 监听服务器启动 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 读取spring配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

1、项目结构:

2、安装Spring

3、依赖包(其中包括Spring、Struts2和Hibernate的一些依赖包)

4、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    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_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 监听服务器启动 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 读取spring配置文件 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/applicationContext.xml,classpath:config/applicationContext-dao.xml</param-value>
  </context-param>

  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
      <init-param>
        <param-name>config</param-name>
        <param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
    </init-param>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

  </web-app>

5、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"
    xmlns:p="http://www.springframework.org/schema/p"
    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.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!-- <import resource="config/applicationContext-dao.xml"/> -->

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:config/hibernate.cfg.xml">
        </property>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
    </bean>

    <!-- 注入service -->
    <bean id="userService" class="com.cn.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <!-- 注入dao
    <bean id="userDao" class="com.cn.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>-->

    <!-- 注入action -->
    <bean id="registAction" class="com.cn.action.RegistAction">
        <property name="userService" ref="userService"></property>
    </bean>

    </beans>

也可以在外部注入;

applicationContext-dao.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- 注入dao -->
    <bean id="userDao" class="com.cn.dao.UserDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    </beans>

6、hibernate.cfg.xml

<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/xtkj?characterEncoding=utf-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">CS</property>

        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="connection.autocommit">true</property>

        <mapping class="com.cn.pojo.User"/>
    </session-factory>

</hibernate-configuration>

7、struts.xml(在web.xml配置文件里配置监听器,服务器一旦启动,就加载Spring容器):

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

    <package name="test" extends="struts-default">
        <action name="registAction_*" class="registAction" method="{1}">
            <result name="welcome">/welcome.jsp</result>
        </action>
    </package>

</struts>    

测试:

8、pojo封装数据层:User.java

package com.cn.pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="tb_user")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String name;
    private String password;
    private String address;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public User(String name, String password, String address) {
        super();
        this.name = name;
        this.password = password;
        this.address = address;
    }
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
}

9、dao数据持久化层:

UserDao.java:

package com.cn.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cn.pojo.User;

public class UserDao extends HibernateDaoSupport implements UserDAOInterf{

    public void save(User user){
        this.getHibernateTemplate().save(user);
    }

}

UserDAOInterf.java:

package com.cn.dao;

import com.cn.pojo.User;

public interface UserDAOInterf {

    public void save(User user);

}

10、service业务逻辑层:

UserService.java:

package com.cn.service;

import org.springframework.transaction.annotation.Transactional;

import com.cn.dao.UserDAOInterf;
import com.cn.dao.UserDao;
import com.cn.pojo.User;

public class UserService implements UserServiceInterf{

    private UserDAOInterf userDao;

    @Transactional
    public void save(User user){
        userDao.save(user);
    }

    public UserDAOInterf getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDAOInterf userDao) {
        this.userDao = userDao;
    }

}

UserServiceInterf.java:

package com.cn.service;

import com.cn.pojo.User;

public interface UserServiceInterf {

    public void save(User user);

}

11、action控制器层(负责业务逻辑层与表现层的交互):RegistAction.java

package com.cn.action;

import com.cn.pojo.User;
import com.cn.service.UserService;
import com.cn.service.UserServiceInterf;

public class RegistAction {

    private User user;
    private UserServiceInterf userService;

    public String regist(){
        userService.save(user);
        return "welcome";
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserServiceInterf getUserService() {
        return userService;
    }

    public void setUserService(UserServiceInterf userService) {
        this.userService = userService;
    }
}

12、regist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP ‘regist.jsp‘ starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <form action="registAction_regist">
        用户名:<input type="text" name="user.name"/><br/>
        密码:<input type="password" name="user.password"/><br/>
        地址:<input type="text" name="user.address"/><br/>
        <input type="submit" value="注册"/>
    </form>
  </body>
</html>
时间: 2024-10-13 08:32:32

SSH框架的搭建和测试(Spring + Struts2 + Hibernate)的相关文章

Struts2+Spring+Hibernate(SSH)框架的搭建

首先需要下载struts2 ,spring,hibernate  的资源包; Struts2资源包下载路径:http://www.apache.org/spring资源包下载路径:http://projects.spring.io/spring-framework/hibernate资源包下载路径:http://hibernate.org/orm/downloads/ 在SSH框架的搭建步骤: 第一步:在eclipse中创建一个web项目,并生成web.xml文件; 第二步:往lib目录导入ja

简单Spring+Struts2+Hibernate框架搭建

使用Maven+Spring+Struts2+Hibernate整合 pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd

Maven搭建Spring+Struts2+Hibernate项目详解

前言 这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架,但是Spring也提供和其他框架的无缝整合,采用组件形式对个框架进行管理,项目实例是按照真实企业里面的开发搭建,也是web的最后一片了.数据库使用mysql,连接池使用的是Druid数据源(这些都无关紧要,可以随时的替换),下面就将详细的介绍一下Maven搭建Spring,Struts2,和hibernation的步奏. 1.数据库设计 数据库库表

SSH(Spring+Struts2+Hibernate)整合

本博文介绍目前较流行的三大(Spring+Struts2+Hibernate)框架的整合. 一般整合需要以下几个步骤: 1.首先导入相应的jar包 Spring所需的jar包如下图: Struts所需的jar包如下图: hibernate所需的jar包如下图: 一些共同所需的jar包如下图: 其中mysql-connector-java-5.1.33-bin.jar是连接mysql数据库所需的jar包. 将上述的jar包拷贝到项目的lib目录下. 2.spring和struts整合 只需要Str

java开发SSM框架的搭建(SpringMVC+Spring+MyBatis)

由于某些原因,重装系统,java-web开发虽然顺手,但烦人的一点是开发环境的搭建.差不多折腾了一整天,笔者在这里把SSM开发环境的搭建重新清理一遍,以飨读者,也供自己以后参考.善于总结,是做好每份工作必备的素质之一. 1安装java虚拟机-JDK(1.7_51) 详见笔者博文:http://blog.csdn.net/gisredevelopment/article/details/24304085 2 安装MyEclipse(2014专业版) 下载地址:http://www.myeclips

SSH(Spring+struts2+hibernate)整合版详解

Spring+Struts2+hibernate框架整合的步骤: 开发工具:idea+Oracle数据库 一:引入相关的依赖jar包 <!--oracle驱动--><!-- https://mvnrepository.com/artifact/com.oracle/ojdbc14 --><dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactI

Spring+Struts2+Hibernate的整合

这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形式对个框架进行管理,项目实例是按照真实企业里面的开发搭建,也是web的最后一片了.数据库使 用mysql,连接池使用的是Druid数据源(这些都无关紧要,可以随时的替换),下面就将详细的介绍一下Maven搭建 Spring,Struts2,和hibernation的步奏. 1.数据库设计 数据库库表

spring+struts2+hibernate 最精简jar包引用

转载自:http://blog.csdn.net/thomas0yang/article/details/6035679 1.spring(6个 ) IoC * SPRING_HOME/dist/spring.jar     * SPRING_HOME/lib/log4j/log4j-1.2.14.jar     * SPRING_HOME/lib/jakarta-commons/commons-logging.jar * SPRING_HOME/lib/junit/junit.jar (可选)

ssh框架简单搭建

这里是个人对SSH框架搭建的一点心得,仅供新手,勿喷 首先,搞清楚分层, 视图层 --> 控制层  --> 业务层 --> DAO层--> 持久层 搭建的顺序是从后向前,搭建一点测试一点,免得都最后找错误太过于繁琐 先搭建持久层,这里是hibernate框架接管 1.建立javaBean对象(ElecText.jvav)   属于持久层对象(PO对象)   属性ID.名称.日期.备注2.创建映射文件ElecText.hbm.xml   建立PO对象与数据库表Elec_Text的关联