前言
确实好久没有写过了,一直以来把写博文当作自己学习的总结,当作做过的笔记,随时都可以拿出来看看。不过最近习惯了用OneNote和印象笔记,所以就很少在论坛写博文。但是偶尔看到几篇被转载了,也小小的虚荣了下。这种虚荣大神们已经习以为常,他们是不能理解的。
好吧,转入正题。
参加工作从事Java Web开发将近两年,也没有完整的搭建出一个SSH框架的项目,说出来确实有点不好意思,并且前一段一个朋友问我搭建过SSH框架没,我都没有好意思说没有。所以才有了搭建一个SSH框架项目的想法,因此有了今天的这篇博文。
这篇博文的目的
- 尝试搭建一个完整的SSH框架项目。
- 给以后的自己,也给别人一个参考。
读博文前应该注意:
- 本文提纲:本文通过一个用户注册的实例讲解SSH的整合。创建Struts项目,整合Hibernate,整合Spring。最后总结如何熟练创建SSH项目。
- 仅是创建SSH项目,对于其他的扩展例如Struts的国际化,Hibernate的缓存优化,Spring的AOP等,本博文涉及不到。想学习更多的东西请搜索其他博文。
- 本项目的环境: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数据库。
- 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)
- );
- 使用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
- 整合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项目
创建工程
- 在Eclipse中创建一个Dynamic Web Project,本文项目名称为SSHProject。
- 把Struts内apps文件夹下的struts2-blank war包用压缩工具解压,然后把WEB-INF/lib内的jar包和WEB-INF下的web.xml文件分别复制到SSHProject项目下的WEB-INF/lib内和WEB-INF下。
- 在src目录下创建相应package。
- 在WebContent下创建Register.jsp文件,并分别创建注册成功和注册失败的Success.jsp和Fail.jsp。
操作完成后的结构如图所示:
编写代码
- web.xml的内容。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>SSH Project</display-name>
- <filter>
- <filter-name>SSH</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>SSH</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
- Register.jsp的内容。
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!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>User Register Page</title>
- </head>
- <body>
- <s:form action="register">
- <s:textfield name="user.username" label="用户名"></s:textfield>
- <s:password name="user.password" label="密码"></s:password>
- <s:select list="#{'1':'男','0':'女'}" listKey="key" listValue="value"
- name="user.gender" label="性别" value="1"></s:select>
- <s:submit value="注册"></s:submit>
- </s:form>
- </body>
- </html>
- 用户表单类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。
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
- <validators>
- <!-- 添加对用户名的校验 -->
- <field name="user.username">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>用户名不能为空</message>
- </field-validator>
- <field-validator type="regex">
- <param name="expression"><![CDATA[(\w{6,16})]]></param>
- <message>用户名输入不合法,必须为长度在6~16中间的数字或字母</message>
- </field-validator>
- </field>
- <!-- 添加对密码的校验 -->
- <field name="user.password">
- <field-validator type="requiredstring">
- <param name="trim">true</param>
- <message>密码不能为空</message>
- </field-validator>
- <field-validator type="regex">
- <param name="expression"><![CDATA[(\w{6,16})]]></param>
- <message>密码输入不合法,必须为长度在6~16之间的数字或者字母</message>
- </field-validator>
- </field>
- </validators>
- 业务逻辑接口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的内容。
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <include file="struts-default.xml" />
- <package name="ynwi" extends="struts-default">
- <action name="register" class="com.ynwi.ssh.action.RegisterAction">
- <result name="input">/Register.jsp</result>
- <result name="success">/Success.jsp</result>
- <result name="error">/Fail.jsp</result>
- </action>
- </package>
- </struts>
Success.jsp和Fail.jsp仅仅只是一句提示注册成功和失败的话,这里就不在列出。
运行效果
- 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。
- 输入用户名和密码不合法时。
- 注册成功时。
在Struts基础上整合Hibernate
整合Hibernate
- 将Hibernate内的lib/required下的jar包和lib/jpa下的jar包复制到SSHProject项目的lib目录下。
- 在上面Struts项目的基础上添加com.ynwi.ssh.dao和com.ynwi.ssh.daoImpl包。分别创建BaseDao接口和UserDao类。
- 在数据库中创建对应的表user来写入要注册的用户。创建表的脚本见前言说明部分。
- 创建user表的Hibernate映射文件。因为这里要创建user表对应的POJO类,所以我这里把之前com.ynwi.ssh.beans包改成com.ynwi.ssh.forms包,用来存放页面传过来的表单数据,新创建com.ynwi.ssh.beans包,用来创建POJO对象来对应数据库中的表。路径:POJO类同目录。
- 建立Hibernate配置文件。路径:src目录下。
- 编写Session工厂类。整合Spring后,数据源的创建和SessionFactory的创建都交给Spring去管理,就无需这个工厂类了,所以这里我临时创建在daoImpl包里面。
- 修改业务逻辑实现类。
操作完成后的结构如图所示:
编写代码
- 接口类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的内容。代码<generator class="native"></generator>说明将根据本地数据库的设置来创建userId,如MySQL中将表user的userId设置为自动增长,则在保存记录是,userId将自动增长。对于需要在程序中指定主键的值,则将generator的class属性设置为assigned。
- <?xml version="1.0" encoding='UTF-8'?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
- <hibernate-mapping package="com.ynwi.ssh.beans">
- <class name="User" table="user">
- <id name="userId" column="userId">
- <generator class="native"></generator>
- </id>
- <property name="username" column="userName" type="java.lang.String"
- not-null="true" length="16"></property>
- <property name="password" column="password" type="java.lang.String"
- not-null="true" length="16" />
- <property name="gender" column="gender" type="java.lang.Integer" length="1" />
- </class>
- </hibernate-mapping>
- Hibernate配置文件Hibernate.cfg.xml的内容。
- <?xml version='1.0' encoding='utf-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!-- Database connection settings -->
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/project</property>
- <property name="connection.username">root</property>
- <property name="connection.password">root</property>
- <!-- JDBC connection pool (use the built-in) -->
- <!-- <property name="connection.pool_size">1</property> -->
- <!-- SQL dialect -->
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <!-- Enable Hibernate's automatic session context management -->
- <!-- <property name="current_session_context_class">thread</property> -->
- <!-- Disable the second-level cache -->
- <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
- <!-- Echo all executed SQL to stdout -->
- <property name="show_sql">true</property>
- <!-- Drop and re-create the database schema on startup -->
- <!-- <property name="hbm2ddl.auto">update</property> -->
- <mapping resource="com/ynwi/ssh/beans/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
- 工厂类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<Session> threadLocal = new ThreadLocal<Session>();
- 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
- 将Spring内libs目录下包含所有的jar包(不需要复制结尾为sources和javadoc的jar包)到SSHProject项目的lib目录下。
- 编写Spring的配置文件applicationContext.xml。路径:src目录下,需要在web.xml配置context-param指定路径,或者把该文件放在WEB-INF下,跟web.xml同目录。这里由于Spring配置数据源的需要,需要把Hibernate内lib/optional/c3p0下的c3p0-0.9.1.jar复制到lib不目下。
- 修改BaseDao和UserDao。在引入Spring后,需要用Spring进行统一的事务管理,数据源和sessionFactory都交给Spring去生成,因此接口类和实现类BaseDao和UserDao都需要做相应的修改。Spring提供了HibernateDaoSupport类来完成对数据的操作,因此UserDao在实现BaseDao的同时还需要继承HibernateDaoSupport类。并将先前session的操作修改成HibernateTemplate(可通过getHibernateTemplate()方法来获得)的操作。
- 修改业务逻辑实现类。在没有加入Spring之前,业务逻辑实现类的Session的获得,dao的实例化,以及事务的管理都是该类执行管理的。加入Spring后,这些都交给Spring去管理。该类的dao的实例化由Spring注入。
- 修改用户注册的RegisterAction类。同样,RegisterAction类中的userManager的实例化也由Spring注入。
- 删除Hibernate的配置文件Hibernate.cfg.xml和工厂类HibernateSesseionFactory类。他们的工作已经交给Spring去做,已经不再有用。
- 修改web.xml,加载Spring。要想启动时加载Spring的配置文件,需要在web.xml中配置对应的监听器(listenser),并制定Spring的配置文件。
- 修改Struts的配置文件struts.xml。把原来指定的名为register的action的class由原来的路径变为applicationContext.xml文件中该Action的id。
操作完成后的结构如图所示:
编写代码
- Spring的配置文件applicationContext.xml的内容。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 定义数据源的信息 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="jdbcUrl">
- <value>jdbc:mysql://localhost/project</value>
- </property>
- <property name="user">
- <value>root</value>
- </property>
- <property name="password">
- <value>root</value>
- </property>
- <property name="maxPoolSize">
- <value>80</value>
- </property>
- <property name="minPoolSize">
- <value>1</value>
- </property>
- <property name="initialPoolSize">
- <value>1</value>
- </property>
- <property name="maxIdleTime">
- <value>20</value>
- </property>
- </bean>
- <!--定义Hibernate的SessionFactory -->
- <!-- SessionFactory使用的数据源为上面的数据源 -->
- <!-- 指定了Hibernate的映射文件和配置信息 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <property name="mappingResources">
- <list>
- <value>com/ynwi/ssh/beans/User.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <prop key="show_sql">true</prop>
- <prop key="hibernate.jdbc.batch_size">20</prop>
- </props>
- </property>
- </bean>
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="baseDao" class="com.ynwi.ssh.daoImpl.UserDao">
- <property name="sessionFactory">
- <ref bean="sessionFactory" />
- </property>
- </bean>
- <!--用户注册业务逻辑类 -->
- <bean id="userManager" class="com.ynwi.ssh.serviceImpl.UserManagerImpl">
- <property name="dao">
- <ref bean="baseDao" />
- </property>
- </bean>
- <!-- 用户注册的Action -->
- <bean id="regAction" class="com.ynwi.ssh.action.RegisterAction">
- <property name="userManager">
- <ref bean="userManager" />
- </property>
- </bean>
- <!-- more bean definitions go here -->
- </beans>
- 修改后的接口类Dao和实现类UserDao的内容。
BaseDao.java
- package com.ynwi.ssh.dao;
- import org.hibernate.HibernateException;
- public interface BaseDao {
- public void saveObject(Object obj) throws HibernateException;
- }
- 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的内容。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>SSH Project</display-name>
- <filter>
- <filter-name>SSH</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>SSH</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
- 修改后的Struts配置文件struts.xml的内容。
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <include file="struts-default.xml" />
- <package name="ynwi" extends="struts-default">
- <action name="register" class="regAction">
- <result name="input">/Register.jsp</result>
- <result name="success">/Success.jsp</result>
- <result name="error">/Fail.jsp</result>
- </action>
- </package>
- </struts>
运行结果
- 输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp后。
- 点击注册前后数据库里面的数据。
总结
唠叨
本来想创建Struts2 + Spring3 + Hibernate4的项目,结果行动之后才发现版本之间不兼容的现象挺严重(我自己是这么认为的,如果有大神看出来是我开发中出的问题导致麻烦告知下),不得不把Hibernate4换成Hibernate3。因为中间换框架,所以导致此博文中整合hibernate部分是否会报错,不过最终的项目是可以执行的。我会把项目打包上传到csdn资源上。可以点击这里下载。
如何快速开发SSH框架项目
- 创建Dynamic Web Project并且导入jar包。
- 我习惯上把classes文件夹由build文件夹内转移到WEB-INF下。在项目上右键Build Path——〉Configure Build Path。设置Default output folder。
- 在WEB_INF下面创建web.xml并配置Struts的Filter和Spring的Listener。
- 在WEB-INF下面创建applicationContext.xml并配置数据源,sessionFactory,Transaction以及各Beans信息。
- 在src目录下创建struts.xml用来配置action信息,模板可以从Struts空项目里面拿。这里的action对应的class应该为applicationContext.xml内对应的class的bean的id。
转自:http://blog.csdn.net/yeohcooller/article/details/9316923