关于SSH框架的一些整合要点
关于ssh框架的一些整合要点。昨天做了ssh框架的整合,但是也是感觉生疏了许多,下面介绍一下ssh整合的具体过程和常见问题的注意事项!
1:首先做struts部分的测试,在src目录导入struts.xml文件,然后在web.xml中添加核心过滤器strutsprepareandexecutefilter
在项目中导入相应的struts,spring,hibernate的jar包
编写一个测试页面index.
<%@ page language="java" import="java.util.*" pageencoding="gb18030"%> <%@taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="s" uri="/struts-tags" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %>
welcome login page
在地址栏中键入一下url,出现如下界面说明struts配置成功!
2:spring和hibernate的整合:
a:建立jdbc.properties文件
b: 在web-inf下创建spring-hibernate.xml文件
/web-inf/spring-hibernate.xml
上面标红的一行的配置文件一定要放好位置哦,如果放到web-inf下会报错的要注意!!com.li.model hibernate.dialect=org.hibernate.dialect.mysqldialect
c:创建user实体类对应中的表tb_user
import javax.persistence.column; 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 { private int id; private string username; private string password; @id @generatedvalue(strategy=generationtype.identity) public int getid() { return id; } public void setid(int id) { this.id = 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; } }
d:建立userdao类 利用hibernate实现对底层数据库的操作
import java.util.arraylist;
import java.util.iterator;
import java.util.list;
import javax.annotation.resource;
import org.hibernate.session;
import org.hibernate.sessionfactory;
import org.hibernate.transaction;
//数据库访问层
import com.li.model.user;
public class userdao {
private sessionfactory sf;
public sessionfactory getsf() {
return sf;
}
//使用spring的自动注入功能
@resource(name="sessionfactory")
public void setsf(sessionfactory sf) {
this.sf = sf;
}
//获取用户列表信息
public list
// todo auto-generated method stub
//打开hibernate的session
list
session session = sf.opensession();
transaction tc = (transaction) session.begintransaction();
list list = session.createquery("from user").list();
for (iterator iterator = list.iterator(); iterator.hasnext();) {
user u = (user) iterator.next();
users.add(u);
}
try {
tc.commit();
} catch (exception e) {
e.printstacktrace();
}
session.close();
return users;
}
}
import javax.annotation.resource;
import org.springframework.stereotype.component;
import org.springframework.stereotype.service;
import com.li.dao.userdao;
import com.li.model.user;
public class userservice {
private userdao userdao;
public userdao getuserdao() {
return userdao;
}
@resource(name="userdao")
public void setuserdao(userdao userdao) {
this.userdao = userdao;
}
//获得用户列表
public list
return userdao.getusers();
}
}
f:建立action,showuseraction
import java.util.list;
import javax.annotation.resource;
import org.springframework.stereotype.controller;
import com.li.model.user;
import com.li.service.userservice;
import com.opensymphony.xwork2.actionsupport;
public class showusersaction extends actionsupport{
//存储接收的user列表信息
private list
//用户服务业务类
private userservice userservice;
public void setusers(list
this.users = users;
}
public list
return users;
}
public userservice getuserservice() {
return userservice;
}
@resource(name="userservice")
public void setuserservice(userservice userservice) {
this.userservice = userservice;
}
//演示用
public string listusers(){
system.out.println("userservice is " + userservice);
this.users = userservice.getusers();
system.out.println("users is " + this.getusers());
if(this.getusers() != null){
return success;
}else{
return error;
}
}
}
g:在struts.xml做如下配置:
h:当然最重要的是struts2-spring-plugins.jar包的导入,没有导入它会出现user is not mapped等等的错误信息
i:在web.xml作如下配置:
好了大功告成ssh整合完毕!文章有不足和错误之处欢迎指正。