SSH(Struts、Spring、Hibernate)三大框架整合
1. 新建数据库ssh_db -> 新建表user_tb(id为主键,自动递增)
2. 导入jar包(struts、hibernate 和 spring)
3. 注册页面reg.jsp,将表单的 action 属性设置为 handleAction,input 元素的 name 属性值加上前缀“user.”,如user.username
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding= "UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org
/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>账号注册</title>
</head>
<body>
<form action="handleAction" method="post">
账 号:<input name="user.username"/><br/> <br/>
密 码:<input type="password" name="user.password"/><br/> <br/>
<input type="submit" value="注册"/> <input type="submit" value="登录">
</form>
</body>
</html>
4.数据处理及输出页面handledata.jsp,当Action中返回的user对象为空时,页面输出“注册失败”,不为空时输出注册信息
<%@page import="java.sql.*"%>
<%@page import="indi.wt.ssh.pojo.*"%>
<%@page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>数据处理</title>
</head>
<%@ taglib prefix="s" uri="/struts-tags" %>
<body>
<%
User user = (User) request.getAttribute("user");
if (user != null) {
out.print("<p>恭喜你,注册成功!以下是你的注册信息:</>");
out.print("<p>账号:" + user.getUsername() + "</>");
out.print("<p>密码:" + user.getPassword() + "</>");
} else {
out.print("<p>注册失败!</>");
}
%>
</body>
</html>
5.实体层:根据数据库表结构生成 User 类,并生成对应字段的 set 和 get 方法(id为主键,生成策略为自动增长,username和password为属性)-> 加hibernate注解如下
package indi.wt.ssh.pojo; import javax.persistence.*;
import org.hibernate.annotations.Where; @Entity
@Table(name="user_tb")
@Where(clause = "")
public class User {
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) //主键生成策略
private int id; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
6. 数据访问层,加入insertUser和getUser方法
package indi.wt.ssh.dao; import indi.wt.ssh.pojo.User; import org.hibernate.*;
import org.hibernate.cfg.*; public class UserDao {
public int insertUser(User user) {
//System.out.println("insertUser");
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
sf.close();
return user.getId();
} public User getUser(int id) { User user = null;
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction(); Object obj = session.createQuery("from User u where u.id=" + id).uniqueResult();
tx.commit();
user = (User) obj; session.close();
sf.close();
return user;
}
}
7. Action层:注入User和UserDao对象 -> 生成相应地get和set方法 -> 重写execute方法
package indi.wt.ssh.action; import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport; import indi.wt.ssh.dao.UserDao;
import indi.wt.ssh.pojo.User; public class HandleDataAction extends ActionSupport { User user = null;
UserDao userDao = null; public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public String execute() {
if(userDao == null) System.out.println("userdao = null");
int id = userDao.insertUser(user);
System.out.println("id = "+id);
if (id > 0) {
ServletActionContext.getRequest().setAttribute("user",user);
return SUCCESS;
} else {
return "error";
} } }
8. 配置web.xml,加入Spring监听器和Struts过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SSHIntegrationPrj</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
9. 配置struts文件struts.xml,放在src目录下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings> <action name="RegAction" >
<result>/reg.jsp</result>
</action> <action name="handleAction" class="indi.wt.ssh.action.HandleDataAction">
<result name="success">/handledata.jsp</result>
</action> </package> </struts>
10. 配置hibernate文件hibernate.cfg.xml,放在src目录下
<?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">
<hibernate-configuration>
<session-factory>
<!-- 数据库JDBC驱动类名 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh_db</property>
<property name="connection.username">wt</property>
<property name="connection.password">123456</property> <!-- 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- ddl语句自动建表 -->
<property name="hbm2ddl.auto">none</property>
<!--输出sql语句 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 连接池配置 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="automaticTestTable">Test</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">120</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<property name="c3p0.idleConnectionTestPeriod">18000</property>
<property name="c3p0.maxIdleTime">25000</property>
<property name="c3p0.idle_test_period">120</property> <!-- 注册ORM映射文件 -->
<mapping class="indi.wt.ssh.pojo.User"/>
</session-factory>
</hibernate-configuration>
11. 配置spring文件applicationContext.xml,放在web-inf目录下 (因UserDao和HandleDataAction之间存在依赖关系,故将到 dao 对象的配置放在前面。特别要注意的是,系统默认采用的自动装配策略是 byName 方式,所以此处 dao 的 id 值必须和 action 中的属性名一致,action 的 id 值要和 Struts.xml 文件中配置的 action 名一致)
<?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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置一个 bean -->
<bean id="userDao" class="indi.wt.ssh.dao.UserDao"></bean>
<bean id="handleAction" class="indi.wt.ssh.action.HandleDataAction"></bean>
</beans>
SSH(Struts、Spring、Hibernate)三大框架整合的更多相关文章
- 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
hibernate工作原理 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6.提 ...
- [转] 浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
hibernate工作原理 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6 ...
- Struts,spring,hibernate三大框架的面试
Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...
- Struts,Spring,Hibernate三大框架的
1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Session 4.创建事务Transation 5.持 ...
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...
- 用eclipse搭建SSH(struts+spring+hibernate)框架
声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...
- Struts2,Spring, Hibernate三大框架SSH的整合步骤
整合步骤 创建web工程 引入相应的jar包 整合spring和hibernate框架 编写实体类pojo和hbm.xml文件 编写bean-base.xml文件 <!-- 1) 连接池实例 - ...
- ssh整合思想初步 structs2 Spring Hibernate三大框架各自要点
Web层用Structs2的action Service层用Spring的IoC和aop以及JdbcTemplate或者Transaction事务(创建对象及维护对象间的关系) Dao层用Hibern ...
- java 的 struts2 Spring Hibernate 三大框架的整合
原理就不说了,直接上配置文件及代码,用来备用 首先,将三大框架所需要的jar包导入项目中 导入 struts2-spring-plugin-2.3.3.jar包 此包的作用是作为struts2 与 ...
随机推荐
- SP4487 GSS6 - Can you answer these queries VI
题目大意 给出一个由N个整数组成的序列A,你需要应用M个操作: I p x 在 p 处插入插入一个元素 x D p 删除 p 处的一个元素 R p x 修改 p 处元素的值为 x Q l r 查询一 ...
- MT【81】含参数三次函数因式分解
解答: 评:这题实质上是对关于$x$的三次函数进行了一个因式分解.这种把$a$看成主元的技巧是初中处理高次的因式分解的常用技巧.如果用三次求导去做计算量比较大,要计算极值.
- 【题解】 bzoj4472: [Jsoi2015]salesman (动态规划)
bzoj4472,懒得复制,戳我戳我 Solution: 题面意思:从\(1\)号节点出发,每到一个节点就必须停下,获得节点权值(每个节点只会获得一次),每个点有个规定的停留次数,求最大可获得多大权值 ...
- 【题解】 [HNOI2002]营业额统计 (Splay)
懒得复制,戳我戳我 Solution: \(Splay\)板子题,注意可以选择相等大小 Code: //It is coded by Ning_Mew on 4.10 #include<bits ...
- 【BZOJ4477】[JSOI2015]字符串树(Trie树)
[BZOJ4477][JSOI2015]字符串树(Trie树) 题面 BZOJ 题解 对于每个点维护其到根节点的所有字符串构成的\(Trie\),显然可持久化一下就很好写了. 然后每次询问就是\(u+ ...
- KEIL5.25生成.bin文件步骤
添加.bin文件转换工具 KEIL5的自带.bin文件转化工具在安装目录下:我的安装目录是C盘即,C:\Keil_v5\ARM\ARMCC\bin\fromelf.exe 添加格式为:[C:\Keil ...
- linux命令总结mpstat命令
简介 mpstat是Multiprocessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查 ...
- 标准误(Standard Error)
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- bzoj千题计划287:bzoj1228: [SDOI2009]E&D
http://www.lydsy.com/JudgeOnline/problem.php?id=1228 打SG函数表,找规律: 若n是奇数m是奇数,则SG(n,m)=0 若n是偶数m是偶数,则SG( ...
- hdu 2433 Travel
http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...