教程案例講稿jfinal18manual_第1頁(yè)
教程案例講稿jfinal18manual_第2頁(yè)
教程案例講稿jfinal18manual_第3頁(yè)
教程案例講稿jfinal18manual_第4頁(yè)
教程案例講稿jfinal18manual_第5頁(yè)
已閱讀5頁(yè),還剩36頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第一章快速上 添加java文 第二章 第三章 第四章 第五章 JFinal獨(dú)創(chuàng)Db+Record模 第六章 第七章 第八章 Marker基 Marker示 在JFinal中擴(kuò) 第十章JFinal架構(gòu)及擴(kuò) JFinal是基于Java語(yǔ)言的極速WEB+ORM開發(fā)框架,其設(shè)計(jì)目標(biāo)是開發(fā)迅速、代碼量少、學(xué)習(xí)簡(jiǎn)單、功能強(qiáng)大、輕量級(jí)、易擴(kuò)展、RestfulJava語(yǔ)言所有優(yōu)勢(shì)的同時(shí)友:)JFinal JFinal:JFinal推薦使用EclipseIDEforJavaEEDevelopers做為開發(fā)環(huán)境 注意:Targetruntime一定要選擇特別注意:此處的Defaultoutfolder必須要與WebRoot\WEB-INF\classes 以使用JFinal集成的Jetty來(lái)啟動(dòng)項(xiàng)目。改成WebContent\WEB-INF\classes才能對(duì)應(yīng)上。將jfinal-xxx.jar與jetty-server-8.1.8.jar拷貝至項(xiàng)目WEB-INF\lib下即可。注意: packagepackageimportpublicclassDemoConfigextendsJFinalConfigpublicvoidconfigConstant(Constants{}publicvoidconfigRoute(Routes{ }publicvoidconfigPlugin(Pluginsme)publicvoidconfigInterceptor(Interceptorsme)publicvoidconfigHandler(Handlersme)}簽內(nèi)的配置相一致(在本例中該配置為demo.DemoConfig)。在demo包下創(chuàng)建oController類文件,內(nèi)容如下packagepackageimportpublic oControllerextendsControllerpublicvoid{renderText(" oJFinal}Mainclass輸入框中填入:com.jfinal.core.JFinalDebug按鈕啟動(dòng)項(xiàng)框架搭建完成。如需完整demo示例請(qǐng)?jiān)诖颂帲?注意:在tomcatjetty-server-xxx.jar這個(gè)包,否則會(huì)引起沖突。TomcatTOMCAT-HOME/bin/startup.batstartup.sh而不能使用上面介紹的啟動(dòng)方式,因?yàn)樯厦娴膯?dòng)方式需要用到j(luò)etty-server-xxx.jar。publicpublicclassDemoConfigextends{publicvoidconfigConstant(Constants{}publicvoidconfigRoute(Routesme)publicvoidconfigPlugin(Pluginsme)publicvoidconfigInterceptor(Interceptorsme)publicvoidconfigHandler(Handlersme)}publicpublicvoidconfigConstant(Constants{me.setDevMode(true);}在開發(fā)模式下,JFinal會(huì)對(duì)每次請(qǐng)求輸出報(bào)告,如輸出本次請(qǐng)求的Controller、Method以及請(qǐng)求所攜帶的參數(shù)。JFinal支持JSP、 Marker、Velocity三種常用視圖。此方法用來(lái)配置JFinal路由,如下代碼配置了將”/ oController這個(gè)控制器,通過(guò)以下的配置,將訪問(wèn)oController.index()方法, publicpublicvoidconfigRoute(Routes{ }controllerClass,Stringvie 應(yīng)一個(gè)Controller,controllerKey僅能定位到Controller。第二個(gè)參數(shù)controllerClass是該路徑該參數(shù)具體細(xì)節(jié)將在Controller相關(guān)章節(jié)中給出)。當(dāng)vieth未指定時(shí)默認(rèn)值為urlmethodmethodindex。urlParaurl中攜帶參數(shù)值,urlPara可以在一次請(qǐng)求中同時(shí)攜帶多個(gè)值,JFinal默認(rèn)使用減號(hào)“-”來(lái)分隔多個(gè)值(可constants.setUrlParaSeparator(String)設(shè)置分隔符ControllergetPara(intindex)分別取出這些值。controllerKey、method、urlPara這三部分必須使用正斜杠“/”分隔。注意,controllerKey自身也可以包含正斜杠“”,如“/admin/article”,這樣實(shí)質(zhì)上實(shí)現(xiàn)了struts2namespacepublicpublicclassUserControllerextends{publicvoid{}}UserControllercontrollerKey值為“/user”,在使用了@ActionKey(“/login”)注解以后,actionKey由原來(lái)的“/user/login”變?yōu)榱恕?login”actionKey中使用減號(hào)或性化的路由,大體思路就是在Handler中改變第一個(gè)參數(shù)Stringtarget的值。publicclasspublicclassFrontRoutesextendsRoutespublicvoidconfig()add("/",IndexController.class);}}publicclassAdminRoutesextendsRoutespublicvoidconfig()add("/admin",AdminController.class);}}publicclassMyJFinalConfigextendsJFinalConfigpublicvoidconfigRoute(Routesme.add(newFrontRoutes());端端路由me.add(newAdminRoutes());后端}publicvoidconfigConstant(Constantsme)publicvoidconfigPlugin(Pluginsme)publicvoidconfigInterceptor(Interceptorsme)publicvoidconfigHandler(Handlersme)可以讓MyJFinalConfig文件更簡(jiǎn)潔,而且有利于大規(guī)模團(tuán)隊(duì)開發(fā),避免多人同時(shí)修改此方法用來(lái)配置JFinal的PluginC3p0數(shù)據(jù)庫(kù)連接池插件與ActiveRecordpublicpublicvoidconfigPlugin(Plugins{getProperty("user"),getProperty("password"));ActiveRecordPluginarp=newActiveRecordPlugin(c3p0Plugin); }此處配置的器將會(huì)對(duì)所有的請(qǐng)求進(jìn)行,除非使用@ClearInterceptor在Controller中清publicpublicvoidconfigInterceptor(Interceptors{me.add(new}JFinalInterceptorStruts2,但使用起來(lái)更方便,Interceptor配置粒度分為Global、Controller、Action三個(gè)層次,其中以上代碼配置粒度為全局。ControllerAction級(jí)的Interceptor配置將在后續(xù)章節(jié)中詳細(xì)介紹。publicpublicvoidconfigHandler(Handlers{me.add(new}JFinalConfig中的afterJFinalStart()與beforeJFinalStop()方法供開發(fā)者在JFinalConfig繼承類中覆蓋。JFinal會(huì)在系統(tǒng)啟動(dòng)完成后回調(diào)afterJFinalStart()方法,會(huì)在系統(tǒng)關(guān)閉前回調(diào)Controller是JFinal類之一,該類作為MVC模式中的控制器。基于JFinal的Web應(yīng)用的控制器需要繼承該類。Controller是定義Action方法的地點(diǎn),是組織Action的式,一個(gè)Controller可以包含多個(gè)Action。Controller是線程安全的。publicpublicoControllerextendsControllerpublicvoidindex()}publicvoidtest()}}、、Controller提供了getPara系列方法用來(lái)從請(qǐng)求中獲取參數(shù)getPara系列方法分為兩種類型。第一種類型為第一個(gè)形參為String的getPara 系列方法。該系列方法是對(duì) name)的封裝,這類方法都是轉(zhuǎn)調(diào)了HttpServletRequest.getParameter(Stringname)。第二種類型為第一個(gè)形參為int或無(wú)形參的getPara系列方法。該系列方法是去獲取urlPara中所帶的參數(shù)值。getParaMap與getParaNames分別對(duì)應(yīng)HttpServletRequest的getParameterMap與getParameterNames。返回url請(qǐng)求中的urlPara這個(gè)請(qǐng)求將返回url請(qǐng)求中的urlPara這個(gè)請(qǐng)求將返回5urlParaSeparator“-”時(shí)非常有用。返回url請(qǐng)求中的urlPara參數(shù)的整體值,如這個(gè)data"setAttr(StringObject)HttpServletRequest.setAttribute(String,Object),該方法可以將各種數(shù)據(jù)傳遞給View并在View中顯示出來(lái)。還可以通過(guò)繼承Render抽象類來(lái)無(wú)限擴(kuò)展視圖類型。Controller.render(String)Controller.render(String)時(shí)的視圖類型由JFinalConfig.configConstant(Constantsconstants)配置中的constants.setViewType(ViewType)ViewType有:Marker、JSP、Velocity,不進(jìn)行配置時(shí)的缺省配置為Marker。假設(shè)在JFinalConfig.configRoute(Routesroutes)中有如下Controller映射配置:“/,渲染名為test.html的視圖,該視圖的全路徑為渲染名為test.html的視圖,該視圖的全路徑 渲染名為test.html的視圖,且視圖類型為將所有通過(guò)Controller.setAttr(String,Object)setAttr(“useruser)setAttr(“blog”,blog)其它屬性并不轉(zhuǎn)換為json。渲染純文本內(nèi)容”oJFinal”renderHtml(“o渲染Html內(nèi)容”oHtml”renderError(404,renderError(500,件,解決辦法是使用:render(newJsonRender(params).forIE())。返回需要使用return語(yǔ)句。在一個(gè)action中多次調(diào)用render方法只有最后一次有效。Interceptor是JFinalAOP的實(shí)現(xiàn)方式,通過(guò)實(shí)現(xiàn)Interceptor接口以及使用@Beforeannotation可以細(xì)粒度地進(jìn)行配置。Interceptor并非線程安全的,線程安全器需要繼承PrototypeInterceptor來(lái)實(shí)現(xiàn)。器的Action前后各輸出一行文本到控制臺(tái):importimportimportpublicclassDemoInterceptorimplementsInterceptorpublicvoidintercept(ActionInvocation{System.out.println("Beforeactioninvoking");}}器配置共分三個(gè)級(jí)別:Global級(jí)、Controller級(jí)、Action級(jí)。Global級(jí)的器將對(duì)所有Action進(jìn)行,Controller級(jí)器將對(duì)該Controller中所有Action進(jìn)行,Action級(jí)器僅對(duì)該Action進(jìn)行。annotation放置在Action定義前進(jìn)行配置。 public oControllerextends{publicvoidindex() }publicvoidtest() }publicvoidlogin() }publicvoidclearAllLayers() }}ClearInterceptor默認(rèn)僅清除上一級(jí)別的器。Action級(jí)的上一級(jí)為Controller級(jí),Controller級(jí)的上一級(jí)為Global級(jí)。如果需刪除多級(jí)器,可以為ClearInterceptor加入?yún)?shù)publicpublicclassDemoConfigextendsJFinalConfigpublicvoidconfigInterceptor(Interceptorsme)//此處配置的 me.add(newAaaInterceptor());}}ActionInvocationInterceptorintercept方法中的唯一參數(shù),提供了很多便利的方法以便在器中使用。voidControllerStringStringMethodStringpublicpublicclassManagerInterceptorextendsInterceptorStackpublicvoidconfig()}}ActiveRecordJFinalPluginJFinalConfigpublicpublicclassDemoConfigextendsJFinalConfigpublicvoidconfigPlugin(Pluginsme)"userName","password");ActiveRecordPluginarp=newActiveRecordPlugin(cp); ("user", }}后者是ActiveRecrod支持插件。ActiveReceord中定義了addMap(StringtableName,Class<?extendsModel>modelClass>)方法,該方法建立了數(shù)據(jù)庫(kù)表名到Model的映射關(guān)系。另外,以上代碼中arp.addMap(“user”,User.class),表的主鍵名為默認(rèn)為“id”,如果主鍵名稱為“user_id”則需要手動(dòng)指定,如:arp.addMap(“user”,“user_id”,User.class)。publicpublicclassUserextendsModel<User>publicstaticfinalUserdao=new}////newUser().set("name","James").set("age",//刪除id值為25的User//Useruser=User.dao.findById(25,"name,StringuserName=IntegeruserAge=//分頁(yè)查 whereage>?",18);不能用于數(shù)據(jù)承載對(duì)象。數(shù)據(jù)承載需要使用newUser().set(…)來(lái)實(shí)現(xiàn)。ppDbRecordModel////Recorduser=newRecord().set("name","James").set("age",25);Db.save("user",user);查詢id值為25的Record將其name屬性改為James并更新到數(shù)據(jù)庫(kù)userDb.findById("user"25).set("name""James");Db.update("user",user);user=Db.findById("user",25,"name,StringuserName=IntegeruserAge=//查詢所 List<Record>users=Db.find("select*fromuserwhereage>age>?",18);booleanbooleansucceed=Db.tx(newpublicbooleanrun()throwsSQLExceptionid=?",100,123);id=?",100,456);returncount==1&&count2==返回false,則自動(dòng)回滾事務(wù)。publicvoidtrans_demo()IntegertransAmount=IntegerfromAccountId=IntegertoAccountId=AccountfromAccount=AccounttoAccount=fromAccount.set("cash",fromAccount.getInt("cash")-toAccount.set("cash",toAccount.getInt("cash")+}publicvoidconfigInterceptor(Interceptors{me.add(newme.add(newTxByActionKeys("/cash/trans","/other"));me.add(newTxByActionMethods("save","update"));}publicvoidconfigInterceptor(Interceptors{me.add(newme.add(newTxByActionKeys("/cash/trans","/other"));me.add(newTxByActionMethods("save","update"));}則匹配上將開啟事務(wù)。TxByActionKeys可以對(duì)指定的actionKey進(jìn)行并開啟事務(wù)publicpublicvoidlist()List<Blog>blogList=Blog.dao.findByCache("cacheName","key","select*fromblog");}上例findByCache方法中的cacheName需要在ehcache.xml中配置如:<cachename="cacheName"…>。此外Model.paginateByCache(…)、Db.findByCache(…)、在ehccache.xml中配置相對(duì)應(yīng)的cacheName就可以了。目前ActiveRecordPlugin提供了MysqlDialect、OracleDialect、AnsiSqlDialect實(shí)現(xiàn)類。MysqlDialectOracleDialectMysqlOracle的支持,AnsiSqlDialect實(shí)現(xiàn)對(duì)遵守ANSISQL數(shù)據(jù)庫(kù)的支持。以下是數(shù)據(jù)庫(kù)Dialect的配置代碼:publicpublicclassDemoConfigextendsJFinalConfigpublicvoidconfigPlugin(Plugins{ActiveRecordPluginarp=new}}JFinalActiveRecord天然支持表關(guān)聯(lián)操作,并不需要學(xué)習(xí)新的東西,JFinalpublicpublicvoidrelation()joinuseruonb.user_id=u.idwhereb.id=?";Blogblog=Blog.dao.findFirst(sql,123);Stringname=blog.getStr("user_name");}以下代碼演示第二種方式在Blog中獲取相關(guān)聯(lián)的User以及在User中獲取相關(guān)聯(lián)的publicpublicclassBlogextendspublicstaticfinalBlogdao=newpublicUsergetUser()return}}publicclassUserextendspublicstaticfinalUserdao=newpublicList<Blog>getBlogs()returnBlog.dao.find("select*fromblogwhereuser_id=?",}}大的Oracle使用者。以下是一個(gè)完整的Oracle配置示例:publicpublicclassDemoConfigextendsJFinalConfigpublicvoidconfigPlugin(Plugins{C3p0Plugincp=newActiveRecordPluginarp=newActiveRecordPlugin(cp);//配置屬性名(字段名)大小寫不敏感容器工廠 ("user","user_id",User.class);}}如:arp.addMa(“user”,“ID”,User.class)。如果想讓ActiveRecord對(duì)屬性名(字段名)的大小寫不敏感可以通過(guò)設(shè)置CaseInsensitiveContainerFactory來(lái)達(dá)到,有了這個(gè)設(shè)置,則arp.addMa(“user”,“ID”,User.class)不再需要了。持自動(dòng)主鍵主要分為兩步:一是創(chuàng)建序列,二是在model中使用這個(gè)序列,具體辦法如下:INCREMENTBY1CACHEUseruser=newUser().set("id","my_seq.nextval").set("age",18);Integerid=yourModel.set(主鍵名,序列名)ActiveRecordPlugin可同時(shí)支持多數(shù)據(jù)源、多方言、多緩存、多事務(wù)級(jí)別等特性,對(duì)每個(gè)ActiveRecordPlugin可進(jìn)行彼此獨(dú)立的配置。簡(jiǎn)言之JFinal可以同時(shí)使用多數(shù)據(jù)源,并且可publicpublicvoidconfigPlugin(Pluginsme)mysqlC3p0PlugindsMysql=new//mysqlActiveRecrodPlugin實(shí)例,并指定configName為mysqlActiveRecordPluginarpMysqlnewActiveRecordPlugin("mysql"dsMysql);arpMysql.setCache(newEhCache()); oracleC3p0PlugindsOracle=new//oracleActiveRecrodPlugin實(shí)例,并指定configName為oraclearpOracle.addMap("blog",Blog.class);}ModelModelActiveRecrodPlugin實(shí)例以及相關(guān)dsMysqlList<Record>users=Db.use("mysql").find("select*fromdsOracleList<Record>blogs=Db.use("oracle").find("select*fromAPIDb+Record來(lái)為DbKit.MAIN_CONFIG_NAME常量來(lái)設(shè)置主配置。EhCachePluginJFinalEhCachePlugin可以提高系統(tǒng)的并發(fā)EhCachePluginJFinalPluginJFinalConfigpublicpublicclassDemoConfigextendsJFinalConfigpublicvoidconfigPlugin(Plugins{me.add(new}}publicvoidlist()List<Blog>blogList=Blog.dao.find("selectpublicvoidlist()List<Blog>blogList=Blog.dao.find("select*fromblog");Useruser=User.dao.findById(getParaToInt());setAttr("blogList",blogList);}時(shí)斜杠”/”CacheInterceptorCacheName注解配合使用,以此來(lái)取代publicvoidlist()List<Blog>blogList=Blog.dao.find("select*fromblog");setAttr("blogList",blogList);}以上用法需要在ehcache.xml中配置名為blogList的cache<cachename="blogListpublicvoid}自動(dòng)更新cacheName為blogList的緩存數(shù)據(jù)。publicpublicvoidlist()List<Blog>blogList=CacheKit.get("blog",if(blogList==null)blogList=Blog.dao.find("select*fromblog");CacheKit.put("blog","blogList",blogList);}}CacheKit中最重要的兩個(gè)方法是get(StringcacheName,Objectkey)與put(StringObjectkey,Objectvalue)。getcache中取數(shù)據(jù),putcache。參數(shù)到的key;參數(shù)value是被緩存的數(shù)據(jù)。publicpublicvoidlist()List<Blog>blogList=CacheKit.get("blog","blogList",publicObjectload()returnBlog.dao.find("select*from}cacheName=blogkey=blogLi

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論