当前位置: 代码迷 >> Eclipse >> Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目课程
  详细解决方案

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

热度:51   发布时间:2016-04-23 00:04:08.0
Eclipse搭建SSH(Struts2+Spring3+Hibernate3)框架项目教程
  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表的创建脚本。
    [sql]?view plaincopyprint?
    ?
    1. create?table?user(??
    2. userId?int?auto_increment,??
    3. userName?varchar(16)?not?null,??
    4. password?varchar(16)?not?null,??
    5. gender?int?not?null,??
    6. primary?key(userId)??
    7. );??
  5. 使用Spring3.1时出现错误的详细信息。
    [plain]?view plaincopyprint?
    ?
    1. SEVERE:?Context?initialization?failed??
    2. 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??
    3. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1445)??
    4. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)??
    5. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)??
    6. ????at?org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)??
    7. ????at?org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)??
    8. ????at?org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)??
    9. ????at?org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)??
    10. ????at?org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)??
    11. ????at?org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)??
    12. ????at?org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)??
    13. ????at?org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)??
    14. ????at?org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)??
    15. ????at?org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)??
    16. ????at?org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)??
    17. ????at?org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)??
    18. ????at?org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)??
    19. ????at?java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)??
    20. ????at?java.util.concurrent.FutureTask.run(FutureTask.java:166)??
    21. ????at?java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)??
    22. ????at?java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)??
    23. ????at?java.lang.Thread.run(Thread.java:722)??
    24. Caused?by:?java.lang.NullPointerException??
    25. ????at?org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.scanPackages(LocalSessionFactoryBuilder.java:138)??
    26. ????at?org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:294)??
    27. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)??
    28. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)??
    29. ????...?20?more??
    30. ??
    31. 七月?13,?2013?6:48:44?下午?org.apache.catalina.core.StandardContext?listenerStart??
    32. SEVERE:?Exception?sending?context?initialized?event?to?listener?instance?of?class?org.springframework.web.context.ContextLoaderListener??
    33. 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??
    34. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1445)??
    35. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)??
    36. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)??
    37. ????at?org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)??
    38. ????at?org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)??
    39. ????at?org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)??
    40. ????at?org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)??
    41. ????at?org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)??
    42. ????at?org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)??
    43. ????at?org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)??
    44. ????at?org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)??
    45. ????at?org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)??
    46. ????at?org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)??
    47. ????at?org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723)??
    48. ????at?org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)??
    49. ????at?org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)??
    50. ????at?java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)??
    51. ????at?java.util.concurrent.FutureTask.run(FutureTask.java:166)??
    52. ????at?java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)??
    53. ????at?java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)??
    54. ????at?java.lang.Thread.run(Thread.java:722)??
    55. Caused?by:?java.lang.NullPointerException??
    56. ????at?org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.scanPackages(LocalSessionFactoryBuilder.java:138)??
    57. ????at?org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:294)??
    58. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1504)??
    59. ????at?org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1442)??
    60. ????...?20?more??
  6. 整合Spring时候,使用Hibernate时点击注册报错信息。
    [plain]?view plaincopyprint?
    ?
    1. java.lang.reflect.InvocationTargetException??
    2. ????sun.reflect.NativeMethodAccessorImpl.invoke0(Native?Method)??
    3. ????sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)??
    4. ????sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)??
    5. ????java.lang.reflect.Method.invoke(Method.java:601)??
    6. ????com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)??
    7. ????com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)??
    8. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)??
    9. ????com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)??
    10. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    11. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    12. ????com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)??
    13. ????org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)??
    14. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    15. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    16. ????com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)??
    17. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    18. ????com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)??
    19. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    20. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    21. ????com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)??
    22. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    23. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    24. ????com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)??
    25. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    26. ????org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)??
    27. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    28. ????org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)??
    29. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    30. ????org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)??
    31. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    32. ????com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)??
    33. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    34. ????com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)??
    35. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    36. ????org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)??
    37. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    38. ????com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)??
    39. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    40. ????com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)??
    41. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    42. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    43. ????com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)??
    44. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    45. ????org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)??
    46. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    47. ????com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)??
    48. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    49. ????com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)??
    50. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    51. ????org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)??
    52. ????org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)??
    53. ????org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)??
    54. ????org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)??
    55. ??
    56. root?cause??
    57. ??
    58. java.lang.NoSuchMethodError:?org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;??
    59. ????org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:323)??
    60. ????org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)??
    61. ????org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457)??
    62. ????org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:392)??
    63. ????org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)??
    64. ????org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)??
    65. ????com.ynwi.ssh.daoImpl.UserDao.saveObject(UserDao.java:12)??
    66. ????com.ynwi.ssh.serviceImpl.UserManagerImpl.regUser(UserManagerImpl.java:23)??
    67. ????com.ynwi.ssh.action.RegisterAction.execute(RegisterAction.java:29)??
    68. ????sun.reflect.NativeMethodAccessorImpl.invoke0(Native?Method)??
    69. ????sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)??
    70. ????sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)??
    71. ????java.lang.reflect.Method.invoke(Method.java:601)??
    72. ????com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)??
    73. ????com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)??
    74. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)??
    75. ????com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)??
    76. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    77. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    78. ????com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)??
    79. ????org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)??
    80. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    81. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    82. ????com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)??
    83. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    84. ????com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)??
    85. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    86. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    87. ????com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)??
    88. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    89. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    90. ????com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)??
    91. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    92. ????org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)??
    93. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    94. ????org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)??
    95. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    96. ????org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)??
    97. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    98. ????com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)??
    99. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    100. ????com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)??
    101. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    102. ????org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)??
    103. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    104. ????com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)??
    105. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    106. ????com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)??
    107. ????com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)??
    108. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    109. ????com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)??
    110. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    111. ????org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)??
    112. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    113. ????com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)??
    114. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    115. ????com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)??
    116. ????com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)??
    117. ????org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)??
    118. ????org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)??
    119. ????org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)??
    120. ????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的内容。

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?id="WebApp_9"?version="2.4"?xmlns="http://java.sun.com/xml/ns/j2ee"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">??
  5. ??
  6. ????<display-name>SSH?Project</display-name>??
  7. ??
  8. ????<filter>??
  9. ????????<filter-name>SSH</filter-name>??
  10. ????????<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>??
  11. ????</filter>??
  12. ??
  13. ????<filter-mapping>??
  14. ????????<filter-name>SSH</filter-name>??
  15. ????????<url-pattern>/*</url-pattern>??
  16. ????</filter-mapping>??
  17. ??
  18. ????<welcome-file-list>??
  19. ????????<welcome-file>index.html</welcome-file>??
  20. ????</welcome-file-list>??
  21. ??
  22. </web-app>??

?

  • Register.jsp的内容。

?

[html]?view plaincopyprint?
?
  1. <%@?page?language="java"?contentType="text/html;?charset=UTF-8"??
  2. ????pageEncoding="UTF-8"%>??
  3. <%@?taglib?prefix="s"?uri="/struts-tags"%>??
  4. <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">??
  5. <html>??
  6. <head>??
  7. <meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">??
  8. <title>User?Register?Page</title>??
  9. </head>??
  10. <body>??
  11. ??
  12. ????<s:form?action="register">??
  13. ????????<s:textfield?name="user.username"?label="用户名"></s:textfield>??
  14. ????????<s:password?name="user.password"?label="密码"></s:password>??
  15. ????????<s:select?list="#{'1':'男','0':'女'}"?listKey="key"?listValue="value"??
  16. ????????????name="user.gender"?label="性别"?value="1"></s:select>??
  17. ????????<s:submit?value="注册"></s:submit>??
  18. ????</s:form>??
  19. ??
  20. </body>??
  21. </html>??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.beans;??
  2. ??
  3. public?class?UserForm?{??
  4. ??
  5. ????private?String?username;??
  6. ????private?String?password;??
  7. ????private?int?gender;??
  8. ??
  9. ????public?String?getUsername()?{??
  10. ????????return?username;??
  11. ????}??
  12. ??
  13. ????public?void?setUsername(String?username)?{??
  14. ????????this.username?=?username;??
  15. ????}??
  16. ??
  17. ????public?String?getPassword()?{??
  18. ????????return?password;??
  19. ????}??
  20. ??
  21. ????public?void?setPassword(String?password)?{??
  22. ????????this.password?=?password;??
  23. ????}??
  24. ??
  25. ????public?int?getGender()?{??
  26. ????????return?gender;??
  27. ????}??
  28. ??
  29. ????public?void?setGender(int?gender)?{??
  30. ????????this.gender?=?gender;??
  31. ????}??
  32. ??
  33. }??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.action;??
  2. ??
  3. import?com.opensymphony.xwork2.ActionSupport;??
  4. import?com.ynwi.ssh.beans.UserForm;??
  5. import?com.ynwi.ssh.service.UserManager;??
  6. import?com.ynwi.ssh.serviceImpl.UserManagerImpl;??
  7. ??
  8. public?class?RegisterAction?extends?ActionSupport?{??
  9. ??
  10. ????private?static?final?long?serialVersionUID?=?1L;??
  11. ??
  12. ????private?UserForm?user;??
  13. ??
  14. ????private?UserManager?userManager;??
  15. ??
  16. ????public?UserForm?getUser()?{??
  17. ????????return?user;??
  18. ????}??
  19. ??
  20. ????public?void?setUser(UserForm?user)?{??
  21. ????????this.user?=?user;??
  22. ????}??
  23. ??
  24. ????public?UserManager?getUserManager()?{??
  25. ????????return?userManager;??
  26. ????}??
  27. ??
  28. ????public?void?setUserManager(UserManager?userManager)?{??
  29. ????????this.userManager?=?userManager;??
  30. ????}??
  31. ??
  32. ????public?String?execute()?{??
  33. ????????try?{??
  34. ????????????this.setUserManager(new?UserManagerImpl());??
  35. ????????????userManager.regUser(user);??
  36. ????????????return?SUCCESS;??
  37. ??
  38. ????????}?catch?(Exception?e)?{??
  39. ????????????e.printStackTrace();??
  40. ????????????return?ERROR;??
  41. ????????}??
  42. ????}??
  43. ??
  44. }??

?

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

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding="GBK"?>??
  2. <!DOCTYPE?validators?PUBLIC?"-//OpenSymphony?Group//XWork?Validator?1.0.2//EN"??
  3. ????"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">??
  4. ??
  5. <validators>??
  6. ????<!--?添加对用户名的校验?-->??
  7. ????<field?name="user.username">??
  8. ????????<field-validator?type="requiredstring">??
  9. ????????????<param?name="trim">true</param>??
  10. ????????????<message>用户名不能为空</message>??
  11. ????????</field-validator>??
  12. ????????<field-validator?type="regex">??
  13. ????????????<param?name="expression"><![CDATA[(\w{6,16})]]></param>??
  14. ????????????<message>用户名输入不合法,必须为长度在6~16中间的数字或字母</message>??
  15. ????????</field-validator>??
  16. ????</field>??
  17. ??????
  18. ????<!--?添加对密码的校验?-->??
  19. ????<field?name="user.password">??
  20. ????????<field-validator?type="requiredstring">??
  21. ????????????<param?name="trim">true</param>??
  22. ????????????<message>密码不能为空</message>??
  23. ????????</field-validator>??
  24. ????????<field-validator?type="regex">??
  25. ????????????<param?name="expression"><![CDATA[(\w{6,16})]]></param>??
  26. ????????????<message>密码输入不合法,必须为长度在6~16之间的数字或者字母</message>??
  27. ????????</field-validator>??
  28. ????</field>??
  29. </validators>??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.service;??
  2. ??
  3. import?com.ynwi.ssh.beans.UserForm;??
  4. ??
  5. public?interface?UserManager?{??
  6. ??
  7. ????public?void?regUser(UserForm?user);??
  8. ??
  9. }??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.serviceImpl;??
  2. ??
  3. import?com.ynwi.ssh.beans.UserForm;??
  4. import?com.ynwi.ssh.service.UserManager;??
  5. ??
  6. public?class?UserManagerImpl?implements?UserManager?{??
  7. ??
  8. ????@Override??
  9. ????public?void?regUser(UserForm?user)?{??
  10. ??????????
  11. ????}??
  12. ??
  13. }??

?

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

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding="UTF-8"??>??
  2. <!DOCTYPE?struts?PUBLIC??
  3. ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"??
  4. ????"http://struts.apache.org/dtds/struts-2.0.dtd">??
  5. ??
  6. <struts>??
  7. ??
  8. ????<include?file="struts-default.xml"?/>??
  9. ????<package?name="ynwi"?extends="struts-default">??
  10. ????????<action?name="register"?class="com.ynwi.ssh.action.RegisterAction">??
  11. ????????????<result?name="input">/Register.jsp</result>??
  12. ????????????<result?name="success">/Success.jsp</result>??
  13. ????????????<result?name="error">/Fail.jsp</result>??
  14. ????????</action>??
  15. ????</package>??
  16. ??
  17. </struts>??


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实体中有这两个方法。

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.dao;??
  2. ??
  3. import?org.hibernate.HibernateException;??
  4. import?org.hibernate.Session;??
  5. ??
  6. public?interface?BaseDao?{??
  7. ??
  8. ????public?void?saveObject(Object?obj)?throws?HibernateException;??
  9. ??
  10. ????public?Session?getSession();??
  11. ??
  12. ????public?void?setSession(Session?session);??
  13. }??

?

  • 实现类UserDao.java的内容。

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.daoImpl;??
  2. ??
  3. import?org.hibernate.HibernateException;??
  4. import?org.hibernate.Session;??
  5. ??
  6. import?com.ynwi.ssh.dao.BaseDao;??
  7. ??
  8. public?class?UserDao?implements?BaseDao?{??
  9. ??
  10. ????private?Session?session;??
  11. ??
  12. ????@Override??
  13. ????public?Session?getSession()?{??
  14. ????????return?session;??
  15. ????}??
  16. ??
  17. ????@Override??
  18. ????public?void?setSession(Session?session)?{??
  19. ????????this.session?=?session;??
  20. ????}??
  21. ??
  22. ????@Override??
  23. ????public?void?saveObject(Object?obj)?throws?HibernateException?{??
  24. ????????session.save(obj);??
  25. ????}??
  26. ??
  27. }??

?

  • 表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。

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding='UTF-8'?>??
  2. <!DOCTYPE?hibernate-mapping?PUBLIC??
  3. ????????????????????????????"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"??
  4. ????????????????????????????"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"?>??
  5. ??
  6. <hibernate-mapping?package="com.ynwi.ssh.beans">??
  7. ????<class?name="User"?table="user">??
  8. ????????<id?name="userId"?column="userId">??
  9. ????????????<generator?class="native"></generator>??
  10. ????????</id>??
  11. ????????<property?name="username"?column="userName"?type="java.lang.String"??
  12. ????????????not-null="true"?length="16"></property>??
  13. ????????<property?name="password"?column="password"?type="java.lang.String"??
  14. ????????????not-null="true"?length="16"?/>??
  15. ????????<property?name="gender"?column="gender"?type="java.lang.Integer"?length="1"?/>??
  16. ????</class>??
  17. </hibernate-mapping>??

?

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

?

[html]?view plaincopyprint?
?
  1. <?xml?version='1.0'?encoding='utf-8'?>??
  2. <!DOCTYPE?hibernate-configuration?PUBLIC??
  3. "-//Hibernate/Hibernate?Configuration?DTD?3.0//EN"??
  4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">??
  5. ??
  6. <hibernate-configuration>??
  7. ??<session-factory>??
  8. ????<!--?Database?connection?settings?-->??
  9. ????<property?name="connection.driver_class">com.mysql.jdbc.Driver</property>??
  10. ????<property?name="connection.url">jdbc:mysql://localhost:3306/project</property>??
  11. ????<property?name="connection.username">root</property>??
  12. ????<property?name="connection.password">root</property>??
  13. ??
  14. ????<!--?JDBC?connection?pool?(use?the?built-in)?-->??
  15. ????<!--?<property?name="connection.pool_size">1</property>?-->??
  16. ??
  17. ????<!--?SQL?dialect?-->??
  18. ????<property?name="dialect">org.hibernate.dialect.MySQLDialect</property>??
  19. ??
  20. ????<!--?Enable?Hibernate's?automatic?session?context?management?-->??
  21. ????<!--?<property?name="current_session_context_class">thread</property>?-->??
  22. ??
  23. ????<!--?Disable?the?second-level?cache??-->??
  24. ????<!--?<property?name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>?-->??
  25. ??
  26. ????<!--?Echo?all?executed?SQL?to?stdout?-->??
  27. ????<property?name="show_sql">true</property>??
  28. ??
  29. ????<!--?Drop?and?re-create?the?database?schema?on?startup?-->??
  30. ????<!--?<property?name="hbm2ddl.auto">update</property>?-->??
  31. ????<mapping?resource="com/ynwi/ssh/beans/User.hbm.xml"/>??
  32. ??</session-factory>??
  33. </hibernate-configuration>??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.daoImpl;??
  2. ??
  3. import?org.hibernate.HibernateException;??
  4. import?org.hibernate.Session;??
  5. import?org.hibernate.SessionFactory;??
  6. import?org.hibernate.cfg.Configuration;??
  7. import?org.hibernate.service.ServiceRegistry;??
  8. import?org.hibernate.service.ServiceRegistryBuilder;??
  9. ??
  10. public?class?HibernateSessionFactory?{??
  11. ??
  12. ????private?static?final?String?CFG_FILE_LOCATION?=?"/Hibernate.cfg.xml";??
  13. ??
  14. ????private?static?final?ThreadLocal<Session>?threadLocal?=?new?ThreadLocal<Session>();??
  15. ??
  16. ????private?static?final?Configuration?cfg?=?new?Configuration()??
  17. ????????????.configure(CFG_FILE_LOCATION);??
  18. ??
  19. ????private?static?ServiceRegistryBuilder?builder?=?new?ServiceRegistryBuilder()??
  20. ????????????.applySettings(cfg.getProperties());??
  21. ??
  22. ????private?static?ServiceRegistry?registry;??
  23. ??
  24. ????private?static?SessionFactory?sessionFactory;??
  25. ??
  26. ????public?static?Session?currentSession()?throws?HibernateException?{??
  27. ????????Session?session?=?threadLocal.get();??
  28. ??
  29. ????????if?(session?==?null?||?session.isOpen()?==?false)?{??
  30. ??
  31. ????????????if?(sessionFactory?==?null)?{??
  32. ????????????????try?{??
  33. ????????????????????registry?=?builder.buildServiceRegistry();??
  34. ????????????????????sessionFactory?=?cfg.buildSessionFactory(registry);??
  35. ????????????????}?catch?(Exception?e)?{??
  36. ????????????????????e.printStackTrace();??
  37. ????????????????}??
  38. ????????????}??
  39. ??
  40. ????????????session?=?sessionFactory.openSession();??
  41. ????????????threadLocal.set(session);??
  42. ??
  43. ????????}??
  44. ??
  45. ????????return?session;??
  46. ????}??
  47. ??
  48. ????public?static?void?closeSession()?throws?HibernateException?{??
  49. ????????Session?session?=?threadLocal.get();??
  50. ????????threadLocal.set(null);??
  51. ????????if?(session?!=?null)?{??
  52. ????????????session.close();??
  53. ????????}??
  54. ????}??
  55. ??
  56. }??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.serviceImpl;??
  2. ??
  3. import?org.hibernate.HibernateException;??
  4. import?org.hibernate.Session;??
  5. import?org.hibernate.Transaction;??
  6. ??
  7. import?com.ynwi.ssh.beans.User;??
  8. import?com.ynwi.ssh.dao.BaseDao;??
  9. import?com.ynwi.ssh.daoImpl.HibernateSessionFactory;??
  10. import?com.ynwi.ssh.daoImpl.UserDao;??
  11. import?com.ynwi.ssh.forms.UserForm;??
  12. import?com.ynwi.ssh.service.UserManager;??
  13. ??
  14. public?class?UserManagerImpl?implements?UserManager?{??
  15. ??
  16. ????private?BaseDao?dao;??
  17. ??
  18. ????private?Session?session;??
  19. ??
  20. ????public?UserManagerImpl()?{??
  21. ????????dao?=?new?UserDao();??
  22. ????}??
  23. ??
  24. ????@Override??
  25. ????public?void?regUser(UserForm?userForm)?throws?HibernateException?{??
  26. ????????session?=?HibernateSessionFactory.currentSession();??
  27. ????????dao.setSession(session);??
  28. ????????//?获取事务??
  29. ????????Transaction?ts?=?session.beginTransaction();??
  30. ????????//?构造User对象??
  31. ????????User?user?=?new?User();??
  32. ????????user.setUsername(userForm.getUsername());??
  33. ????????user.setPassword(userForm.getPassword());??
  34. ????????user.setGender(userForm.getGender());??
  35. ????????//?保存User对象??
  36. ????????dao.saveObject(user);??
  37. ????????//?提交事务??
  38. ????????ts.commit();??
  39. ????????//?关闭Session??
  40. ????????HibernateSessionFactory.closeSession();??
  41. ????}??
  42. ??
  43. }??

?

运行效果

  • 输入访问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的内容。

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????xsi:schemaLocation="http://www.springframework.org/schema/beans??
  5. ???????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">??
  6. ??
  7. ????<!--?定义数据源的信息?-->??
  8. ????<bean?id="dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource"??
  9. ????????destroy-method="close">??
  10. ????????<property?name="driverClass">??
  11. ????????????<value>com.mysql.jdbc.Driver</value>??
  12. ????????</property>??
  13. ????????<property?name="jdbcUrl">??
  14. ????????????<value>jdbc:mysql://localhost/project</value>??
  15. ????????</property>??
  16. ????????<property?name="user">??
  17. ????????????<value>root</value>??
  18. ????????</property>??
  19. ????????<property?name="password">??
  20. ????????????<value>root</value>??
  21. ????????</property>??
  22. ????????<property?name="maxPoolSize">??
  23. ????????????<value>80</value>??
  24. ????????</property>??
  25. ????????<property?name="minPoolSize">??
  26. ????????????<value>1</value>??
  27. ????????</property>??
  28. ????????<property?name="initialPoolSize">??
  29. ????????????<value>1</value>??
  30. ????????</property>??
  31. ????????<property?name="maxIdleTime">??
  32. ????????????<value>20</value>??
  33. ????????</property>??
  34. ????</bean>??
  35. ??
  36. ????<!--定义Hibernate的SessionFactory?-->??
  37. ????<!--?SessionFactory使用的数据源为上面的数据源?-->??
  38. ????<!--?指定了Hibernate的映射文件和配置信息?-->??
  39. ????<bean?id="sessionFactory"??
  40. ????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">??
  41. ????????<property?name="dataSource">??
  42. ????????????<ref?local="dataSource"?/>??
  43. ????????</property>??
  44. ????????<property?name="mappingResources">??
  45. ????????????<list>??
  46. ????????????????<value>com/ynwi/ssh/beans/User.hbm.xml</value>??
  47. ????????????</list>??
  48. ????????</property>??
  49. ????????<property?name="hibernateProperties">??
  50. ????????????<props>??
  51. ????????????????<prop?key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>??
  52. ????????????????<prop?key="show_sql">true</prop>??
  53. ????????????????<prop?key="hibernate.jdbc.batch_size">20</prop>??
  54. ????????????</props>??
  55. ????????</property>??
  56. ????</bean>??
  57. ??
  58. ????<bean?id="transactionManager"??
  59. ????????class="org.springframework.orm.hibernate3.HibernateTransactionManager">??
  60. ????????<property?name="sessionFactory"?ref="sessionFactory"?/>??
  61. ????</bean>??
  62. ??
  63. ????<bean?id="baseDao"?class="com.ynwi.ssh.daoImpl.UserDao">??
  64. ????????<property?name="sessionFactory">??
  65. ????????????<ref?bean="sessionFactory"?/>??
  66. ????????</property>??
  67. ????</bean>??
  68. ??
  69. ????<!--用户注册业务逻辑类?-->??
  70. ????<bean?id="userManager"?class="com.ynwi.ssh.serviceImpl.UserManagerImpl">??
  71. ????????<property?name="dao">??
  72. ????????????<ref?bean="baseDao"?/>??
  73. ????????</property>??
  74. ????</bean>??
  75. ??
  76. ????<!--?用户注册的Action?-->??
  77. ????<bean?id="regAction"?class="com.ynwi.ssh.action.RegisterAction">??
  78. ????????<property?name="userManager">??
  79. ????????????<ref?bean="userManager"?/>??
  80. ????????</property>??
  81. ????</bean>??
  82. ??
  83. ????<!--?more?bean?definitions?go?here?-->??
  84. ??
  85. </beans>??

?

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

BaseDao.java

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.dao;??
  2. ??
  3. import?org.hibernate.HibernateException;??
  4. ??
  5. public?interface?BaseDao?{??
  6. ??
  7. ????public?void?saveObject(Object?obj)?throws?HibernateException;??
  8. ??
  9. }??

UserDao.java

?

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.daoImpl;??
  2. ??
  3. import?org.hibernate.HibernateException;??
  4. import?org.springframework.orm.hibernate3.support.HibernateDaoSupport;??
  5. ??
  6. import?com.ynwi.ssh.dao.BaseDao;??
  7. ??
  8. public?class?UserDao?extends?HibernateDaoSupport?implements?BaseDao?{??
  9. ??
  10. ????@Override??
  11. ????public?void?saveObject(Object?obj)?throws?HibernateException?{??
  12. ????????getHibernateTemplate().save(obj);??
  13. ????}??
  14. ??
  15. }??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.serviceImpl;??
  2. ??
  3. import?org.hibernate.HibernateException;??
  4. import?org.springframework.beans.BeanUtils;??
  5. ??
  6. import?com.ynwi.ssh.beans.User;??
  7. import?com.ynwi.ssh.dao.BaseDao;??
  8. import?com.ynwi.ssh.forms.UserForm;??
  9. import?com.ynwi.ssh.service.UserManager;??
  10. ??
  11. public?class?UserManagerImpl?implements?UserManager?{??
  12. ??
  13. ????private?BaseDao?dao;??
  14. ??
  15. ????public?void?setDao(BaseDao?dao)?{??
  16. ????????this.dao?=?dao;??
  17. ????}??
  18. ??
  19. ????@Override??
  20. ????public?void?regUser(UserForm?userForm)?throws?HibernateException?{??
  21. ????????User?user?=?new?User();??
  22. ????????BeanUtils.copyProperties(userForm,?user);??
  23. ????????dao.saveObject(user);??
  24. ????}??
  25. ??
  26. }??

?

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

?

[java]?view plaincopyprint?
?
  1. package?com.ynwi.ssh.action;??
  2. ??
  3. import?com.opensymphony.xwork2.ActionSupport;??
  4. import?com.ynwi.ssh.forms.UserForm;??
  5. import?com.ynwi.ssh.service.UserManager;??
  6. ??
  7. public?class?RegisterAction?extends?ActionSupport?{??
  8. ??
  9. ????private?static?final?long?serialVersionUID?=?1L;??
  10. ??
  11. ????private?UserForm?user;??
  12. ??
  13. ????private?UserManager?userManager;??
  14. ??
  15. ????public?UserForm?getUser()?{??
  16. ????????return?user;??
  17. ????}??
  18. ??
  19. ????public?void?setUser(UserForm?user)?{??
  20. ????????this.user?=?user;??
  21. ????}??
  22. ??
  23. ????public?void?setUserManager(UserManager?userManager)?{??
  24. ????????this.userManager?=?userManager;??
  25. ????}??
  26. ??
  27. ????public?String?execute()?{??
  28. ????????try?{??
  29. ????????????userManager.regUser(user);??
  30. ????????????return?SUCCESS;??
  31. ??
  32. ????????}?catch?(Exception?e)?{??
  33. ????????????e.printStackTrace();??
  34. ????????????return?ERROR;??
  35. ????????}??
  36. ????}??
  37. ??
  38. }??

?

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

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <web-app?id="WebApp_9"?version="2.4"?xmlns="http://java.sun.com/xml/ns/j2ee"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  4. ????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">??
  5. ??
  6. ????<display-name>SSH?Project</display-name>??
  7. ??
  8. ????<filter>??
  9. ????????<filter-name>SSH</filter-name>??
  10. ????????<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>??
  11. ????</filter>??
  12. ??
  13. ????<filter-mapping>??
  14. ????????<filter-name>SSH</filter-name>??
  15. ????????<url-pattern>/*</url-pattern>??
  16. ????</filter-mapping>??
  17. ??
  18. ????<listener>??
  19. ????????<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>??
  20. ????</listener>??
  21. ??
  22. ????<welcome-file-list>??
  23. ????????<welcome-file>index.html</welcome-file>??
  24. ????</welcome-file-list>??
  25. ??
  26. </web-app>??

?

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

?

[html]?view plaincopyprint?
?
  1. <?xml?version="1.0"?encoding="UTF-8"??>??
  2. <!DOCTYPE?struts?PUBLIC??
  3. ????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.0//EN"??
  4. ????"http://struts.apache.org/dtds/struts-2.0.dtd">??
  5. ??
  6. <struts>??
  7. ??
  8. ????<include?file="struts-default.xml"?/>??
  9. ????<package?name="ynwi"?extends="struts-default">??
  10. ????????<action?name="register"?class="regAction">??
  11. ????????????<result?name="input">/Register.jsp</result>??
  12. ????????????<result?name="success">/Success.jsp</result>??
  13. ????????????<result?name="error">/Fail.jsp</result>??
  14. ????????</action>??
  15. ????</package>??
  16. ??
  17. </struts>??

?

运行结果

  • 输入访问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。
  相关解决方案