SSH框架学习1目录目录................................................................................................................................1一、建立web项目.........................................................................................................3二、加入基本的Struts)...................................................................................................5三、在struts基础上加入spring....................................................................................11四、struts+spring搭建,spring注解注入......................................................................21五、在struts和spring基础上加入hibernate................................................................25六、加入log4j...............................................................................................................57七、加入c3p0连接池并改用spring提供的sessionFactory...........................................61八、现在对SSH框架的理解.........................................................................................69九、Junit4单元测试......................................................................................................71十、Junit+GroboUtils进行多线程测试...........................................................................75十一、调整和设置........................................................................................................79十二、myeclipse10下ssh框架搭建详细图解...............................................................89SSH框架学习3一、建立web项目之前想用myeclipse一次搞定,发现,那样做反而不利于理解,对于我这样的新手还是一步一步来的好。计划是这样的,搭建web,添加struts,添加spring,数据库里面建立个user表,添加hibernate,首先把最基本的框架搭起来。之后,又一点一点把框架丰富起来,添加role表,添加验证,ajax啥的。搭建web不多说了,直接上图。ContextrootURL里面,默认是和项目名一样,会发布到tomcat的:webapp\项目名\这个地方,如果是:/,如上图,会发布到tomcat的:webapp\ROOT\目录下。SSH框架学习4myeclipse会默认添加一个index.jsp页面,只要servers运行起来能看到下面这个图,就说明基本的web就搭建好了。SSH框架学习5二、加入基本的Struts)用myeclipse来添加struts,只是省了自己新建配置文件,导入包这步。其实自己做也不麻烦。1、导入struts包。struts最新版是2.3.7,下载地址下载下来的的压缩包解开,里面有个apps目录,里面是些.war文件。其实war就是zip文件,重新命名下,就可以解压缩。把struts2-blank解压开来,能得到一个示例项目。既然人家都说这是blank了,就直接用这里面的包做最基础的包好了。我不喜欢直接把包拷贝到lib目录下,宁可多建很多个Libraries包。SSH框架学习62、包导入好了,开始,建一个action。先建立一个Package:demo.myssh.action,在下面建一个UserAction的类。类的代码如下[java]viewplaincopy1.packagedemo.myssh.action;2.SSH框架学习73.importcom.opensymphony.xwork2.ActionSupport;4.5.@SuppressWarnings(serial)6.publicclassUserActionextendsActionSupport{7.8.@Override9.publicStringexecute()throwsException{10.11.this.addActionMessage(UserActionworking);12.this.addActionMessage(helloworld.);13.14.returnActionSupport.SUCCESS;15.}16.17.}3、顺便改造下默认的index.jsp,用actionmessage来接收UserAction的信息,这样好测试Action是否起作用了。[html]viewplaincopy1.%@pagelanguage=javaimport=java.util.*pageEncoding=UTF-8%2.%@taglibprefix=suri=/struts-tags%3.%4.Stringpath=request.getContextPath();5.StringbasePath=request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/;SSH框架学习86.%7.8.!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//EN9.html10.head11.basehref=%=basePath%12.titleMyJSP'index.jsp'startingpage/title13./head14.15.body16.ThisismyJSPpage.br17.s:actionmessage/18./body19./html4、接下来,修改web.xml[html]viewplaincopy1.?xmlversion=1.0encoding=UTF-8?2.web-app3.version=3.0xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/javaee/web-app_3_0.xsd7.display-namemyssh/display-name8.9.filter10.filter-namestruts2/filter-nameSSH框架学习911.filter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class12./filter13.14.filter-mapping15.filter-namestruts2/filter-name16.url-pattern*.action/url-pattern17./filter-mapping18.19.welcome-file-list20.welcome-fileindex.jsp/welcome-file21./welcome-file-list22.23./web-app5、在src目录下,添加struts.xml文件[html]viewplaincopy1.?xmlversion=1.0encoding=UTF-8?2.!DOCTYPEstrutsPUBLIC3.-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN4.=mysshdemoextends=struts-defaultSSH框架学习107.actionname=userclass=demo.myssh.action.UserAction8.result/index.jsp/result9./action10./package11./struts这样,一个基本的struts就建立好了。访问,就能看到以下SSH框架学习11三、在struts基础上加入springspring现在的版本是3.1.3,下载地址的包里面没有提供例子,额,这点不如struts。1、导入包。首先需要添加struts的spring插件:struts2-spring-plugin-2.3.7.jar其次,spring还需要一个外部的jar:commons-logging-1.1.1.jar,下载地址在这里,spring只需要以下几个jar2、新建packa:demo.myssh.business,添加UserService类。[java]viewplaincopy1.packagedemo.myssh.business;2.3.publicclassUserService{4.SSH框架学习125.publicStringdoing()6.{7.returnUserServiceworking;8.}9.}3、为了让UserService能够注入到UserAction,还需要为UserAction添加注入用的属性和方法[java]viewplaincopy1.packagedemo.myssh.action;2.3.importcom.opensymphony.xwork2.ActionSupport;4.importdemo.myssh.business.UserService;5.6.@SuppressWarnings(serial)7.publicclassUserActionextendsActio