Struts+Spring+Hibernate框架配置方案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Struts+Spring+Hibernate框架配置方案2007-10-1123:24:17标签:配置StrutsSpring方案Hibernate[推送到技术圈]版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。本文以给出了一个完整的SSH(Struts+Spring+Hibernat)框架配置方案。(附源文件)本文的读者:适合刚刚学习SSH、想体验一下SSH和想用SSH做点事情的朋友。你需要的知识:会eclipse的基本操作、tomcat的配置,如果不会你可能犯了“躁进”的毛病,先学基础吧(建议你读一下《学习原来是这样的》)。当然你要有一定的Struts、Spring、Hibernate知识,至少知道struts-config.xml、hibernate.cfg.xml、ApplicationContext.xml是干什么的。因为要配置的文件比较多,为了给读者一个直观印象,在正式开始前,我先给出配置的路线图。图中按照hello.do请求的处理顺序,描述了6个配置文件之间的依赖关系。下面我们将一个实际的例子,依次来讲述各个文件的配置。例子SSHDemo演示了又前台传入请求hello.do,请求,返回一条由数据库中取出的数据。第一步、建立项目1.用MyEclipse建立SSHDemo项目。你至少需要下面的jar文件:1.Struts.jar2.commons-beanutils.jar3.commons-digester.jar4.commons-collections.jar5.commons-logging.jar6.Hibernate3.jar7.antlr-2.7.6.jar8.asm.jar9.cglib-2.1.1.jar10.dom4j-1.6.1.jar11.dom4j-1.6.1.jar12.ehcache-1.2.jar13.jta.jar14.log4j-1.2.11.jar15.mysql-connector-java-5.0.0-beta-bin.jar16.spring.jar这些文件你可以在spring的下载包中找到。把上面的jar文件加入classPath中,并拷贝到WEB-INF中的lib中。第二步、建立相关文件,包括类文件,jsp文件等11.建立hello.jsp用来显示由数据库传来的信息hello.jsp%@pagelanguage=javacontentType=text/html;charset=GB2312pageEncoding=GB2312%!DOCTYPEHTMLPUBLIC-//W3C//DTDHTML4.01Transitional//ENhtmlheadmetahttp-equiv=Content-Typecontent=text/html;charset=GB2312titleStrutsDemo/title/headbodyH1Hell,${userInfo[username]}!/H1/body/html2.建立数据库表createtableus(idint(10)primarykeyauto_increment,namevarchar(20))type=innodb;insertintous(name)values(zhaoyuan);3.创建User.javapackageedu.bjtu.zhao.ssh;publicclassUser{privateIntegerid;privateStringname;publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}}4.创建UserDAO.javapackageedu.bjtu.zhao.ssh;importorg.hibernate.Hibernate;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;publicclassUserDAO{privateSessionFactorysessionFactory;publicUserDAO(){}publicUserDAO(SessionFactorysessionFactory){this.setSessionFactory(sessionFactory);}publicvoidsetSessionFactory(SessionFactorysessionFactory){this.sessionFactory=sessionFactory;}publicUserfind(Integerid){Sessionsession=sessionFactory.openSession();Useruser=(User)session.load(User.class,id);Hibernate.initialize(user);session.close();returnuser;}}5.创建HalloAction.javapackageedu.bjtu.zhao.ssh;importjava.util.*;importjavax.servlet.http.*;importorg.apache.struts.action.Action;importorg.apache.struts.action.ActionForm;importorg.apache.struts.action.ActionForward;importorg.apache.struts.action.ActionMapping;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassHelloActionextendsAction{//privateUserCheckeruserChecker;publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest,HttpServletResponseresponse)throwsException{Stringusername=request.getParameter(user);ApplicationContextcontext=newClassPathXmlApplicationContext(applicationContext.xml);//建立DAO对象UserDAOuserDAO=(UserDAO)context.getBean(userDAO);Useruser=newUser();user=userDAO.find(newInteger(1));if(username==null){username=user.getName();}Mapmodel=newHashMap();model.put(username,username);request.setAttribute(userInfo,model);returnmapping.findForward(helloUser);}}第三步,开始配置路线图中的6个xml文件(你可以参考文章的路线图)1.配置web.xml?xmlversion=1.0encoding=UTF-8?web-appid=WebApp_IDversion=2.4xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/j2ee/web-app_2_4.xsdsession-configsession-timeout30/session-timeout/session-config!--StandardActionServletConfiguration--servletservlet-nameaction/servlet-nameservlet-classorg.apache.struts.action.ActionServlet/servlet-classinit-paramparam-nameconfig/param-nameparam-value/WEB-INF/struts-config.xml/param-value/init-paramload-on-startup1/load-on-startup/servlet!--StandardActionServletMapping--servlet-mappingservlet-nameaction/servlet-nameurl-pattern*.do/url-pattern/servlet-mapping!—配置Spring的applicationContext.xml--context-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/classes/applicationContext.xml/param-value/context-param/web-app2.配置struts-config.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEstruts-configPUBLIC-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.2//EN=/hellotype=org.springframework.web.struts.DelegatingActionProxyforwardname=helloUserpath=/WEB-INF/jsp/hello.jsp//action/action-mappings!--因为使用了代理类,DelegatingActionProxy,注册一个插件,让它通过/Hello去寻找真正的实现类--plug-inclassName=org.springframework.web.struts.ContextLoaderPlugInset-propertyproperty=contextConfigLocationvalue=/WEB-INF/action-servlet.xml//plug-in/struts-config3.配置ApplicationContext.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEbeansPUBLIC-//SPRING//DTDBEAN//EN

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功