SSH2框架整合(Struts2.1.6+hibernate3.3.2+spring2.5.6)
其实整合也不是很麻烦,大家只要有个整合的思路就可以了,这里我个人认为,整合的话先
spring >> hibernate >> struts 这样一步步下去比较容易点(纯属我个人认为,不认同的也没关系,顺序随便大家怎么样加入都是没有关系的,不废话了,看下面。。。。。。)
---------------------------------------------------------------------------------------------------------------------------------
设计好数据库(也不一定要的,那个就先不说了),这里使用mysql数据库,相关信息如下(数据库名:ssh2_demo,表就一张userinfo)
1.先是建立一个web项目,我这里命名为:SSH2_DEMO_1 (大家喜欢随便怎么命名都ok)
2. 先加入Spring的需要的jar跟spring的配置文件xml,如下图所示(没有可以到官网去下载)
Spring需要的jar: http://pan.baidu.com/share/link?shareid=309734&uk=4245483071
然后在项目的web.xml里面加入spring的信息
至此,spring算是ok了
---------------------------------------------------------------------------------------------------------------------------------3.加入hibernate的需要的jar,如下图所示(没有可以到官网去下载,hibernate也是有配置的xml的,不过我这里整合到applicationContext.xml里面去了,所有看不到hibernate的那个配置文件hibernate.cfg.xml) 不要少了mysql的驱动啊
hibernate需要的jar: http://pan.baidu.com/share/link?shareid=309727&uk=4245483071
mysql 的驱动包 : http://pan.baidu.com/share/link?shareid=309731&uk=4245483071
然后在applicationContext.xml 里面加入数据源的配置信息(我这里是用mysql数据库,就要写上对应数据库的方言什么的)
到这里执行项目如果没有问题的就继续往下走。。。
4.建立实体类Userinfo.java 跟hibernate的映射文件Userinfo.hbm.xml(命名不要错了实体类名.hbm.xml)
Userinfo.java
package cn.wzqiang.dto;
public class Userinfo {
privateint id;
privateString username;
privateString password;
publicUserinfo() {
}
publicUserinfo(String username, String password) {
this.username= username;
this.password= password;
}
publicint getId() {
returnthis.id;
}
publicvoid setId(int id) {
this.id= id;
}
publicString getUsername() {
returnthis.username;
}
publicvoid setUsername(String username) {
this.username= username;
}
publicString getPassword() {
returnthis.password;
}
publicvoid setPassword(String password) {
this.password= password;
}
}
Userinfo.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
然后在applicationContext.xml加上该信息(红色字体那)
5.有了这些自然就要有增删改查这些方法了,编写一个接口UserInfo_dao 和写一个Userinfo_impl类实现这个接口
UserInfo_dao.java
package cn.wzqiang.dao;
import java.util.List;
import cn.wzqiang.dto.Userinfo;
public interface UserInfo_dao {
/**
* 用户登录
* @param uname , upwd
* @return
*/
boolean LoginInfo(String uname ,String upwd);
/**
* 添加信息
* @param info
* @return
*/
boolean AddInfo(Userinfo info);
/**
* 删除
* @param info
* @return
*/
boolean DeleteInfo(Userinfo info);
/**
* 修改
* @param info
* @return
*/
boolean UpdateInfo(Userinfo info);
/**
* 根据ID得到对应的信息
* @param id
* @return
*/
Userinfo QueryInfo(int id);
/**
* 查询全部
* @return
*/
List
}
Userinfo_impl.java
package cn.wzqiang.dao.impl;
import java.util.List;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import cn.wzqiang.dao.UserInfo_dao;
import cn.wzqiang.dto.Userinfo;
public class Userinfo_impl implementsUserInfo_dao {
//注入SessionFactory
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public boolean AddInfo(Userinfo info) {
// TODO Auto-generated method stub
try {
sessionFactory.getCurrentSession().persist(info);
return true;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public boolean DeleteInfo(Userinfo info) {
// TODO Auto-generated method stub
try {
sessionFactory.getCurrentSession().delete(info);
return true;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public boolean UpdateInfo(Userinfo info) {
// TODO Auto-generated method stub
try {
sessionFactory.getCurrentSession().merge(info);
return true;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public Userinfo QueryInfo(int id) {
// TODO Auto-generated method stub
Userinfo info = new Userinfo();
info = (Userinfo)sessionFactory.getCurrentSession().createQuery("fromUserinfo where id = "+id) .uniqueResult();;
return info;
}
public List
// TODO Auto-generated method stub
List
list =sessionFactory.getCurrentSession().createQuery("fromUserinfo").list();
return list;
}
@SuppressWarnings("rawtypes")
public boolean LoginInfo(String uname ,String upwd) {
// TODO Auto-generated method stub
try {
List list = sessionFactory.getCurrentSession().createQuery("fromUserinfo where username = '"+uname+"' and password ='"+upwd+"'").list();
if(list.size()!=0){
return true;
}else{
return false;
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
6.学过hibernate的都知道操作数据库一般要进行事务处理(查询不用),这里这个事务处理可以由spring来进行管理(添加红色字体到applicationContext.xml)
---------------------------------------------------------------------------------------------------------------------------------运行ok,没问题
7.加入struts2的需要的jar跟配置信息struts.mxl,如下图所示(没有可以到官网去下载struts2)
Struts2需要的jar: http://pan.baidu.com/share/link?shareid=309737&uk=4245483071
修改web.xml信息,添加struts2的信息进去
-------------------------------------------------------------------------------------------------------------------------------------------------
运行ok,可以跑^_^一个基本的ssh2算是ok了
------
写两个action 一个登录,一个useinfo的增删改查
LoginAction.java
package cn.wzqiang.action;
import java.util.List;
importorg.apache.struts2.ServletActionContext;
import cn.wzqiang.dao.UserInfo_dao;
import cn.wzqiang.dto.Userinfo;
public class LoginAction {
/**
* 注入Userinfo_impl
*/
privateUserInfo_dao userinfo_impl;
publicvoid setUserinfo_impl(UserInfo_dao userinfo_impl) {
this.userinfo_impl= userinfo_impl;
}
publicString userLogin(){
Stringuname = ServletActionContext.getRequest().getParameter("uname");//接收传过来的用户名
Stringupwd = ServletActionContext.getRequest().getParameter("upwd");//接收传过来的用户密码
booleanb = false;
try{
b= userinfo_impl.LoginInfo(uname, upwd);
System.out.println("你们祈祷不要是乱码啊:^_^"+uname+";"+upwd+";>>>>"+b);
if(b==true){
List
ServletActionContext.getRequest().getSession().setAttribute("listinfos",list);
return"loginok";
}else{
return"loginerror";
}
}catch (Exception e) {
e.printStackTrace();
return"loginerror";
}
}
}
package cn.wzqiang.action;
importorg.apache.struts2.ServletActionContext;
import cn.wzqiang.dao.UserInfo_dao;
import cn.wzqiang.dto.Userinfo;
public class UserinfoAction {
/**
* 注入Userinfo_impl
*/
privateUserInfo_dao userinfo_impl;
publicvoid setUserinfo_impl(UserInfo_dao userinfo_impl) {
this.userinfo_impl= userinfo_impl;
}
privateString msg;
publicString getMsg() {
returnmsg;
}
publicvoid setMsg(String msg) {
this.msg= msg;
}
/**
* 添加用户
* @return
*/
publicString AddUserinfo(){
returnnull;
}
/**
* 删除用户
* @return
*/
publicString DeleteUserinfo(){
intid=Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
try{
Userinfoinfo = userinfo_impl.QueryInfo(id);
booleanb = userinfo_impl.DeleteInfo(info);
if(b){
msg= "删除成功!";
}else{
msg= "操作删除失败!";
}
}catch (Exception e) {
e.printStackTrace();
}
return"json";
}
/**
* 修改用户
* @return
*/
publicString UpdateUserinfo(){
return"json";
}
/**
* 查询所有用户
* @return
*/
publicString QueryAllUserinfo(){
returnnull;
}
/**
* 根据ID查询用户信息
* @return
*/
publicString QueryById(){
returnnull;
}
}
设置好struts.xml相关信息
—这里
----------------------
applicationContext.xml
加入以下信息
-----------------
Index.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%
String path =request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<basehref="<%=basePath%>"/>
<title>这是个首页title>
<metahttp-equiv="pragma"content="no-cache"/>
<metahttp-equiv="cache-control"content="no-cache"/>
<metahttp-equiv="expires"content="0"/>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"/>
<metahttp-equiv="description"content="This is my page"/>
head>
<body>
<divstyle="margin: 0 auto; width:200px;">
<formaction="<%=basePath%>login/ulogin!userLogin"method="post">
UserName:<inputname="uname"type="text"value=""style="width:140px; height:22px;"/><br/>
PassWord:<inputname="upwd"type="password"value=""style="width:140px; height:22px;"/><br/><br/>
<inputtype="submit"value="登 陆"/>
form>
div>
body>
html>
由于我使用到了jstl表达式,加入对于的jar
Jstl 的jar: http://pan.baidu.com/share/link?shareid=309742&uk=4245483071
Ok.jsp
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s"uri="/struts-tags" %>
<%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
$(function(){
//删除
$(".delete").click(function(){
var uid =$(this).attr("id");
$.ajax({
type:'POST',
url:'<%=basePath%>loginIsok/loginok_DeleteUserinfo',
data:{id:uid},
success:function(data){
alert(data.msg);
if(data.msg.length> 4){
$(".contents"+uid).remove();
}
},
error:function(){
alert("Error");
}
});
});
//修改
$(".update").click(function(){
alert("由于例子简单,所以就不写这个功能了@_@");
});
//鼠标移动变色
$(".contents").each(function(){
$(this).hover(function(){
$(this).css({"background-color":"#333333","cursor":"pointer"});
},function(){
$(this).css("background-color","#666633");
});
});
});
序号 用户名 操作 ${status.index+1} ${infos.username}
------------------------------------------------------------------------------------------------------------------------------------------------------------
如果有什么不对的,请大家指正,一起学习,
源码有空再上传出来给大家,谢谢了。。。。
本文来源:https://www.2haoxitong.net/k/doc/942397f2aef8941ea76e0535.html
文档为doc格式