Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程

前言

确实好久没有写过了,一直以来把写博文当作自己学习的总结,当作做过的笔记,随时都可以拿出来看看。不过最近习惯了用OneNote和印象笔记,所以就很少在论坛写博文。但是偶尔看到几篇被转载了,也小小的虚荣了下。这种虚荣大神们已经习以为常,他们是不能理解的。

好吧,转入正题。

参加工作从事Java Web开发将近两年,也没有完整的搭建出一个SSH框架的项目,说出来确实有点不好意思,并且前一段一个朋友问我搭建过SSH框架没,我都没有好意思说没有。所以才有了搭建一个SSH框架项目的想法,因此有了今天的这篇博文。

这篇博文的目的

  1. 尝试搭建一个完整的SSH框架项目。
  2. 给以后的自己,也给别人一个参考。

读博文前应该注意:

  1. 本文提纲:本文通过一个用户注册的实例讲解SSH的整合。创建Struts项目,整合Hibernate,整合Spring。最后总结如何熟练创建SSH项目。
  2. 仅是创建SSH项目,对于其他的扩展例如Struts的国际化,Hibernate的缓存优化,Spring的AOP等,本博文涉及不到。想学习更多的东西请搜索其他博文。
  3. 本项目的环境:Windows 8-64位,Eclipse Indigo Service Release 2,Tomcat 7.0,Struts-2.2.3.1,spring-framework-3.2.3.RELEASE(本来使用Spring3.1,但是整合Spring的时候启动服务器报错。错误信息竟然是Spring框架中的方法参数空指针。。。对比Spring3.2版本后,发现在3.2种增加了判断此参数是否为空,所以最终改为用Spring3.2.3的版本。详细错误信息见本目录下5),hibernate-release-4.0.0.CR6(整合Spring3时,可以启动服务器,但是点击注册后调用UserDao类的getHibernateTemplate().save(Obj)时浏览器页面报错,但是服务器没有反应。提示org.hibernate.SessionFactory中有没有openSession(Interceptor interceptor)
    方法,但是Spring框架中的代码Session session = (entityInterceptor != null ? sessionFactory.openSession(entityInterceptor) : sessionFactory.openSession())显示用到该方法,虽然调用这段代码的方法传进来的entityInterceptor就为null。。。网上搜索无果,所以最终在整合Spring的时候使用Hibernate-3.6.10.Final。详细错误信息见本目录下6
    ),MySQL 5.5.28下的project数据库。
  4. user表的创建脚本。
    create table user(
    userId int auto_increment,
    userName varchar(16) not null,
    password varchar(16) not null,
    gender int not null,
    primary key(userId)
    );
  5. 使用Spring3.1时出现错误的详细信息。
    SEVERE: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1445)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
    	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	at java.lang.Thread.run(Thread.java:722)
    Caused by: java.lang.NullPointerException
    	at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.scanPackages(LocalSessionFactoryBuilder.java:138)
    	at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:294)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)
    	... 20 more
    
    七月 13, 2013 6:48:44 下午 org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1445)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
    	at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
    	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	at java.lang.Thread.run(Thread.java:722)
    Caused by: java.lang.NullPointerException
    	at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.scanPackages(LocalSessionFactoryBuilder.java:138)
    	at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:294)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)
    	... 20 more
  6. 整合Spring时候,使用Hibernate时点击注册报错信息。
    java.lang.reflect.InvocationTargetException
    	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	java.lang.reflect.Method.invoke(Method.java:601)
    	com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    	com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    	com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    	org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
    	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    
    root cause
    
    java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
    	org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:323)
    	org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
    	org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457)
    	org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:392)
    	org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    	org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
    	com.ynwi.ssh.daoImpl.UserDao.saveObject(UserDao.java:12)
    	com.ynwi.ssh.serviceImpl.UserManagerImpl.regUser(UserManagerImpl.java:23)
    	com.ynwi.ssh.action.RegisterAction.execute(RegisterAction.java:29)
    	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	java.lang.reflect.Method.invoke(Method.java:601)
    	com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    	com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    	com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    	org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    	com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    	org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
    	org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    

创建Struts项目

创建工程

  1. 在Eclipse中创建一个Dynamic Web Project,本文项目名称为SSHProject。
  2. 把Struts内apps文件夹下的struts2-blank war包用压缩工具解压,然后把WEB-INF/lib内的jar包和WEB-INF下的web.xml文件分别复制到SSHProject项目下的WEB-INF/lib内和WEB-INF下。
  3. 在src目录下创建相应package。
  4. 在WebContent下创建Register.jsp文件,并分别创建注册成功和注册失败的Success.jsp和Fail.jsp。

操作完成后的结构如图所示:


编写代码

  • web.xml的内容。




	SSH Project

	
		SSH
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	

	
		SSH
		/*
	

	
		index.html
	


  • Register.jsp的内容。

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>




User Register Page



	
		
		
		
		
	


  • 用户表单类UserForm.java的内容。

package com.ynwi.ssh.beans;

public class UserForm {

	private String username;
	private String password;
	private int gender;

	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 getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}

}

  • 注册Action类RegisterAction.java的内容。

package com.ynwi.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.ynwi.ssh.beans.UserForm;
import com.ynwi.ssh.service.UserManager;
import com.ynwi.ssh.serviceImpl.UserManagerImpl;

public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private UserForm user;

	private UserManager userManager;

	public UserForm getUser() {
		return user;
	}

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

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public String execute() {
		try {
			this.setUserManager(new UserManagerImpl());
			userManager.regUser(user);
			return SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}

  • 验证文件RegisterAction-validation.xml的内容。该xml文件为RegisterAction的验证文件,命名格式固定,为< ActionName >-validation.xml。





	
	
		
			true
			用户名不能为空
		
		
			
			用户名输入不合法,必须为长度在6~16中间的数字或字母
		
	
	
	
	
		
			true
			密码不能为空
		
		
			
			密码输入不合法,必须为长度在6~16之间的数字或者字母
		
	

  • 业务逻辑接口UserManaer.java的内容。

package com.ynwi.ssh.service;

import com.ynwi.ssh.beans.UserForm;

public interface UserManager {

	public void regUser(UserForm user);

}

  • 业务逻辑实现类UserManagerImpl.java的内容

package com.ynwi.ssh.serviceImpl;

import com.ynwi.ssh.beans.UserForm;
import com.ynwi.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {

	@Override
	public void regUser(UserForm user) {
		
	}

}

  • 配置文件struts.xml的内容。






	
	
		
			/Register.jsp
			/Success.jsp
			/Fail.jsp
		
	



Success.jsp和Fail.jsp仅仅只是一句提示注册成功和失败的话,这里就不在列出。

运行效果

  • 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。


  • 输入用户名和密码不合法时。


  • 注册成功时。

在Struts基础上整合Hibernate

整合Hibernate

  1. 将Hibernate内的lib/required下的jar包和lib/jpa下的jar包复制到SSHProject项目的lib目录下。
  2. 在上面Struts项目的基础上添加com.ynwi.ssh.dao和com.ynwi.ssh.daoImpl包。分别创建BaseDao接口和UserDao类。
  3. 在数据库中创建对应的表user来写入要注册的用户。创建表的脚本见前言说明部分。
  4. 创建user表的Hibernate映射文件。因为这里要创建user表对应的POJO类,所以我这里把之前com.ynwi.ssh.beans包改成com.ynwi.ssh.forms包,用来存放页面传过来的表单数据,新创建com.ynwi.ssh.beans包,用来创建POJO对象来对应数据库中的表。路径:POJO类同目录。
  5. 建立Hibernate配置文件。路径:src目录下。
  6. 编写Session工厂类。整合Spring后,数据源的创建和SessionFactory的创建都交给Spring去管理,就无需这个工厂类了,所以这里我临时创建在daoImpl包里面。
  7. 修改业务逻辑实现类。

操作完成后的结构如图所示:


编写代码

  • 接口类BaseDao.java的内容。注意:接口里的两个get/set方法是为了让实现类覆盖。这样使用BaseDao dao = new UserDao()的时候,dao实体中有这两个方法。

package com.ynwi.ssh.dao;

import org.hibernate.HibernateException;
import org.hibernate.Session;

public interface BaseDao {

	public void saveObject(Object obj) throws HibernateException;

	public Session getSession();

	public void setSession(Session session);
}

  • 实现类UserDao.java的内容。

package com.ynwi.ssh.daoImpl;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.ynwi.ssh.dao.BaseDao;

public class UserDao implements BaseDao {

	private Session session;

	@Override
	public Session getSession() {
		return session;
	}

	@Override
	public void setSession(Session session) {
		this.session = session;
	}

	@Override
	public void saveObject(Object obj) throws HibernateException {
		session.save(obj);
	}

}

  • 表user的POJO类User.java的内容,因为跟UserForm.java内容差不多,所以不再列出。这个比UserForm.java多一个userId属性,因为在表user里面有userId的column。在POJO类里需要跟这个column有映射。
  • POJO类User.java的Hibernate映射文件User.hbm.xml的内容。代码说明将根据本地数据库的设置来创建userId,如MySQL中将表user的userId设置为自动增长,则在保存记录是,userId将自动增长。对于需要在程序中指定主键的值,则将generator的class属性设置为assigned。





	
		
			
		
		
		
		
	

  • Hibernate配置文件Hibernate.cfg.xml的内容。





  
    
    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3306/project
    root
    root

    
    

    
    org.hibernate.dialect.MySQLDialect

    
    

    
    

    
    true

    
    
    
  

  • 工厂类SessionFactory的内容。(这是使用Hibernate4时候的SessionFactory,Hibernate3时候sessionFactory的创建估计会不一样,请自行查阅。

package com.ynwi.ssh.daoImpl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateSessionFactory {

	private static final String CFG_FILE_LOCATION = "/Hibernate.cfg.xml";

	private static final ThreadLocal threadLocal = new ThreadLocal();

	private static final Configuration cfg = new Configuration()
			.configure(CFG_FILE_LOCATION);

	private static ServiceRegistryBuilder builder = new ServiceRegistryBuilder()
			.applySettings(cfg.getProperties());

	private static ServiceRegistry registry;

	private static SessionFactory sessionFactory;

	public static Session currentSession() throws HibernateException {
		Session session = threadLocal.get();

		if (session == null || session.isOpen() == false) {

			if (sessionFactory == null) {
				try {
					registry = builder.buildServiceRegistry();
					sessionFactory = cfg.buildSessionFactory(registry);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			session = sessionFactory.openSession();
			threadLocal.set(session);

		}

		return session;
	}

	public static void closeSession() throws HibernateException {
		Session session = threadLocal.get();
		threadLocal.set(null);
		if (session != null) {
			session.close();
		}
	}

}

  • 修改后的业务逻辑实现类的内容。

package com.ynwi.ssh.serviceImpl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ynwi.ssh.beans.User;
import com.ynwi.ssh.dao.BaseDao;
import com.ynwi.ssh.daoImpl.HibernateSessionFactory;
import com.ynwi.ssh.daoImpl.UserDao;
import com.ynwi.ssh.forms.UserForm;
import com.ynwi.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {

	private BaseDao dao;

	private Session session;

	public UserManagerImpl() {
		dao = new UserDao();
	}

	@Override
	public void regUser(UserForm userForm) throws HibernateException {
		session = HibernateSessionFactory.currentSession();
		dao.setSession(session);
		// 获取事务
		Transaction ts = session.beginTransaction();
		// 构造User对象
		User user = new User();
		user.setUsername(userForm.getUsername());
		user.setPassword(userForm.getPassword());
		user.setGender(userForm.getGender());
		// 保存User对象
		dao.saveObject(user);
		// 提交事务
		ts.commit();
		// 关闭Session
		HibernateSessionFactory.closeSession();
	}

}

运行效果

  • 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。


  • 点击注册前后数据库里面的数据。

在Struts + Hibernate项目中整合Spring

整合Spring

  1. 将Spring内libs目录下包含所有的jar包(不需要复制结尾为sources和javadoc的jar包)到SSHProject项目的lib目录下。
  2. 编写Spring的配置文件applicationContext.xml。路径:src目录下,需要在web.xml配置context-param指定路径,或者把该文件放在WEB-INF下,跟web.xml同目录。这里由于Spring配置数据源的需要,需要把Hibernate内lib/optional/c3p0下的c3p0-0.9.1.jar复制到lib不目下。
  3. 修改BaseDao和UserDao。在引入Spring后,需要用Spring进行统一的事务管理,数据源和sessionFactory都交给Spring去生成,因此接口类和实现类BaseDao和UserDao都需要做相应的修改。Spring提供了HibernateDaoSupport类来完成对数据的操作,因此UserDao在实现BaseDao的同时还需要继承HibernateDaoSupport类。并将先前session的操作修改成HibernateTemplate(可通过getHibernateTemplate()方法来获得)的操作。
  4. 修改业务逻辑实现类。在没有加入Spring之前,业务逻辑实现类的Session的获得,dao的实例化,以及事务的管理都是该类执行管理的。加入Spring后,这些都交给Spring去管理。该类的dao的实例化由Spring注入。
  5. 修改用户注册的RegisterAction类。同样,RegisterAction类中的userManager的实例化也由Spring注入。
  6. 删除Hibernate的配置文件Hibernate.cfg.xml和工厂类HibernateSesseionFactory类。他们的工作已经交给Spring去做,已经不再有用。
  7. 修改web.xml,加载Spring。要想启动时加载Spring的配置文件,需要在web.xml中配置对应的监听器(listenser),并制定Spring的配置文件。
  8. 修改Struts的配置文件struts.xml。把原来指定的名为register的action的class由原来的路径变为applicationContext.xml文件中该Action的id。

操作完成后的结构如图所示:


编写代码

  • Spring的配置文件applicationContext.xml的内容。




	
	
		
			com.mysql.jdbc.Driver
		
		
			jdbc:mysql://localhost/project
		
		
			root
		
		
			root
		
		
			80
		
		
			1
		
		
			1
		
		
			20
		
	

	
	
	
	
		
			
		
		
			
				com/ynwi/ssh/beans/User.hbm.xml
			
		
		
			
				org.hibernate.dialect.MySQLDialect
				true
				20
			
		
	

	
		
	

	
		
			
		
	

	
	
		
			
		
	

	
	
		
			
		
	

	

  • 修改后的接口类Dao和实现类UserDao的内容。

BaseDao.java

package com.ynwi.ssh.dao;

import org.hibernate.HibernateException;

public interface BaseDao {

	public void saveObject(Object obj) throws HibernateException;

}
UserDao.java

package com.ynwi.ssh.daoImpl;

import org.hibernate.HibernateException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ynwi.ssh.dao.BaseDao;

public class UserDao extends HibernateDaoSupport implements BaseDao {

	@Override
	public void saveObject(Object obj) throws HibernateException {
		getHibernateTemplate().save(obj);
	}

}

  • 修改后的业务逻辑实现类UserManagerImpl的内容。

package com.ynwi.ssh.serviceImpl;

import org.hibernate.HibernateException;
import org.springframework.beans.BeanUtils;

import com.ynwi.ssh.beans.User;
import com.ynwi.ssh.dao.BaseDao;
import com.ynwi.ssh.forms.UserForm;
import com.ynwi.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {

	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	@Override
	public void regUser(UserForm userForm) throws HibernateException {
		User user = new User();
		BeanUtils.copyProperties(userForm, user);
		dao.saveObject(user);
	}

}

  • 修改后的用户注册Action类RegisterAction的内容。

package com.ynwi.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.ynwi.ssh.forms.UserForm;
import com.ynwi.ssh.service.UserManager;

public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private UserForm user;

	private UserManager userManager;

	public UserForm getUser() {
		return user;
	}

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

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public String execute() {
		try {
			userManager.regUser(user);
			return SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}

  • 修改后的web.xml的内容。




	SSH Project

	
		SSH
		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	

	
		SSH
		/*
	

	
		org.springframework.web.context.ContextLoaderListener
	

	
		index.html
	


  • 修改后的Struts配置文件struts.xml的内容。






	
	
		
			/Register.jsp
			/Success.jsp
			/Fail.jsp
		
	


运行结果

  • 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。


  • 点击注册前后数据库里面的数据。

总结

唠叨

本来想创建Struts2 + Spring3 + Hibernate4的项目,结果行动之后才发现版本之间不兼容的现象挺严重(我自己是这么认为的,如果有大神看出来是我开发中出的问题导致麻烦告知下),不得不把Hibernate4换成Hibernate3。因为中间换框架,所以导致此博文中整合Hibernate部分是否会报错,不过最终的项目是可以执行的。我会把项目打包上传到csdn资源上。可以点击这里下载。

如何快速开发SSH框架项目

  1. 创建Dynamic Web Project并且导入jar包。
  2. 我习惯上把classes文件夹由build文件夹内转移到WEB-INF下。在项目上右键Build Path——〉Configure Build Path。设置Default output folder。
  3. 在WEB_INF下面创建web.xml并配置Struts的Filter和Spring的Listener。
  4. 在WEB-INF下面创建applicationContext.xml并配置数据源,sessionFactory,Transaction以及各Beans信息。
  5. 在src目录下创建struts.xml用来配置action信息,模板可以从Struts空项目里面拿。这里的action对应的class应该为applicationContext.xml内对应的class的bean的id。

你可能感兴趣的:(JavaWeb相关,Java,IEDs,Eclipse,JSP,Servlet,Spring,Hibernate,Struts2,SSH)