最近一直在折腾java web相关内容,这里就把最近学习的spring+struts2+hibernate进行一个整合,也就是大家经常说的ssh。
环境工具IDE :IDea 2018
数据库:MysqL
关于如何安装tomcat、MysqL、IDea,请自行查找。
1、使用IDea创建spring项目
2、创建项目SSHDemo,项目结构如下
3、添加三层的包。
这里采用三层的架构进行项目的设计
action存放struts2的action类
dao 数据库 *** 作
service业务层
test单元测试
domain javabean
4、配置struts2
在web.xml中你会看到下面的红字提示,引起这个的原因是因为struts2 版本>=2.5 需要使用另外的方式进行配置
struts2 org.apache.struts2.dispatcher. struts2 /*修改后的配置如下
struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter其他内容不变
在action包 添加一个UserAction
<span >import<span > com.opensymphony.xwork2.ActionSupport;<span >import<span > com.opensymphony.xwork2.ModelDriven;
<span >import<span > com.ssh.domain.User;<span >/**<span >
user相关action
<span >public <span >class UserAction <span >extends ActionSupport <span >implements ModelDriven
@OverrIDe
<span >return<span > user;
}<span >public<span > String login() {
System.out.println(user);
<span >return<span > NONE;
}
}
配置struts.xml
添加一个登录页面login.Jsp测试
登录<span ><
<span >form <span >action<span >="${pageContext.request.contextpath}/user_login.action"<span >><span >用户名:<span ><<span >input <span >type<span >="text"<span > name<span >="name"<span >><span >
密码:<span ><input <span >type<span >="password"<span > name<span >="password"<span > ID<span >="password"<span >>
<span ><<span >input <span >type<span >="submit"<span > value<span >="登录"<span >>
<span ></<span >form<span >>
<span ></<span >body<span >>
<span ></<span >HTML<span >>
启动
在启动时发现如下内容
打开project structure修复出现的问题
重启Tomcat发现如下错误
六月 06,2018 10:57:12 上午 org.apache.catalina.startup.TldConfig execute信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable deBUG logging for this logger for a complete List of JARs that were scanned but no TLDs were found in them. SkipPing unneeded JARs during scanning can improve startup time and JsP compilation time.
ERROR StatusLogger Log4j2 Could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR dispatcher dispatcher initialization Failed
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:289)
at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:422)
at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:231)
at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)
at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:106)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:584)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:581)
at com.opensymphony.xwork2.inject.ContainerImpl.callinContext(ContainerImpl.java:560)
at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:581)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createbootstrapContainer(DefaultConfiguration.java:287)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:162)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.dispatcher.getContainer(dispatcher.java:957)
at org.apache.struts2.dispatcher.dispatcher.init_PreloadConfiguration(dispatcher.java:463)
at org.apache.struts2.dispatcher.dispatcher.init(dispatcher.java:496)
at org.apache.struts2.dispatcher.Initoperations.initdispatcher(Initoperations.java:73)
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:61)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
at org.apache.catalina.core.ApplicationFilterConfig.
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5699)
at org.apache.catalina.util.lifecycleBase.start(lifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1017)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:993)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1900)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.Mbeanfactory.createStandardContext(Mbeanfactory.java:618)
at org.apache.catalina.mbeans.Mbeanfactory.createStandardContext(Mbeanfactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:835)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:287)
... 64 more
Caused by: java.lang.ExceptionInInitializerError
at com.opensymphony.xwork2.ognl.OgnlValueStackFactory.setContainer(OgnlValueStackFactory.java:88)
... 69 more
Caused by: java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!
at ognl.OgnlRuntime.
... 70 more
Caused by: java.lang.classNotFoundException: javassist.Classpool
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1928)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1771)
at java.lang.class.forname0(Native Method)
at java.lang.class.forname(Class.java:264)
at ognl.OgnlRuntime.
... 70 more
六月 06,2018 10:57:12 上午 org.apache.catalina.core.StandardContext startInternal
严重: One or more Filters Failed to start. Full details will be found in the appropriate container log file
六月 06,2018 10:57:12 上午 org.apache.catalina.core.StandardContext startInternal
严重: Context [] startup Failed due to prevIoUs errors
好吧,解决问题吧
Caused by: java.lang.classNotFoundException: javassist.Classpool
这个问题,是由于没有引入javassist-3.11.0.GA.jar包引起的,那么找到该包,引入即可
重新发布,输入用户名和密码
代码。
最后struts.xml文件内容,这种配置也是一种安全设置,通配符,如果该action有其他不想被别人访问到的方法,如果通过通配符的方式,直接暴漏出来了,通过这种配置,可以配置允许访问哪些方法,对其它隐私的方法进行保护。
<span ><!
<span >DOCTYPE struts PUBliC"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"<span >>
<span ><
<span >struts<span >><span ><<span >package <span >name<span >="userAction"<span > extends<span >="struts-default"<span > namespace<span >="/"<span >>
<span ><<span >action <span >name<span >="user_*"<span > method<span >="{1}"<span > class<span >="com.ssh.action.UserAction"<span >>
<span ><<span >allowed-methods<span >>login<span ></<span >allowed-methods<span >>
<span ></<span >action<span >>
<span ></<span >package<span >>
<span ></<span >struts<span >>
测试struts2
后台action能收到数据
好了,到这里,struts2 算是配置成功了,发现还是不容易的。
整合spring在src目录下添加applicationContext.xml配置文件
<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">将action的<a href="https://m.jb51.cc/tag/chuangjian/" target="_blank" >创建</a>交给spring ioc 注意action是多例的 一定要将s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e<a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>为prototype</span><span >--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;">class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="com.ssh.action.UserAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="userAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="prototype"</span><span >/></span>
<span ></
<span >beans<span >>我们希望在服务器启动的时候加载该配置文件,那么需要在web.xml添加如下内容
org.springframework. contextConfigLocation classpath:applicationContext.xml这里需要添加spring-web-3.2.0.RELEASE.jar包
成功加载
整合Hibernate添加jar包
在src目录下添加hibernate配置文件hibernate.cfg.xml
com.MysqL.jdbc.Driver jdbc:MysqL:///sshdemo root abcd org.hibernate.dialect.MysqLDialect true true update org.hibernate.connection.C3P0ConnectionProvIDer 5 20 120 3000
在domain包下添加映射配置User.hbm.xml
在applicationContext.xml中添加如下配置
<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">加载sessionFactory配置</span><span >--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"><a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="sessionFactory"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="org.springframework.orm.hibernate3.LocalSession<a href="https://www.jb51.cc/tag/factorybean/" target="_blank" >factorybean</a>"</span><span >></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"><a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="con<a href="https://www.jb51.cc/tag/fig/" target="_blank" >fig</a>Location"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> value</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="cla<a href="https://www.jb51.cc/tag/ssp/" target="_blank" >ssp</a>ath:hibernate.cfg.xml"</span><span >></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">property</span><span >></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean</span><span >></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> 注入事务<a href="https://m.jb51.cc/tag/guanli/" target="_blank" >管理</a>器,提交事务和回滚事务 </span><span >--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"><a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="transactionManager"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="org.springframework.orm.hibernate3.HibernateTransactionManager"</span><span >></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"><a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="sessionFactory"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> ref</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="sessionFactory"</span><span >/></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean</span><span >></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">开启事务注解方式</span><span >--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">tx:annotation-driven </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;">transaction-manager</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="transactionManager"</span><span >/></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">配置userDao</span><span >--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;">class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="com.ssh.dao.UserDao"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="userDao"</span><span >></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">注入sessionFactory</span><span >--></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"><a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="sessionFactory"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> ref</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="sessionFactory"</span><span >></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">property</span><span >></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean</span><span >></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">配置userService</span><span >--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"><a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="userService"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="com.ssh.service.UserService"</span><span >></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">注入userDao</span><span >--></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">property </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"><a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="userDao"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> ref</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="userDao"</span><span >></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">property</span><span >></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"></</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean</span><span >></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><!--</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">将action的<a href="https://m.jb51.cc/tag/chuangjian/" target="_blank" >创建</a>交给spring ioc 注意action是多例的 一定要将s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e<a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a>为prototype</span><span >--></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"><</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">bean </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;">class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="com.ssh.action.UserAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> <a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="userAction"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #ff0000;"> s<a href="https://www.jb51.cc/tag/cop/" target="_blank" >cop</a>e</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">="prototype"</span><span >/></span>
<span ></
<span >beans<span >>userDao
<span >import<span > com.ssh.domain.User;<span >import<span > org.springframework.orm.hibernate3.support.HibernateDaoSupport;<span >public <span >class UserDao <span >extends<span > HibernateDaoSupport {
<span >public <span >voID<span > save(User user) {
<span >this<span >.getHibernateTemplate().save(user);
}
}
userService
<span >import<span > com.ssh.dao.UserDao;<span >import<span > com.ssh.domain.User;
<span >import<span > org.springframework.transaction.annotation.Transactional;
@Transactional
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> UserDao userDao;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> setUserDao(UserDao userDao) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">this</span>.userDao =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> userDao;}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> save(User user) { userDao.save(user);}
}
userServiceTest
<span >import<span > com.ssh.domain.User;<span >import<span > com.ssh.service.UserService;
<span >import<span > org.junit.Test;
<span >import<span > org.junit.runner.RunWith;
<span >import<span > org.springframework.test.context.ContextConfiguration;
<span >import<span > org.springframework.test.context.junit4.SpringJUnit4ClassRunner;<span >import<span > javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.
<span >class<span >)@ContextConfiguration("classpath:applicationContext.xml"<span >)
<span >public <span >class<span > UserServiceTest {
@Resource(name = "userService"<span >)
<span >private<span > UserService userService;
@Test</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> save<a href="https://www.jb51.cc/tag/test/" target="_blank" >test()</a> { User user </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> User(); user.set<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a>(</span>1<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); user.set<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>(</span>"w<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>fy"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); user.setPassword(</span>"1234"<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); userService.save(user);}
}
测试
总结
1、配置过程很麻烦,很可能会缺包。
2、在保存数据的时候发现,执行了,但数据库中没有数据,原因是因为没有配置事务管理器,还要注意开启注解的方式管理事务。
3、当然仍然可以进一步的整合,比如去掉hibernate.cfg.xml文件,在applicationContext.xml进行配置
4、注意如果使用hibernate4需要下载hibernate4.2+的jar包
总结以上是内存溢出为你收集整理的[Java web]Spring+Struts2+Hibernate整合过程全部内容,希望文章能够帮你解决[Java web]Spring+Struts2+Hibernate整合过程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)