SSH Java三大框架整合(Struts 2+Hibernate+Spring+mysql)

以前一直整合Struts 1.x,用Struts1.x做一些简单的项目。最近把Struts 2学了,感觉两者区别挺大的,自己学完就想整点东西出来,然后就把Spring 、Hibernate、Struts 2整合一下。

------------IT行业苦逼的孩子,希望大牛指导

一、集成环境

Struts 2  、Spring 2.5 、Hibernate 3.3

项目开始首先应该从官网上把这三个的框架下载了,方便开发的时候导入jar,以及阅读文档以及API。

二、开始整合

1.业务逻辑

使用这三种框架做一个MIS型系统的用户登录与注册。

2.添加Spring 2.5支持

(1)把Spring 2.5的jar文件添加到项目下WEB-INF下的lib文件夹

(2)配置spring所需要的配置文件beans.xml(将数据源配置好,此处使用mysql数据库)

配置文件如下:



	
	
	
	
		
		
		
		
		
		
	
	
		
			
		
		
			
				hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
			
		
		
			
				zzuli/itstudio/ssh2/bean/User.hbm.xml
			
		
	
	
	
		
	
	
	
上面文件需要注意的就是对spring管理事务的配置

(3)对业务bean的配置(User.java)

package zzuli.itstudio.ssh2.bean;

public class User {
	
	private String username;
	private String password;
	private String sex;
	private int age;

	public User() {
	}

	public User(String username, String password, String sex, int age) {
		super();
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.age = age;
	}
	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 String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}		
}

(4)添加Hibernate支持所需要的jar文件,并配置User.hbm.xml文件,文件配置如下:





	
		
		
		
		
	

(5)书写业务接口UserService

package zzuli.itstudio.ssh2.service;

import java.util.List;

import zzuli.itstudio.ssh2.bean.User;

public interface UserService {
	public void save(User user);
	public void update(User user);
	public User find(String username);
	public void delete(String... username);
	public List list();  
}

(6)书写业务实现类UserServiceBean

package zzuli.itstudio.ssh2.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import zzuli.itstudio.ssh2.bean.User;
import zzuli.itstudio.ssh2.service.UserService;

@Service
@Transactional
public class UserServiceBean implements UserService {

	@Resource
	SessionFactory sessionFactory;

	public void delete(String... usernames) {
		for (String username : usernames) {
			sessionFactory.getCurrentSession().delete(
					sessionFactory.getCurrentSession().load(User.class,
							username));
		}
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public User find(String username) {
		return (User) sessionFactory.getCurrentSession().get(User.class, username);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List list() {
		return sessionFactory.getCurrentSession().createQuery("from User")
				.list();
	}

	public void save(User user) {
		sessionFactory.getCurrentSession().persist(user);
	}

	public void update(User user) {
		sessionFactory.getCurrentSession().merge(user);
	}

}

以上所述已经把Spring和Hibernate集成完毕,自己可以写Juint,进行测试

(7)添加Struts2的支持所需要的jar文件,并配置struts.xml,内容如下:




	
	
	
	
	
	
	
		
		
			/WEB-INF/addUser.jsp
			/WEB-INF/message.jsp
		
		
			/WEB-INF/login.jsp
			/WEB-INF/message.jsp
		
	
    

(8)在web.xml中添加对Spring容器的支持,以及配置struts。详细内容如下:



	
	
		contextConfigLocation
		classpath:beans.xml
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		struts2
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	
	
		struts2
		/*
	
	
		index.jsp
	

(9)编写登录页面login.jsp,并编写相应的Action

login.jsp部分内容如下:
	
		
			用户名:
密码:
  
LoginAction.java
package zzuli.itstudio.ssh2.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import zzuli.itstudio.ssh2.bean.User;
import zzuli.itstudio.ssh2.service.UserService;

@Controller @Scope("prototype")
public class LoginAction {
	@Resource UserService userService;
	
	private User user;
	public User getUser() {
		return user;
	}

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

	//添加界面
	public String input(){
		return "login";
	}

	public String login(){
		//从前台页面获取数据
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
		//业务逻辑
		User user2 = userService.find(user.getUsername());
		if(user2 != null && !("").equals(user.getUsername()))
			ActionContext.getContext().put("message", "登录成功!");
		else
			ActionContext.getContext().put("message", "登录失败!");
		return "message";
	}
}
返回页面如下(message.jsp):

		

(10)编写用户注册addUser.jsp以及相应的Action

addUser.jsp
	
		
			用户名:
密码:
性别:
年龄:

AddUserAction.java

package zzuli.itstudio.ssh2.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import zzuli.itstudio.ssh2.bean.User;
import zzuli.itstudio.ssh2.service.UserService;

@Controller @Scope("prototype")
public class AddUserAction {
	
	@Resource UserService userService;
	private User user;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	//添加界面
	public String addUI(){
		return "add";
	}
	public String add(){
		userService.save(user);
		//保存之后向前台返回一个信息
		ActionContext.getContext().put("message", "注册成功!");
		return "message";
	}
}

至此三大框架的简单集成已经结束。可以再自己浏览器下进行测试。

进入登录界面输入:http://localhost:8080/SSH2/user/login_input.action

进入注册界面输入:http://localhost:8080/SSH2/user/user_addUI.action



By Mr.Z

你可能感兴趣的:(Java)