1. Trang chủ
  2. » Công Nghệ Thông Tin

Head First Servlets and JSP phần 8 potx

74 325 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 74
Dung lượng 2,13 MB

Nội dung

 FKDSWHU %XWKRZGR\RXORRSRYHUWKH ERG\",WORRNVOLNHGR6WDUW7DJLV FDOOHGWRRHDUO\DQGGR(QG7DJLVWRR ODWHDQG,GRQ·WKDYHDQ\ZD\WRNHHS UH LQYRNLQJWKHERG\HYDOXDWLRQ SDFNDJHDQGLPSRUWV SXEOLFFODVV&ODVVLF7HVWH[WHQGV7DJ6XSSRUW^ SXEOLFLQWGR6WDUW7DJWKURZV-VS([FHSWLRQ^ UHWXUQ (9$/B%2'<B,1&/8'( ` SXEOLFLQWGR(QG7DJWKURZV-VS([FHSWLRQ^ UHWXUQ(9$/B3$*( ` ` SDFNDJHDQGLPSRUWV SXEOLFFODVV6LPSOH7DJ7HVWH[WHQGV6LPSOH7DJ6XSSRUW^ SXEOLFYRLGGR7DJWKURZV-VS([FHSWLRQ,2([FHSWLRQ^ IRULQWL LL^ JHW-VS%RG\LQYRNHQXOO ` ` ` &ODVVLFWDJ 6LPSOHWDJ But where do you loop over the body, if the body is evaluated in between the methods instead of IN a method like doTag()? ItÕs easy to loop the body of a Simple tag; you just keep calling invoke() on the body, from within doTag(). LWHUDWLQJZLWK&ODVVLFWDJV" Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH  3IMPLETAGSARESIMPLEITSALLABOUTDO4AG"UTWITHCLASSICTAGS THERESADO3TART4AGANDADO%ND4AG!NDTHATBRINGSUPAN INTERESTINGPROBLEMWHENANDHOWISTHEBODYEVALUATED4HERES NODO"ODYMETHODBUTTHEREISADO!FTER"ODYMETHODTHATS CALLEDAFTERTHEBODYISEVALUATEDANDBEFORETHEDO%ND4AGRUNS +TI[[QK\IO[PI^MILQNNMZMV\TQNMKaKTM &RQWDLQHU :HE&RQWDLQHU &ODVVLFWDJFODVV 7DJ+DQGOHU2EMHFW These happen the Þ rst time the tag is invoked, but the Container may (depending on the circumstances) reuse the Classic tag object after this. This gives the handler a reference to a PageContext. If the tag is invoked with attributes, the JavaBean-style setter for each attribute is called (just as with SimpleTag handlers). A nested tag can communicate with the other tags in which its nested. doAfterBody() lets you do things AFTER the body runs, and unlike the other methods it can be invoked more than once. The body is evaluated between the doStartTag() and doEndTag() methods.         &ODVVLF+DQGOHUFODVV /RDGFODVV ,QVWDQWLDWHFODVVQRDUJFRQVWUXFWRUUXQV &DOOWKHVHW3DJH&RQWH[W3DJH&RQWH[WPHWKRG ,IWKHWDJKDVDWWULEXWHVFDOODWWULEXWHVHWWHUV &DOOWKHGR6WDUW7DJPHWKRG &DOOWKHGR(QG7DJPHWKRG ,IWKHWDJLVQHVWHGLQYRNHGIURPZLWKLQDQRWKHU WDJFDOOWKHVHW3DUHQW7DJPHWKRG ,IWKHWDJLV127GHFODUHGWRKDYHDQHPSW\ ERG\$1'WKHWDJLV127LQYRNHGZLWKDQHPSW\ ERG\$1'WKHGR6WDUW7DJPHWKRGUHWXUQV (9$/B%2'<B,1&/8'(WKHERG\LVHYDOXDWHG ,IWKHERG\FRQWHQWZDVHYDOXDWHGFDOOWKH GR$IWHU%RG\PHWKRG doEndTag() is always called once, either after doStartTag() or after doAfterBody(). Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU 4HEDO3TART4AGANDDO%ND4AGMETHODSRETURNANINT4HATINTTELLSTHE #ONTAINERWHATTODONEXT7ITHDO3TART4AGTHEQUESTIONTHE#ONTAINER ASKSISh3HOULD)EVALUATETHEBODYvASSUMINGTHEREISONEANDASSUMING THE4,$DOESNTDECLARETHEBODYASEMPTY 7ITHDO%ND4AGTHE#ONTAINERASKSh3HOULD)KEEPEVALUATINGTHERESTOF THECALLINGPAGEv4HERETURNVALUESAREREPRESENTEDBYCONSTANTSDECLARED INTHE4AGAND)TERATION4AGINTERFACES <PM+TI[[QKTQNMKaKTMLMXMVL[WVZM\]ZV^IT]M[ GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1&/8'( GR(QG7DJ 6.,3B3$*( (9$/B3$*( 3RVVLEOHUHWXUQYDOXHVZKHQ \RXH[WHQG7DJ6XSSRUW GR$IWHU%RG\ 6.,3B%2'< (9$/B%2'<B$*$,1 GR$IWHU%RG\ GR6WDUW7DJ (YDOXDWH%2'< GR(QG7DJ (YDOXDWH3$*( UHWXUQ(9$/B%2'<B,1&/8'( UHWXUQ6.,3B%2'< UHWXUQ6.,3B%2'< UHWXUQ(9$/B%2'<B$*$,1 UHWXUQ(9$/B3$*( 'RQH UHWXUQ6.,3B3$*( This is the only return value constant declared in IterationTag (the others are all from Tag).  :LWKGR6WDUW7DJWKHUHWXUQYDOXHVDUH 6.,3B%2'<DQG(9$/B%2'<B,1&/8'(%XWZLWK GR(QG7DJWKHYDOXHVDUH6.,3B3$*(DQG (9$/B3$*( ,IWKHQDPHVZHUHFRQVLVWHQWGR(QG7DJZRXOGUHWXUQ (9$/B3$*(B,1&/8'(DVRSSRVHGWR(9$/B3$*( WRPDWFKWKHZD\GR6WDUW7DJUHWXUQV(9$/B%2'<B ,1&/8'(%XWLW¶VQRW6RGRQ¶WEHIRROHGLI\RXVHHFRGH RQWKHH[DPZLWKFRUUHFWORRNLQJEXWZURQJUHWXUQYDOXHV 7KHFRQVWDQWVXVHGDVUHWXUQ YDOXHVIRUGR6WDUW7DJDQG GR(QG7DJUHWXUQYDOXHFRQVWDQWV DUHLQFRQVLVWHQWO\QDPHG Returning SKIP_PAGE from doEndTag() is exactly like throwing a SkipPageException from a Simple tag! If a page included the page that invoked the tag, the current (included) page stops processing, but the including page continues &ODVVLFWDJOLIHF\FOH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH  7HENYOUWRITEATAGHANDLERTHATEXTENDS4AG3UPPORTYOUGETALLTHELIFECYCLEMETHODSFROMTHE 4AGINTERFACEPLUSTHEONEMETHODFROM)TERATION4AGDO!FTER"ODY7ITHOUTDO!FTER"ODY YOUCANTITERATEOVERTHEBODYBECAUSEDO3TART4AGISTOOEARLYANDDO%ND4AGISTOOLATE "UTWITHDO!FTER"ODYYOURRETURNVALUETELLSTHE#ONTAINERWHETHERITSHOULDREPEATTHEBODY AGAIN%6!,?"/$9?!'!).ORCALLTHEDO%ND4AGMETHOD3+)0?"/$9 1 \ M Z I \ Q W V < I O  T M \ [  a W ]ZMXMI\\PMJWLa 7DJLQWHUIDFH LQWGR(QG7DJ 7DJJHW3DUHQW LQWGR6WDUW7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W YRLGVHW3DUHQW7DJ YRLGUHOHDVH LQWHUIDFH!! 7DJ ,WHUDWLRQ7DJLQWHUIDFH LQWGR$IWHU%RG\ LQWHUIDFH!! ,WHUDWLRQ7DJ YRLGGR$IWHU%RG\ LQWGR6WDUW7DJ LQWGR(QG7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W PRUHPHWKRGV 7DJ6XSSRUW 7DJ6XSSRUWFODVV 6KDUSHQ \RXU SHQFLO SXEOLFYRLGGR7DJWKURZV-VS([FHSWLRQ,2([FHSWLRQ^ 6WULQJ>@PRYLHV ^³6SLGHUPDQ´³6DYHG´³$PHOLH´` IRULQWL LPRYLHVOHQJWKL^ JHW-VS&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>L@ JHW-VS%RG\LQYRNHQXOO ` ` 7U\WRPSOHPHQWWKHVDPHIXQFWLRQDOLW\RIWKLV 6LPSOH7DJGR7DJLQD&ODVVLFWDJKDQGOHU$VVXPH WKH7/'LVFRQILJXUHGWRDOORZERG\FRQWHQW // package and imports public class MyIteratorTag extends TagSupport { public int doStartTag() throws JspException { public int doAfterBody() throws JspException { public int doEndTag() throws JspException { } Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU /RRNDWWKHOHJDOWDJKDQGOHUFRGHEHORZDQG ILJXUHRXWZKHWKHULWZRXOGJLYH\RXWKHUHVXOW VKRZQJLYHQWKH-63WDJLQYRFDWLRQ OLVWHGEHORZ7KLVLVDOVRWKHVDPH UHVXOWSURGXFHGE\WKH&ODVVLF7DJ KDQGOHUIURPWKHSUHYLRXVSDJH<HV ZH·UHDQVZHULQJWKH6KDUSHQ<RXU 3HQFLOZLWK\HWDQRWKHUH[HUFLVH %(WKH&RQWDLQHU SDFNDJHDQGLPSRUWV SXEOLFFODVV0\,WHUDWRU7DJH[WHQGV7DJ6XSSRUW^ 6WULQJ>@PRYLHV QHZ6WULQJ>@^³6SLGHUPDQ´³6DYHG´³$PHOLH´` LQWPRYLH&RXQWHU SXEOLFLQWGR6WDUW7DJWKURZV-VS([FHSWLRQ^ PRYLH&RXQWHU   UHWXUQ(9$/B%2'<B,1&/8'( ` SXEOLFLQWGR$IWHU%RG\WKURZV-VS([FHSWLRQ^ LIPRYLH&RXQWHUPRYLHVOHQJWK^ SDJH&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>PRYLH&RXQWHU@ PRYLH&RXQWHU UHWXUQ(9$/B%2'<B$*$,1 `HOVH^ UHWXUQ6.,3B%2'< ` ` SXEOLFLQWGR(QG7DJWKURZV-VS([FHSWLRQ^ UHWXUQ(9$/B3$*( ` ` 7KHWDJKDQGOHUFODVV :HWDNHWKHVWLQJRXWRI62$3 KWWSORFDOKRVWWHVWV3DJH$MVS &RQWDFWXVDWOLNHZHFDUH#ZLFNHGO\VPDUWFRP 6SLGHUPDQ 6DYHG $PHOLH 'HVLUHGUHVXOW-63WKDWLQYRNHVWKHWDJ #WDJOLESUH¿[ ´PLQH´XUL ´.DWK\&ODVVLF7DJV´! KWPO!ERG\! WDEOHERUGHU ´´! PLQHLWHUDWH0RYLHV! WU!WG!^PRYLH`WG!WU! PLQHLWHUDWH0RYLHV! WDEOH! ERG\!KWPO! &ODVVLFWDJ H[HUFLVH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH  )FYOUDONTOVERRIDETHE4AG3UPPORTLIFECYCLEMETHODSTHATRETURN ANINTEGERBEAWAREOFTHEDEFAULTVALUESTHE4AG3UPPORTMETHOD IMPLEMENTATIONSRETURN4HE4AG3UPPORTCLASSASSUMESTHATYOURTAG DOESNTHAVEABODYBYRETURNING3+)0?"/$9FROMDO3TART4AG ANDTHATIFYOU$/HAVEABODYTHATSEVALUATEDYOUWANTIT EVALUATEDONLYONCEBYRETURNING3+)0?"/$9FROMDO!FTER"ODY )TALSOASSUMESTHATYOUWANTTHERESTOFTHEPAGETOEVALUATEBY RETURNING%6!,0!'%FROMDO%NDTAG ,MNI]T\ZM\]ZV^IT]M[NZWU<IO;]XXWZ\ GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1&/8'( GR(QG7DJ 6.,3B3$*( (9$/B3$*( 'HIDXOWUHWXUQYDOXHVZKHQ\RXGRQ·WRYHUULGHWKH 7DJ6XSSRUWPHWKRGLPSOHPHQWDWLRQ GR$IWHU%RG\ 6.,3B%2'< (9$/B%2'<B$*$,1 7KH7DJ6XSSRUWFODVV DVVXPHV\RXUWDJGRHVQ·W KDYHDERG\RUWKDWLIWKH ERG\,6HYDOXDWHGWKDWWKH ERG\VKRXOGEHHYDOXDWHG RQO\21&( ,WDOVRDVVXPHVWKDW\RX DOZD\VZDQWWKHUHVWRIWKH SDJHWREHHYDOXDWHG  <RXUHDOO\PXVWNQRZWKLVOLIHF\FOHIRUWKH H[DP'RQ¶WIRUJHWWKDWGR6WDUW7DJDQG GR(QG7DJDUHDOZD\VFDOOHGDQGWKH\¶UH FDOOHGRQO\RQFHUHJDUGOHVVRIDQ\WKLQJ HOVHWKDWKDSSHQV%XWGR$IWHU%RG\ FDQUXQIURPWRPDQ\WLPHVGHSHQGLQJ RQWKHUHWXUQYDOXHRIGR6WDUW7DJDQG SUHYLRXVGR$IWHU%RG\FDOOV GR6WDUW7DJDQG GR(QG7DJUXQ H[DFWO\RQFH  7KLQNDERXWLW7KHGHIDXOWUHWXUQYDOXHIURP GR6WDUW7DJLV6.,3B%2'<VRLI\RXZDQWWKHERG\ RI\RXUWDJHYDOXDWHGDQG\RXH[WHQG7DJ6XSSRUW \RX0867RYHUULGHGR6WDUW7DJLIIRUQRRWKHUUHDVRQ WKDQWRUHWXUQ(9$/B%2'<B,1&/8'( :LWKGR$IWHU%RG\LWVKRXOGEHREYLRXVWKDWLI\RX ZDQWWRLWHUDWHRYHUWKHERG\\RXKDYHWRRYHUULGHWKDW PHWKRGDVZHOOVLQFHLWVUHWXUQYDOXHLV6.,3B%2'< <RX0867RYHUULGHGR6WDUW7DJ LI\RXZDQWWKHWDJERG\WREH HYDOXDWHG Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU %(WKH&RQWDLQHU$QVZHU SXEOLFFODVV0\,WHUDWRU7DJH[WHQGV7DJ6XSSRUW^ 6WULQJ>@PRYLHV QHZ6WULQJ>@^³6SLGHUPDQ´³6DYHG´³$PHOLH´` LQWPRYLH&RXQWHU SXEOLFLQWGR6WDUW7DJWKURZV-VS([FHSWLRQ^ PRYLH&RXQWHU   SDJH&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>PRYLH&RXQWHU@ PRYLH&RXQWHU UHWXUQ(9$/B%2'<B,1&/8'( ` SXEOLFLQWGR$IWHU%RG\WKURZV-VS([FHSWLRQ^ LIPRYLH&RXQWHUPRYLHVOHQJWK^ SDJH&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>PRYLH&RXQWHU@ PRYLH&RXQWHU UHWXUQ(9$/B%2'<B$*$,1 `HOVH^ UHWXUQ6.,3B%2'< ` ` SXEOLFLQWGR(QG7DJWKURZV-VS([FHSWLRQ^ UHWXUQ(9$/B3$*( ` ` 7KHWDJKDQGOHUFODVV :HWDNHWKHVWLQJRXWRI62$3 KWWSORFDOKRVWWHVWV3DJH$MVS &RQWDFWXVDWOLNHZHFDUH#ZLFNHGO\VPDUWFRP 6SLGHUPDQ 6DYHG $PHOLH 'HVLUHGUHVXOW -63WKDWLQYRNHVWKHWDJ #WDJOLESUH¿[ ´PLQH´XUL ´.DWK\&ODVVLF7DJV´! KWPO!ERG\! WDEOHERUGHU ´´! PLQHLWHUDWH0RYLHV! WU!WG!^PRYLH`WG!WU! PLQHLWHUDWH0RYLHV! WDEOH! ERG\!KWPO! $FWXDOUHVXOWXQOHVV\RXDGGWKH WZROLQHVKLJKOLJKWHGEHORZ :HWDNHWKHVWLQJRXWRI62$3 KWWSORFDOKRVWWHVWV3DJH$MVS &RQWDFWXVDWOLNHZHFDUH#ZLFNHGO\VPDUWFRP 6SLGHUPDQ 6DYHG $PHOLH ThereÕs an empty cell at the top! You MUST add these two lines to produce the correct response. This doAfterBody() method was correct, but it runs only AFTER the body has already been processed once! Without the two extra lines in doStartTag(), the body is processed once without there being a movie attribute, so you get the empty cell. &ODVVLFWDJH[HUFLVHDQVZHUV Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH  WKHUH DUH QR ' XPE 4XHVWLRQV 4  4HISSEEMSSTUPIDTHERES DUPLICATECODEINDO3TART4AGAND DO!FTER"ODY  $ 9ESTHERESDUPLICATECODE )NTHISCASEIFYOUREIMPLEMENTING 4AG3UPPORTANDYOUWANTTOSET VALUESTHEBODYCANUSETHENYOU -534SETTHOSEATTRIBUTEVALUESIN DO3TART4AG9OUCANTWAITUNTIL DO!FTER"ODYBECAUSEBYTHETIME YOUGETTODO!FTER"ODYTHEBODY HASALREADYBEENPROCESSEDONCE 9ESITSKINDOFSTUPID7HICHISWHY 3IMPLE4AGISSOMUCHBETTER/F COURSEIFYOUWEREWRITINGTHECODE YOUDMAKEAPRIVATEMETHODINYOUR TAGHANDLERSAYSET-OVIEAND YOUDCALLTHATMETHODFROMBOTH DO3TART4AGANDDO!FTER"ODY"UT ITSSTILLANAWKWARDAPPROACH 4   7(9AREYOUSETTING THEINSTANCEVARIABLEVALUE FORMOVIE#OUNTER).3)$%THE DO3TART4AGMETHOD7HYCANTYOU JUSTINITIALIZEITWHENYOUDECLAREIT $ 9IKES5NLIKE3IMPLE4AG HANDLERSWHICHARENEVERREUSEDA #LASSICTAGHANDLERCANBEPOOLED ANDREUSEDBYTHE#ONTAINER4HAT MEANSYOUDBETTERRESETYOUR INSTANCEVARIABLEVALUESWITHEACH NEWTAGINVOCATIONWHICHMEANSIN DO3TART4AG/THERWISETHISCODE WORKSTHEFIRSTTIMEBUTTHENEXTTIME A*30INVOKESITTHEMOVIE#OUNTER VARIABLEWILLSTILLHAVEITSLASTVALUE INSTEADOF  :DWFKRXW²WKLVLVFRPSOHWHO\GLIIHUHQW IURP6LPSOH7DJKDQGOHUVZKLFKDUH GH¿QLWHO\127UHXVHG7KDWPHDQV\RX KDYHWREHYHU\FDUHIXODERXWLQVWDQFH YDULDEOHV²\RXVKRXOGUHVHWWKHPLQ GR6WDUW7DJ 7KH7DJLQWHUIDFHGRHVKDYHDUHOHDVH PHWKRGEXWWKDW¶VFDOOHGRQO\ZKHQ WKHWDJKDQGOHULQVWDQFHLVDERXWWREH UHPRYHGE\WKH&RQWDLQHU6RGRQ¶W DVVXPHWKDWUHOHDVHLVDZD\WRUHVHW WKHWDJKDQGOHU¶VVWDWHLQEHWZHHQWDJ LQYRFDWLRQV 7KH&RQWDLQHUFDQ UHXVH&ODVVLFWDJ KDQGOHUV Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU 9OULLPROBABLYlNDTHATMOSTOFTHETIMETHELIFECYCLEMETHODSFROMTHE4AGAND )TERATION4AGINTERFACESASPROVIDEDBY4AG3UPPORTAREENOUGH"ETWEENTHETHREEKEY METHODSDO3TART4AGDO!FTER"ODYANDDO%ND4AGYOUCANDOJUSTABOUTANYTHING %XCEPTYOUDONTHAVEDIRECTACCESSTOTHECONTENTSOFTHEBODY)FYOUNEEDACCESSTOTHE ACTUALBODYCONTENTSSOTHATYOUCANSAYUSEITINANEXPRESSIONORPERHAPSlLTERORALTERIT INSOMEWAYTHENEXTEND"ODY4AG3UPPORTINSTEADOF4AG3UPPORTANDYOULLHAVEACCESSTO THE"ODY4AGINTERFACEMETHODS *]\_PI\QNaW],7VMMLIKKM[[\W\PMJWLaKWV\MV\[' LQWGR(QG7DJ 7DJJHW3DUHQW LQWGR6WDUW7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W YRLGVHW3DUHQW7DJ YRLGUHOHDVH LQWHUIDFH!! 7DJ ,WHUDWLRQ7DJLQWHUIDFH LQWGR$IWHU%RG\ LQWHUIDFH!! ,WHUDWLRQ7DJ %RG\7DJLQWHUIDFH YRLGGR,QLW%RG\ YRLGVHW%RG\&RQWHQW%RG\&RQWHQW LQWHUIDFH!! %RG\7DJ YRLGGR$IWHU%RG\ LQWGR6WDUW7DJ LQWGR(QG7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W PRUHPHWKRGV 7DJ6XSSRUW 7DJ6XSSRUWFODVV LQWGR6WDUW7DJ %RG\&RQWHQWJHW%RG\&RQWHQW YRLGGR,QLW%RG\ YRLGVHW%RG\&RQWHQW%RG\&RQWHQW PRUHPHWKRGV %RG\7DJ6XSSRUW %RG\7DJ6XSSRUWFODVV ([WHQGLQJ%RG\7DJ6XSSRUW JLYHV\RXWZRPRUHOLIHF\FOH PHWKRGVIURPWKH%RG\7DJ LQWHUIDFH³VHW%RG\&RQWHQW DQGGR,QLW%RG\<RXFDQ XVHWKHVHWRGRVRPHWKLQJ ZLWKWKHDFWXDO&217(176 RIWKHERG\RIWKHWDJXVHG WRLQYRNHWKHKDQGOHU %RG\7DJLQWHUIDFH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH  7HENYOUIMPLEMENT"ODY4AGBYEXTENDING"ODY4AG3UPPORTYOUGET TWOMORELIFECYCLEMETHODSSET"ODY#ONTENTANDDO)NIT"ODY9OUALSO GETONENEWRETURNVALUEFORDO3TART4AG%6!,?"/$9?"5&&%2%$4HAT MEANSTHEREARENOWTHREEPOSSIBLERETURNVALUESFORDO3TART4AGINSTEAD OFTHETWOYOUGETWHENYOUEXTEND4AG3UPPORT ?Q\P*WLa<IOaW]OM\\_WVM_UM\PWL[ G R$IWHU%RG \  G R6WDUW7DJ (YDOXDWH%2'< GR(QG7DJ (YDOXDWH3$*( UHWXUQ(9$/B%2'<B,1&/8'( 2 UHWXUQ6.,3B%2'< XUQ6 UHWXUQ6.,3B%2'< , UHWXUQ(9$/B%2'<B$*$,1 9$ UHWXUQ(9$/B3$*( $ 'RQH UHWXUQ6.,3B3$*( UQ6 VHW%RG\&RQWHQW GR,QLW%RG\ UHWXUQ (9$/B%2'<B%8))(5(' /LIHF\FOHIRUDWDJWKDWLPSOHPHQWV%RG\7DJ GLUHFWO\RUE\H[WHQGLQJ%RG\7DJ6XSSRUW GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1&/8'( (9$/B%2'<B%8))(5(' $FKDQJHLQWKHGHIDXOWUHWXUQ YDOXHIRU%RG\7DJ6XSSRUW New return value, and itÕs the default for BodyTagSupport. (Instead of SKIP_BODY, the default for TagSupport.) Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. [...]...Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... prior written permission for reprints and excerpts from the publisher Redistribution or other use that violates the fair use priviledge under U.S copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... prior written permission for reprints and excerpts from the publisher Redistribution or other use that violates the fair use priviledge under U.S copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... prior written permission for reprints and excerpts from the publisher Redistribution or other use that violates the fair use priviledge under U.S copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... prior written permission for reprints and excerpts from the publisher Redistribution or other use that violates the fair use priviledge under U.S copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... prior written permission for reprints and excerpts from the publisher Redistribution or other use that violates the fair use priviledge under U.S copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan . prohibited. FXVWRPWDJGHYHORSPHQW RXDUHKHUH  )FYOUDONTOVERRIDETHE4AG3UPPORTLIFECYCLEMETHODSTHATRETURN ANINTEGERBEAWAREOFTHEDEFAULTVALUESTHE4AG3UPPORTMETHOD IMPLEMENTATIONSRETURN4HE4AG3UPPORTCLASSASSUMESTHATYOURTAG DOESNTHAVEABODYBYRETURNING3+)0?"/$9FROMDO3TART4AG AND THATIFYOU$/HAVEABODYTHATSEVALUATEDYOUWANTIT EVALUATEDONLYONCEBYRETURNING3+)0?"/$9FROMDO!FTER"ODY )TALSOASSUMESTHATYOUWANTTHERESTOFTHEPAGETOEVALUATEBY RETURNING%6!,0!'%FROMDO%NDTAG ,MNI]TZM]ZV^IT]M[NZWU<IO;]XXWZ GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1& /8& apos;( GR(QG7DJ 6.,3B3$*( (9$/B3$*( 'HIDXOWUHWXUQYDOXHVZKHQRXGRQ·WRYHUULGHWKH 7DJ6XSSRUWPHWKRGLPSOHPHQWDWLRQ GR$IWHU%RG 6.,3B%2'< (9$/B%2'<B$*$,1 7KH7DJ6XSSRUWFODVV DVVXPHVRXUWDJGRHVQ·W KDYHDERGRUWKDWLIWKH ERG,6HYDOXDWHGWKDWWKH ERGVKRXOGEHHYDOXDWHG RQO21&( ,WDOVRDVVXPHVWKDWRX DOZDVZDQWWKHUHVWRIWKH SDJHWREHHYDOXDWHG  <RXUHDOOPXVWNQRZWKLVOLIHFFOHIRUWKH H[DP'RQ¶WIRUJHWWKDWGR6WDUW7DJDQG GR(QG7DJDUHDOZDVFDOOHGDQGWKH¶UH FDOOHGRQORQFHUHJDUGOHVVRIDQWKLQJ HOVHWKDWKDSSHQV%XWGR$IWHU%RG FDQUXQIURPWRPDQWLPHVGHSHQGLQJ RQWKHUHWXUQYDOXHRIGR6WDUW7DJDQG SUHYLRXVGR$IWHU%RGFDOOV GR6WDUW7DJDQG GR(QG7DJUXQ H[DFWORQFH  7KLQNDERXWLW7KHGHIDXOWUHWXUQYDOXHIURP GR6WDUW7DJLV6.,3B%2'<VRLIRXZDQWWKHERG RIRXUWDJHYDOXDWHGDQGRXH[WHQG7DJ6XSSRUW RX 086 7RYHUULGHGR6WDUW7DJLIIRUQRRWKHUUHDVRQ WKDQWRUHWXUQ(9$/B%2'<B,1& /8& apos;( :LWKGR$IWHU%RGLWVKRXOGEHREYLRXVWKDWLIRX ZDQWWRLWHUDWHRYHUWKHERGRXKDYHWRRYHUULGHWKDW PHWKRGDVZHOOVLQFHLWVUHWXUQYDOXHLV6.,3B%2'< <RX 086 7RYHUULGHGR6WDUW7DJ LIRXZDQWWKHWDJERGWREH HYDOXDWHG Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By. prohibited.  FKDSWHU <RXGRQ¶WQHHGWR NQRZDOOWKHGHWDLOVRI XVLQJ%RG7DJ6XSSRUW )RUWKHH[DPDQGSUREDEOIRUWKHUHVW RIRXU-63GHYHORSPHQWOLIHRXQHHG WRNQRZWKHOLIHFFOHIRU%RG7DJ6XSSRUW DQGKRZLWGLIIHUVIURP7DJ6XSSRUW<RX QHHGWRNQRZIRUH[DPSOHWKDWLIRX GR127H[WHQG%RG7DJ6XSSRUWRU LPSOHPHQW%RG7DJWKHQRXPXVW127 UHWXUQ(9$/B%2'<B %8) )(5('IURP GR6WDUW7DJ$QGRXVKRXOGNQRZWKH WZRQHZPHWKRGVIURPWKH%RG7DJ LQWHUIDFHEXWWKDW¶VDERXWLW 4HE"ODY#ONTENTARGUMENTTOSET"ODY#ONTENTISACTUALLYA TYPEOFJAVAIO7RITER9ESITS/+TOlNDTHATDISTURBINGFROMAN //PERSPECTIVE"UTTHATMEANSYOUCANPROCESSTHEBODYBYSAY CHAININGITTOANOTHER)/STREAMORGETTINGTHERAWBYTES ?QP*WLa<IOaW]KIVJ]NNMZPMJWLa 4  7HATHAPPENSIF)RETURN %6!,?"/$9?"5&&%2%$EVENTHOUGHTHE INVOKINGTAGISEMPTY $ 4HESET"ODY#ONTENT AND DO)NIT"ODYMETHODWILLNOTBECALLED IFTHETAGINVOKINGTHEHANDLERISEMPTY !NDBYEMPTYWEMEANTHATTHETAGWAS INVOKEDUSINGANEMPTYTAGMYTAGOR WITHNOCONTENTBETWEENTHEOPENING AND CLOSINGTAGSMYTAGMYTAG 4HE#ONTAINERKNOWSTHERESNOBODYTHIS TIME AND ITJUSTSKIPSTOTHEDO%ND4AG METHODSOTHISISUSUALLYNOTAPROBLEM 5NLESSTHE4,$DECLARESTHETAGTOHAVEAN EMPTYBODY)FTHE4,$SAYS BODYCONTENTEMPTYBODYCONTENT YOUDONTHAVEACHOICE AND YOUMUST./4 RETURN%6!,?"/$9?"5&&%2%$OR %6!,?"/$9?).#,5$%FROMDO3TART4AG 4  7HATABOUTATTRIBUTESINA#LASSIC TAG!RETHEYHANDLEDTHESAMEWAYAS WITH3IMPLETAGS $ 9ESONTHESEQUENCEDIAGRAMFOR BOTH3IMPLETAGHANDLERS AND #LASSICTAG HANDLERSTHEREWASAPLACEWHEREBEAN STYLESETTERMETHODSARECALLEDFOREACH ATTRIBUTE4HISHAPPENSBEFOREA3IMPLE TAGSDO4AGORA#LASSICTAGSDO3TART4AG )NOTHERWORDSTAGATTRIBUTESWORKIN EXACTLYTHESAMEWAYFORBOTH#LASSIC AND 3IMPLETAGSINCLUDINGTHEWAYINWHICH THEYREDECLAREDINTHE4,$  7KDWPLJKWEHREYLRXVEXWLWPHDQVRXKDYHWREHFDUHIXOWR NHHSRXUWDJKDQGOHUDQG7/'LQVQF6RLIRXGHFODUHD WDJLQWKH7/'WRKDYHERGFRQWHQW!HPSWERGFRQWHQW! WKHQWKHUHLVDEVROXWHO12SRLQWLQLPSOHPHQWLQJ%RG7DJRU H[WHQGLQJ%RG7DJ6XSSRUW7KDWDOVRPHDQVWKHUHLVQRSRLQW LQLPSOHPHQWLQJ,WHUDWLRQ7DJEXWRXJHWWKDWDXWRPDWLFDOOE H[WHQGLQJ7DJ6XSSRUW 7KHSRLQWLVRXQHHGWRUHWXUQ6.,3B%2'<IURPGR6WDUW7DJ LIRXU7/'GHFODUHVDQHPSWERGIRUWKHWDJHYHQ,)RX LPSOHPHQW,WHUDWLRQ7DJRU%RG7DJ ,IWKH7/'IRUDWDJGHFODUHVDQ HPSWERGGR6WDUW7DJ 086 7 UHWXUQ6.,3B%2'< %RG7DJDQG%RG7DJ6XSSRUW Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates,. &ODVVLFWDJOLIHFFOH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared

Ngày đăng: 12/08/2014, 19:20

TỪ KHÓA LIÊN QUAN