您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > struts2+ibatis+spring整合开发
Struts2+ibatis+spring整合开发1.工程结构图2.配置文件web.xml?xmlversion=1.0encoding=UTF-8?web-appversion=2.4xmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/j2ee/web-app_2_4.xsdwelcome-file-listwelcome-fileindex.jsp/welcome-file/welcome-file-listcontext-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/applicationContext.xml/param-value/context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerfilterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.FilterDispatcher/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mapping/web-app如图:Struts.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEstrutsPUBLIC-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN=struts.objectFactory.spring.autoWirevalue=type/constantname=struts.objectFactoryvalue=spring/includefile=struts-default.xml/packagename=struts2extends=struts-defaultdefault-interceptor-refname=paramsPrepareParamsStack/!--aciton的class为applicationContext.xml中的注册名--actionname=loginclass=LoginActionresultname=success/success.jsp/resultresultname=input/login.jsp/result/actionactionname=saveclass=LoginActionmethod=saveresultname=successtype=redirect-actionshow.action/resultresultname=error/error.jsp/result/actionactionname=editclass=LoginActionmethod=editresultname=successupdate.jsp/result/actionactionname=updateclass=LoginActionmethod=updateresultname=successtype=redirect-actionshow.action/resultresultname=error/error.jsp/result/actionactionname=deleteclass=LoginActionmethod=deleteresultname=successtype=redirect-actionshow.action/resultresultname=error/error.jsp/result/actionactionname=showclass=LoginActionmethod=findAllUserresultname=success/list.jsp/resultresultname=error/error.jsp/result/action/package/struts如图:Struts.properties#struts.url.http.port=8080struts.devMode=truestruts.configuration.xml.reload=truestruts.locale=zh_CNstruts.i18n.encoding=UTF-8struts.objectFactory=spring如图:sql-map-config.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEsqlMapConfigPUBLIC-//ibatis.apache.org//DTDSQLMapConfig2.0//EN=cn/hsw/model/user.xml//sqlMapConfig如图:applicationContext.xml?xmlversion=1.0encoding=UTF-8?beansdefault-autowire=byTypexmlns=:xsi=:schemaLocation=://=dataSourceclass=org.apache.commons.dbcp.BasicDataSourcedestroy-method=closepropertyname=driverClassNamevalue=com.mysql.jdbc.Driver/propertyname=urlvalue=jdbc:mysql://127.0.0.1:3306/debug/propertyname=usernamevalue=root/propertyname=passwordvalue=123456//beanbeanid=clientclass=org.springframework.orm.ibatis.SqlMapClientFactoryBeanpropertyname=dataSourcereflocal=dataSource//propertypropertyname=configLocationvalueclasspath:sql-map-config.xml/value/property/bean!--bean中的class为实现接口的类;property的name为类中引用的属性名称;ref为spring注册的名称,如上面的client--beanid=userDaoclass=cn.hsw.dao.UserDAOpropertyname=clientref=client//beanbeanid=userServiceclass=cn.hsw.service.UserServicepropertyname=userDAOref=userDao/property/beanbeanid=LoginActionclass=cn.hsw.action.Loginpropertyname=userServiceref=userService/property/bean/beans如图:3.Login.javapackagecn.hsw.action;importjava.util.List;importcn.hsw.model.User;importcn.hsw.service.IUserService;importcom.opensymphony.xwork2.ActionSupport;importcom.opensymphony.xwork2.ModelDriven;importcom.opensymphony.xwork2.Preparable;publicclassLoginextendsActionSupportimplementsModelDrivenUser,Preparable{privateIUserServiceuserService=null;privateStringid;privateintpageIndex=1;privateStringpageBar;privateListUserlist;privateUseruser;publicvoidprepare()throwsException{if(id==null||id.length()==0){user=newUser();}else{user=getUserService().getUserById(Integer.parseInt(id));}}publicStringexecute()throwsException{if(getUserService().isLogin(user)){returnSUCCESS;}returnINPUT;}publicStringsave()throwsException{if(getUserService().insertUser(user)){returnSUCCESS;}returnERROR;}publicStringedit(){returnSUCCESS;}publicStringupdate()throwsException{if(getUserService().updateUser(user)){returnSUCCESS;}returnERROR;}publicStringdelete()throwsException{if(getUserService().deleteUser(Integer.parseInt(id))){returnSUCCESS;}returnERROR;}publicStringfindAllUser()throwsException{try{list=getUserService().getAllUser();}catch(Exceptione){}returnSUCCESS;}publicStringgetId(){returnid;}publicvoidsetId(Stringid){this.id=id;}publicintgetPageIndex(){returnpageIndex;}pu
本文标题:struts2+ibatis+spring整合开发
链接地址:https://www.777doc.com/doc-5946080 .html