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>

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

  1. ssh 框架整合试例 (spring+struts2+hibernate)

    1.首先用Eclipse创建一个web项目(Eclipse EE 版) new->Other-> 输入web 然后选择Dynamic Web Project->next-> 输 ...

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

    首先需要下载struts2 ,spring4,hibernate5  的资源包; struts2资源包下载路径:http://www.apache.org/spring资源包下载路径:http://p ...

  3. SSH(Spring Struts2 Hibernate)框架整合(注解版)

    案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 框架:Spring Struts2  Hibernate 案例架构: 1.依赖jar包 pom.xml < ...

  4. 简单Spring+Struts2+Hibernate框架搭建

    使用Maven+Spring+Struts2+Hibernate整合 pom文件 <project xmlns="http://maven.apache.org/POM/4.0.0&q ...

  5. Spring+Struts2+Hibernate的整合

    这篇主要采用Maven搭建Spring+Struts2+Hibernate的整合项目,复习一下SSH框架,虽然spring提供自己的MVC框架, 但是Spring也提供和其他框架的无缝整合,采用组件形 ...

  6. [Java web]Spring+Struts2+Hibernate整合过程

    摘要 最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh. 环境 工具IDE :Idea 2018 数据库 ...

  7. struts2+spring+hibernate(SSH)框架的搭建和总结

    SSH框架:struts2+spring+hibernate,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. struts2+spring+hibernat ...

  8. SSH框架之Spring+Struts2+Hibernate整合篇

    回顾 -Hibernate框架 ORM: 对象关系映射.把数据库表和JavaBean通过映射的配置文件映射起来, 操作JavaBean对象,通过映射的配置文件生成SQL语句,自动执行.操作数据库. 1 ...

  9. spring+struts2+hibernate框架搭建(Maven工程)

    搭建Spring 1.porm.xml中添加jar包 <!-- spring3 --> <dependency> <groupId>org.springframew ...

随机推荐

  1. [luogu3620][APIO/CTSC 2007]数据备份【贪心+堆+链表】

    题目描述 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽享计算机游戏 ...

  2. BZOJ5093图的价值(斯特林数)

    题目描述 “简单无向图”是指无重边.无自环的无向图(不一定连通). 一个带标号的图的价值定义为每个点度数的k次方的和. 给定n和k,请计算所有n个点的带标号的简单无向图的价值之和. 因为答案很大,请对 ...

  3. 20165223《Java程序设计》第七周Java学习总结

    教材学习内容总结 第11章-JDBC与MySQL数据库 要点 MySQL数据库管理系统 连接MySQL数据库 查询操作(基础) 更新.添加.删除(基础) 预处理语句(重点) 通用查询(难点) 事务 笔 ...

  4. springAop 使用@Around,@After等注解时,代码运行两边的问题

    springAop使用@Around,@After等注解时,代码运行两边的问题 将@Component注解删掉就好了

  5. C# Winform ListView控件

    一.ListView: 1.视图改为为Detalis: 2.编辑列,每添加一个添加一列,右侧属性Text改列名,停靠位置,列头的长度等等: 3.右侧属性,点开Iteme,添加ListViewItem集 ...

  6. poj3889 fractal streets

    分形街道 我干,这个毒瘤. 想起来就头痛. 首先看题就是一大难题...... 说一下题目大意吧. 每当n+1时,把n阶图复制为4份.2*2排好. 右边两个不动.左上顺时针旋转90°,左下逆时针旋转90 ...

  7. 洛谷P3709 大爷的字符串

    题意:多次求区间众数的出现次数. 解: 这题居然可以莫队...... 首先开个桶.然后还要开个数组,cnt[i]表示出现i次的数有多少个. 然后就可以O(1)修改了. #include <cst ...

  8. 【洛谷P1303 A*B Problem】

    题目描述 求两数的积. 输入输出格式 输入格式: 两行,两个数. 输出格式: 积 输入输出样例 输入样例#1: 1 2 输出样例#1: 2 说明 每个数字不超过10^2000,需用高精 emm,显然本 ...

  9. iconv: iconv_open(pToCharset, pFromCharset); 的附加参数//IGNORE

    今天在转换一个文件时iconv() 老是返回 -1, 提示编码转换失败. 一共 30 多个文件, 原编码都是一样的,为什么有的转换会失败,返回 -1呢? 网上搜索了一下, 找到一个随加参数: //IG ...

  10. How MVC pattern Flows

    以上MVC流程中Model和View不存在依赖关系 以上MVC流程View和Model存在耦合关系(依赖关系越少越好)