ºìÁªLinuxÃÅ»§
Linux°ïÖú

¡¾ÐÂÊÖÈëÃÅ¡¿JSPÖбêÇ©¿âµÄÉîÈëÑо¿

·¢²¼Ê±¼ä:2006-05-29 06:24:56À´Ô´:ºìÁª×÷Õß:Rstuosd
±êÇ©¿âTaglib

±êÇ©±»¶¨ÒåºÍ·Ö²¼ÔÚÒ»¸ö³ÆΪ±êÇ©¿âµÄ½á¹¹ÖУ¬Ò»¸ö±êÇ©¿âÊÇÓÉÔªÐÅÏ¢ºÍÀà×é³ÉµÄ¼¯ºÏ£º
1.±êÇ©´¦ÀíÆ÷£ºÊµÏÖ¶¨ÖƱêÇ©¹¦ÄܵÄJavaÀà¡£
2.±êÇ©¸½¼ÓÐÅÏ¢£¨TEI£©£ºÏòJSPÈÝÆ÷Ìṩ±ß¼­ÒÔÈ·ÈϱêÇ©ÊôÐԺʹ´½¨±äÁ¿µÄÀà¡£
3.±êÇ©¿âÃèÊöÆ÷£¨TLD£©£ºÃèÊöµ¥¸ö±êÇ©ºÍÕû¸ö±êÇ©¿âÊôÐÔµÄXMLÎĵµ¡£

±êÇ©´¦ÀíÆ÷ºÍ±êÇ©¸½¼ÓÐÅÏ¢ÐèÒª¶¨Î»ÔÚJSPÈÝÆ÷ÀàÔØÈëÆ÷¿ÉÒÔÕÒµ½µÄµØ·½¡£±êÇ©¿âÃèÊöÆ÷¿ÉÔÚURLÖ¸¶¨µÄ·ûÒâλÖá£JSP1.1¹æ·¶ÒªÇóJSPÈÝÆ÷½ÓÊÜÒ»¸ö´ò°ü³ÉÒò¶¨½á¹¹µÄJARÎļþµÄ±êÇ©¿â¡£TLD±ØÐëÊÇ/META-INFĿ¼ÖÐÃûΪtaglib.tldµÄÎļþ£¬JARÎļþÔò¸´ÖƵ½/WEB-INF/libĿ¼Ï¡£

Ò»¡¢±êǩʵÏÖ

1.¿ª·¢²½Öè
a.¶¨Òå±êÇ©µÄÃû×Ö¡¢ÊôÐÔ¡¢ÉùÃ÷µÄ±äÁ¿ºÍ±êÇ©ÌåµÄÄÚÈÝ¡£
b.±àд±êÇ©¿âÃèÊöÆ÷TLD¡£
c.±àд±êÇ©´¦ÀíÆ÷¡£
d.ÔÚJSPÒ³ÃæÖÐʹÓñêÇ©¡£

2.JSPÒ³ÃæÔÚJSPÈÝÆ÷ÖеÄת»»²½Ö裺
JSPÒ³Ãæ´æÔÚÈýÖÖÐÎʽ£ºjspÎļþ¡¢javaÎļþºÍclassÎļþ¡£
a.Ö¸ÁîÔªËØ¡¢ºÍÏòJSPÈÝÆ÷Ìṩת»»Ê±ÐÅÏ¢¡£
b.HTMLÐÐÔÚ_jspService()·½·¨ÖÐÒÀ˳Ðòת»»µ½out.print()ÓïÃûÖС£
c.½Å±¾ÔªËصÄÉùÃ÷±»Ô­·â²»¶¯µØ¸´ÖƵ½_jspService()·½·¨ÍâµÄÔ´ÂëÖС£
d.½Å±¾ÔªËصıí´ïʽÔÚ_jspService()·½·¨ÖÐÒÀ˳Ðòת»»µ½out.print()ÓïÃûÖС£
e.½Å±¾ÔªËصÄScriptlet±»Ô­·â²»¶¯µØ¸´ÖƵ½_jspService()·½·¨ÖС£
f.ÐÐΪԪËر»×ª»»ÎªÖ´ÐÐÆ书ÄܵÄÔËÐÐʱÂß¼­´úÂë¡£
g.¶¨ÖƱêÇ©±»À©Õ¹µ½µ÷ÓÃÆäÏàÓ¦±êÇ©´¦ÀíÆ÷Öз½·¨µÄJavaÓï¾äÖС£

3.±êÇ©ÔÚJSPÈÝÆ÷ÖеÄת»»²½Ö裺
a.JSPÈÝÆ÷ʹÓÃtaglibÖ¸ÁîÔªËض¨Î»±êÇ©¿âÃèÊöÆ÷£¬½«Ò³ÃæÖÐÓõ½µÄ¶¨ÖƱêÇ©ºÍTLDÏàÆ¥Åä¡£
b.¶ÁÈ¡±êÇ©¿âÃèÊöÆ÷µÄ±êÇ©ÁбíºÍÿһ±êÇ©Ïà¹ØµÄÀàÃû×Ö¡£
c.ÔÚÒ³ÃæÖÐÓöµ½Ò»¸ö±êǩʱ£¬²éÕÒÓë¾ßÓÐÖ¸¶¨Ãû×ֵıêǩǰ׺Ïà¹ØµÄÒ»¸ö±êÇ©¿â¡£
d.ÈÝÆ÷ʹÓÃÔÚTLDÖÐÕÒµ½µÄ±êÇ©½á¹¹ÐÅÏ¢Éú³ÉһϵÁÐÍê³É±êÇ©¹¦ÄܵÄJavaÓï¾ä¡£


¶þ¡¢±êÇ©¿âÃèÊöÆ÷£¨TLD£©

±êÇ©¿âÃèÊöÆ÷ÊÇÒ»¸öÃèÊöÕû¸ö±êÇ©¿â±ê¼ÇÐÅÏ¢ºÍ¿âÖÐÿ¸ö±êÇ©´¦ÀíÆ÷¼°ÆäÊôÐÔµÄXMLÎĵµ¡£

±êÇ©¿âÃèÊöÆ÷µÄDTDÓÉÒ»¸ö¼òµ¥µÄÔªËØ×é³É£¬´ËÔªËØ°üº¬ÏÂÁÐһЩ×ÓÔªËØ¡£
Õû¸ö±êÇ©¿â±ê¼ÇÐÅÏ¢
tlibversion ±êÇ©¿â°æ±¾ºÅ¡£ÊÇÒ»¸öµãʽʮ½øÖÆÊý£¬×î¶àΪ4×éСÊýµã·Ö¸ôµÄÊý×Ö×é³É¡£
jspversion ±êÇ©¿âËùÐèµÄJSP¹æ·¶×îµÍ°æ±¾¡£ÀýÈçJSP1.1
shortname ±êÇ©¿âµÄËõдÃû¡£JSP¿ÉÒÔʹÓøÃÃû×Ö×÷Ϊ¿âÖбêÇ©µÄȱʡǰ׺¡£
uri ±êÇ©¿âΨһURIµÄÔªËØ¡£µäÐÍURLλÖÃÀ´×Ô¿ÉÏÂÔØtaglibµÄλÖá£
info ±êÇ©¿âÃèÊöÐÅÏ¢¡£
ÿ¸ö±êÇ©´¦ÀíÆ÷¼°ÆäÊôÐÔ
tag ÔÚTLDÖмÓÈë±êÇ©£¬ÃèÊö×é³É¿âµÄÿ¸ö±êÇ©¡£
name Óë±êÇ©¿âµÄÃû×Öǰ׺һÆðʹÓõıêÇ©µÄÃû×Ö£¬ ÊÇJSPÈÝÆ÷ΨһµÄ±êÇ©±êʶ¡£
tagclass ʵÏÖ±êÇ©µÄ±êÇ©´¦ÀíÆ÷ÀàµÄÈ«Ãû¡£
teiclass ±êÇ©¸½¼ÓÐÅÏ¢£¨TEI£©ÀàµÄÈ«Ãû¡£TEIÀà¸ø³ö¹ØÓÚ±êÇ©´¦ÀíÆ÷´´½¨±äÁ¿¼°¶Ô±êǩ˾ÐÔÖ´ÐеÄÈÎÒâÓÐЧÐÔÑéÖ¤µÄÐÅÏ¢¡£
bodycontent ÃèÊö±êÇ©´¦ÀíÆ÷ÈçºÎʹÓñêÇ©ÌåµÄÄÚÈÝ¡£ÓÐÈýÖÖÈ¡Öµ£º
empty£º±íʾ±êÇ©Ìå±ØÐëΪ¿Õ£»
JSP:±íʾ½Å±¾ÔªËغÍÄ£°å¼°ÆäËü±êÇ©Ò»Ñù±»ÆÀ¹À¡£
tagdependent£ºÌåÄÚÈݱ»Ô­·â²»¶¯Ð´ÈëBodyContent£¬ÆäËü½Å±¾ÔªËØÒÔÔ´ÂëÐÎʽ³öÏÖ£¬¶ø²»±»JSPÈÝÆ÷½âÊÍ¡£
info ±êÇ©µÄÈ˹¤¿É¶ÁÃèÊöÐÔÐÅÏ¢¡£
attribute ʹÓñêǩʱ±»±àÂëµÄÊôÐÔÐÅÏ¢¡£ÓÃÓÚ¶¨Òå±êÇ©µÄÊôÐÔ¡£
ÊôÐÔÃû£ºÊôÐÔµÄÃû×Ö¡£
true|false£ºÊôÐÔÔÚ±êÇ©Óõ½µÄλÖÃÊÇ·ñÒª±»±àÂë¡£
true|false£ºÊôÐÔÖµÄÜ·ñÓñí´ïʽָ¶¨¡£

Èý¡¢±êÇ©´¦ÀíÆ÷

±êÇ©´¦ÀíÆ÷ÊÇͨ¹ýʵÏÖJSPÈÝÆ÷µ÷ÓõÄһϵÁÐÔ¤¶¨Òå·½·¨Ö´Ðж¨ÖƱêÇ©ÐÐΪµÄÒ»¸öJavaÀà¡£
±êÇ©´¦ÀíÆ÷ʵÏÖÁ˱êÇ©µÄÐÐΪ£¬±êÇ©´¦ÀíÆ÷ÊÇJavaÀà¡£

1.±êÇ©´¦ÀíÆ÷µÄ¹¤×÷·½Ê½
a.µ¼Èëjavax.servlet.jspºÍjavax.servlet.jsp.tagext°ü¡£
b.ʵÏÖjavax.servlet.jsp.tagext°üÖеÄTag½Ó¿Ú»òBodyTag½Ó¿Ú¡£BodyTagÊÇTagµÄ×Ó½Ó¿Ú¡£
c.¼Ì³ÐTagSupportÀà»òBodyTagSuppoertÀà¡£ËüÃÇÊÇÉÏÊö½Ó¿ÚµÄȱʡʵÏÖ¡£
d.ÖØÔØpublic int doStartTag()throws JspException·½·¨¡£

2.±êÇ©´¦ÀíÆ÷µÄ½Ó¿ÚÓëʵÏÖ
javax.servlet.jsp.tagext.TagÊÇʵÏÖ±êÇ©µÄ×î»ù±¾µÄ½Ó¿Ú¡£
javax.servlet.jsp.tagext.TagSupportÊÇʵÏÖTag½Ó¿ÚµÄ¾ßÌåÀà¡£
ͨ³£Çé¿öϼ̳ÐtagSupportÀà¶ø²»Ö±½ÓʵÏÖTag½Ó¿Úͨ³£ÊÇÓÐÒæµÄ¡£³ýÁ˶ÔËùÓбØÐè·½·¨ÌṩÁËȱʡʵÏÖÍâ¡¢»¹±£´æÁËpageContext¶ÔÏó¼°¶ÔǶÌ×±êÇ©µÄÖ§³Ö¡£
Tag½Ó¿Ú°üº¬4¸ö³£Á¿£¬±íʾdoStartTag()ºÍdoEndTag()·½·¨¿ÉÄܵķµ»ØÂë¡£
EVAL_BODY_INCLUDE µ±doStartTag()·µ»Øʱ£¬Ö¸Ã÷servletÓ¦¶Ô±êÇ©Ìå½øÐÐÆÀ¹À¡£
SKIP_BODY µ±doStartTag()·µ»Øʱ£¬Ö¸Ã÷servletÓ¦ºöÊÓ±êÇ©Ìå¡£
EVAL_PAGE µ±doEndTag()·µ»Øʱ£¬Ö¸Ã÷Ò³ÃæÆäÓಿ·ÖÓ¦±»ÆÀ¹À¡£
SKIP_PAGE µ±doEndTag()·µ»Øʱ£¬Ö¸Ã÷Ò³ÃæÆäÓಿ·Ö¾Í±»Ìø¹ý¡£
Tag½Ó¿ÚµÄ·½·¨
public void setPageContext (PageContext ctx) Éú³ÉµÄservletÔÚÇëÇó´¦ÀíÆ÷Ö´ÐÐÆäËüÈÎÎñÇ°Ê×Ïȵ÷Óô˷½·¨£¬ÊµÏÖÀàÓ¦±£´æÉÏÏÂÎĶÔÏóÒÔ±ãËü¿ÉÒÔÔÚ±êÇ©ÉúÃüÆÚÖÐʹÓᣴÓÒ³ÃæÉÏÏÂÎÄÖбêÇ©´¦ÀíÆ÷¿ÉÒÔ·ÃÎÊËùÓÐJSPÒþº¬¶ÔÏó¡£
public void setParent(Tag p) ʹÓÃÒ»¸ö±ê´ð¿ÉÒÔÕÒµ½²Ù×÷Õ»ÖÐËüÉÏÃæµÄ±êÇ©¡£ÔÚsetPageContextºóÁ¢¼´µ÷Óá£
public Tag getParent() ·µ»Ø¸¸±êÇ©¡£
public int doStartTag() throws Jsp ÔÚÉèÖÃÁËÒ³ÃæÉÏÏÂÎÄ¡¢¸¸±êÇ©ºÍ¿ªÊ¼±ê¼ÇÖбàÂëµÄÊôÐÔºóµ÷Ó᣷µ»ØÂë±íÃ÷JSPʵÏÖservletÊÇ·ñ¾ÍÆÀ¹À±êÇ©Ìå¡£
public int doEndTag()throws JspException µ±Óöµ½½á·ñ±ê¼Çʱµ÷Ó᣷µ»ØÂë±íÃ÷JSPÊÇ·ñ¾Í¼ÌŦҳÃæµÄÆäÓಿ·Ý¡£
public void release() È·±£ÔÚÒ³ÃæÍ˳öÇ°±»µ÷Óá£ÊÍ·Å×ÊÔ´²¢ÖØÖñêÇ©´¦ÀíÆ÷״̬¡£
TagSupportÀàµÄ·½·¨
public static Tag finAncestorWithClass(Tag thisTag, Class cls) ΪËùÐèµÄ¸¸±êÇ©´¦ÀíÆ÷²éÕÒÔËÐÐʱ±êÇ©Õ»¡£Ò»¸ö±êÇ©´¦ÀíÆ÷¿ÉÒÔÌṩÆ䷶ΧÄÚ×Ó±êÇ©µ÷Óõķ½·¨¡£
public void setId(String id) ±£´æºÍ¼ìË÷ÔÚidÊôÐÔÖÐÖ¸¶¨µÄÃû×Ö¡£
public void setValue(String name, Object o) ÔÚ±¾µØ¹þÏ£±íÖÐÉèÖÃÖ¸¶¨Ãû×ÖµÄÖµ¡£
public Object getValue (String name) ´Ó±¾µØ¹þÏ£±íÖлñÈ¡Ö¸¶¨Ãû³ÆµÄÖµ¡£
public void removeValue (String name) ´Ó±¾µØ¹þÏ£±íÖÐɾ³ýÖ¸¶¨Ãû³ÆµÄÖµ¡£
public Enumeration getValues() ·µ»Ø¹þÏ£±íÖйؼü×ÖµÄÒ»¸öö¾Ù¡£

3.±êÇ©´¦ÀíÆ÷µÄÉúÃüÆÚ
a.Éú³ÉservletÐèÒª´´½¨±êÇ©´¦ÀíÆ÷ÀàµÄÒ»¸öʵÀý¡£ÊµÏÖ·½Ê½Í¨³£Êǵ÷ÓÃJSPÈÝÆ÷µÄ¹¤³§ÀàµÄÒ»¸ö·½·¨£¬¹¤³§Àà°üº¬Ò»¸ö±êÇ©´¦ÀíÆ÷ʵÀý³ØÒÔʹÆä¿ÉÖØÓò»ÔÙ´¦ÓÚ¼¤»î״̬µÄ¶ÔÏó¡£
b.³õʼ»¯±êÇ©´¦ÀíÆ÷£¬Ê¹servlet»ñÖªÆä´æÔÚÐÔ¡£servletͨ¹ýµ÷ÓñêÇ©´¦ÀíÆ÷µÄÁ½¸ö·½·¨ÊµÏִ˹ý³Ì£ºsetPageContext(PageContext ctx)ºÍsetParent(Tag parent)¡£
c.Èç¹û±êÇ©¾ßÓÐÊôÐÔ£¬ÊôÐÔµÄȡֵͨ¹ý´¦ÀíÆ÷Ìṩsetter·½·¨´«Èëµ½¶ÔÏó¡£ÊôÐÔsetter·½·¨ÊÇÒ»¸ö±êÇ©Ö§³ÖÊôÐÔËùÐèµÄΨһ·½·¨¡£
d.Ò³ÃæµÄÉÏÏÂÎĺ͸¸±êÇ©Òѱ»µ÷Ö㬲¢ÒѾ߱¸ÊôÐÔ¡£´Ëʱµ÷ÓñêÇ©´¦ÀíÆ÷µÄdoStartTag()·½·¨£¬¸Ã·½·¨¿ÉÒÔ¶ÁÈ¡ÕâЩ±äÁ¿²¢Ö´ÐÐʵÏÖ±ê´ð¹¦ÄÜËùÐèµÄ¼ÆËãºÍ²Ù×÷¡£doStartTag()·½·¨±ØÐë·µ»ØÒ»¸öÕûÐÍÊý¡£·µ»ØEVAL_BODY_INCLUDEÔòÕý³£´¦Àí±êÇ©Ì壬·µ»ØSKIP_BODYÔò´Ó³õʼJSPÒ³ÃæÖÐÖ±µ½´Ë±êÇ©½áÊø±ê¼Ç´¦µÄÄÚÈݾù±»ºöÂÔ¡£
e.±êÇ©Ìå±»ÆÀ¹À»òºöÊÓºóµ÷ÓñêÇ©´¦ÀíÆ÷µÄdoEndTag()·½·¨£¬·µ»ØEVAL_PAGEÔòÒ³ÃæµÄÆäÓಿ·Ö±»ÆÀ¹À£¬·µ»ØSKIP_PAGEÔòservlet´úÂëÁ¢¼´´Ó_jspService()Öзµ»Ø¡£

4.Ìå±êÇ©´¦ÀíÆ÷µÄ½Ó¿ÚÓëʵÏÖ
javax.servlet.jsp.tagext.BodyTagÊÇTagµÄ×Ó½Ó¿Ú¡£
javax.servlet.jsp.tagext.BodyTagSupportÊÇʵÏÖBodyTagÀà¡£
BodyContentÊÇjavax.servlet.jsp.JspWriterµÄ×ÓÀ࣬µ«ÓëÆ丸ÀàÓÐËùÇø±ð¡£
BodyContent¶ÔÏóµÄÄÚÈݲ»×Ô¶¯Ð´ÁËÈëservletµÄÊä³öÁ÷£¬¶øÊÇ»ýÀÛÔÚÒ»×Ö·û´®»º´æÖС£µ±±êÇ©ÌåÍê³ÉºóÆä¶ÔÏóÈÔ¿ÉÔÚdoEndTag()·½·¨ÖпÉÒÔÓ¦Óã¬ÓÉgetString()»ògetReader()·½·¨²Ù×÷¡£²¢ÔÚ±ØҪʱÐ޸ļ°Ð´Èë»Ö¸´µÄJspWriterÊä³öÁ÷¡£
BodyContentÀàµÄ·½·¨
public void flush()throws IOException ¸´Ð´JspWrite.flush()·½·¨ÒÔ±ãËü×ÜÊDzúÉúÒç³ö¡£Ë¢ÐÂдÈëÒÑʧЧ£¬ÒòΪËüûÓÐÁ¬½Óµ½½«±»Ð´ÈëµÄʵ¼ÊÊä³öÁ÷ÖС£
public void clearBody() ÖØÖÃBodyContent»º´æΪ¿Õ¡£
public Reader getReader() ·µ»ØReader¶ÁÈ¡ÌåÄÚÈÝ¡£
public String getString() ·µ»Ø°üº¬ÌåÄÚÈݵÄÒ»¸ö×Ö·û´®¡£
public void writeOut(Write w) ½«ÌåÄÚÈÝдÈëÖ¸¶¨Êä³ö¡£
public JspWrite getEnclosing Write() ·µ»ØÕ»ÖÐÏÂÒ»¸ö¸ü¸ßµÄдÈëÕ߶ÔÏ󣨿ÉÄÜÊÇÁíÒ»¸öBodyContent¶ÔÏ󣩡£
BodyTag½Ó¿Ú¶¨ÒåÁËÒ»¸öеÄÕûÐͳ£Á¿
EVAL_BODY_TAG µ±doStartTag()·µ»Øʱ£¬Ê¹µÃеÄBodyContent¶ÔÏó±»´´½¨²¢Óë´Ë±êÇ©´¦ÀíÆ÷Ïà¹ØÁª¡£µ±doAfterBody()·µ»Øʱ£¬Ê¹µÃJSP servletÔÚÐÞ¸ÄÍê´Ë±êÇ©¿ØÖƵÄÈÎÒâ±äÁ¿ºóÔÙ´ÎÆÀ¹ÀÌå¡£
BodyTag½Ó¿ÚµÄ·½·¨
public void setBodyContern (BodyContent out) ÔÚµ±Ç°JspWriterÒѱ»Ð´È룬һ¸öеÄBodyContentÔÚ±»´´½¨ºóÓÉJsp servletµ÷Óã¬Ëü·¢ÉúÔÚdoStartTag()Ö®ºó¡£
public void doInitBody() throws JspException setBodyContent()Ö®ºó£¬Ìå±»ÆÀ¹ÀÇ°µ÷ÓõÄÉúÃüÆÚ·½·¨¡£Èç¹û¶à´ÎÆÀ¹ÀÌ壬´Ë·½·¨Ö»µ÷ÓÃÒ»´Î¡£
public init doAfterBody() throws JspException Ìå±»ÆÀ¹Àºó£¬BodyContentдÈëÕßÈÔ´¦ÓÚ¼¤»î״̬ʱµ÷ÓõÄÉúÃüÆÚ·½·¨¡£´Ë·½·¨±ØÐë·µ»ØEVAL_BODY_TAG»òSKIP_BODY£¬Èô·µ»ØEVAL_BODY_TAGʱÌåÔٴα»ÆÀ¹À¡£
BodyTagSupportÀàµÄ·½·¨
public int doStartTag() throws JspException ¸´Ð´TagSupportÖеÄdoStartTag()·½·¨¡£
public int doEndTag() throws JspException µ÷ÓÃTagSupportÖеÄdoEndTag()·½·¨£¬·µ»Ø½á¹û¡£
public void setBodyContent (BodyContent out) ÔÚÒ»±£»¤³ÉÔ±±äÁ¿bodyContentÖб£´æеÄÌåÄÚÈݶÔÏó£¬×ÓÀà¿ÉÖ±½Ó·ÃÎʴ˶ÔÏó¡£
public void doInitBody() throws JspException ȱʡʲô¶¼²»×ö¡£±»ÐèÒªÖ´Ðгõʼ»¯µÄ×ÓÀàËù¸´Ð´¡£
public int doAfterBody() throws JspException ÿ´ÎÌå±»ÆÀ¹ÀºóÓÉJSP servletµ÷Óã¬ÌåͬÈݶÔÏóÈÔ´¦ÓÚ¼¤»î״̬¡£·µ»ØSKEP_BODY»òEVAL_BODY_TAGÔòÌåÔٴα»ÆÀ¹À
public void release() ÉèÖÃbodyContent¶ÔÏóΪnull£¬È»ºóµ÷ÓÃsuper.release()¡£
public BodyContent getBodyContent() ·µ»ØbodyContent±äÁ¿¡£×ÓÀàÒѾ­¿ÉÒÔ·ÃÎʱ£»¤±äÁ¿£¬µ«´Ë·½·¨ÔÊÐíÎ޹صıêÇ©´¦ÀíÀà¶Ô´ËÌåÄÚÈÝ·¢ËÍÊä³ö¡£
public JspWriter getPreviousOut() ÔÚbodyContent±äÁ¿Éϵ÷ÓÃgetEnclosingWriter()²¢·µ»Ø½á¹ûµÄ¼ò±ã·½·¨¡£

5.Ìå±êÇ©´¦ÀíÆ÷µÄÉúÃüÆÚ

a.Éú³ÉservletÐèÒª´´½¨±êÇ©´¦ÀíÆ÷ÀàµÄÒ»¸öʵÀý¡£ÊµÏÖ·½Ê½Í¨³£Êǵ÷ÓÃJSPÈÝÆ÷µÄ¹¤³§ÀàµÄÒ»¸ö·½·¨£¬¹¤³§Àà°üº¬Ò»¸ö±êÇ©´¦ÀíÆ÷ʵÀý³ØÒÔʹÆä¿ÉÖØÓò»ÔÙ´¦ÓÚ¼¤»î״̬µÄ¶ÔÏó¡£
b.³õʼ»¯±êÇ©´¦ÀíÆ÷£¬Ê¹servlet»ñÖªÆä´æÔÚÐÔ¡£servletͨ¹ýµ÷ÓñêÇ©´¦ÀíÆ÷µÄÁ½¸ö·½·¨ÊµÏִ˹ý³Ì£ºsetPageContext(PageContext ctx)ºÍsetParent(Tag parent)¡£
c.Èç¹û±êÇ©¾ßÓÐÊôÐÔ£¬ÊôÐÔµÄȡֵͨ¹ý´¦ÀíÆ÷Ìṩsetter·½·¨´«Èëµ½¶ÔÏó¡£ÊôÐÔsetter·½·¨ÊÇÒ»¸ö±êÇ©Ö§³ÖÊôÐÔËùÐèµÄΨһ·½·¨¡£
d.Ò³ÃæµÄÉÏÏÂÎĺ͸¸±êÇ©Òѱ»µ÷Ö㬲¢ÒѾ߱¸ÊôÐÔ¡£µ÷ÓñêÇ©´¦ÀíÆ÷µÄdoStartTag()·½·¨£¬¸Ã·½·¨¿ÉÒÔ¶ÁÈ¡ÕâЩ±äÁ¿²¢Ö´ÐÐʵÏÖ±ê´ð¹¦ÄÜËùÐèµÄ¼ÆËãºÍ²Ù×÷¡£
doStartTag()·½·¨±ØÐë·µ»ØÒ»¸öÕûÐÍÊý¡£
·µ»ØEVAL_BODY_TAGÔòÕý³£´¦Àí±êÇ©Ì壨Ìøµ½e£©£»
·µ»ØSKIP_BODYÔò´Ó³õʼJSPÒ³ÃæÖÐÖ±µ½´Ë±êÇ©½áÊø±ê¼Ç´¦µÄÄÚÈݾù±»ºöÂÔ¡££¨Ìøµ½f£©
e.Èç¹û·µ»ØEVAL_BODY_TAGʱ£¬ÔòÕý³£´¦Àí±êÇ©Ìå¡£
e1.ÔÚÕ»Öб£´æµ±Ç°µÄJspWriter¶ÔÏ󣬴´½¨ÐµÄBodyContent¶ÔÏ󣬲¢½«ÆäÖÃΪJSPÒ³ÃæµÄout¶ÔÏó±£´æÔÚÉÏÏÂÎÄ·¶Î§ÄÚÃûΪnameµÄÊôÐÔÖС£²¢µ÷ÓÃËüµÄsetBodyContent()·½·¨¡£
e2.µ÷ÓÃdoInitBody()·½·¨½øÐгõʼ»¯¡£
e3.´¦Àí±êÇ©Ìå¡£½«Êä³öдÈëBodyContent¶ÔÏóÖУ¬´Ë¹ý³ÌÒÀÀµÓÚTLDµÄ±êÇ©ÔªËØ£¬ÓÐÈýÖÖ¿ÉÄÜÈ¡Öµ¡£
e4.µ÷ÓÃdoAfterBody()·½·¨£¬½«ÌåÄÚÌåÄÚÈÝдÈëJspWriter£¬¿ÉÈçÏÂʵÏÖ£º
JspWriter out=bodyContent.getEnclosingWriter();
out.println(bodyContent.getString());//bodyContent.writeOut(out);
bodyContent.clear();
e5.doAfterBody()·½·¨·µ»ØÁ½ÖÖ¿ÉÄÜ£º
·µ»ØEVAL_BODY_TAGʱ£¬ÔÙ¶Ô±êÇ©Ìå½øÐÐÆÀ¹À£¬ÕâÊÇÊý×éºÍö¾Ù±»Ñ­»·´¦ÀíµÄµäÐÍÇé¿ö¡£
·µ»ØSKIP_PAGEʱ£¬¼ÌÐøÒ³ÃæµÄÆäÓಿ·Ý¡£
e6.ÌåÄÚÈÝÍê³É£¬Òò´Ë´´½¨ËüµÄ¹ý³Ì±»·´Ïò£º
µ÷ÓÃpageContent.popBody()·½·¨¼ìË÷Ç°ÃæµÄJspWriter¶ÔÏó¡£
½«Ð´ÈëÕßÉèÖûØoutÒþº¬¶ÔÏó¡£
f.±êÇ©Ìå±»ÆÀ¹À»òºöÊÓºóµ÷ÓÃdoEndTag()·½·¨£¬ÔÊÐí±êÇ©´¦ÀíÆ÷ÏñÊä³öÁ÷·¢»ØÄÚÈÝ¡£
·µ»ØEVAL_PAGEÔòÒ³ÃæµÄÆäÓಿ·Ö±»ÆÀ¹À£»
·µ»ØSKIP_PAGEÔòservlet´úÂëÁ¢¼´´Ó_jspService()Öзµ»Ø¡£
g. ´ËʱÌåµÄÄÚÈÝÔÚÊܱ£»¤µÄbodyContent¶ÔÏóÖÐÈÔÈ»¿ÉÓá£
¿ÉÒÔ½«ËüдÈëservletÊä³öÁ÷ÖУº
JspWriter out=pageContext.getOut();
out.println(bodyContent.getString());
»òÕß
bodyContent.WriteOut(pageContext.getOut());

6.±êÇ©¸½¼ÓÐÅÏ¢Àà


ËÄ¡¢±êÇ©Ö¸Áî

taglibÖ¸ÁîÔªËصÄÄ¿µÄÊÇÖ¸¶¨TLDµÄλÖã¬ÉèÖÃÔÚÒ³ÃæÉÏÓë±êÇ©Çø·Ö¿ªÀ´µÄÒ»¸ö¶Ì±ðÃû¡£
Óï·¨£º
ÊôÐÔ£ºprefix£ºÓÃÓÚ±êʶ±êÇ©¿âµÄΨһ±êʶ¡£uri£º±êÇ©¿â±¾ÉíµÄURI¡£
uri²»±ØÖ¸ÏòÒ»¸öʵ¼ÊÎļþ£¬ËüÊÇJSPÈÝÆ÷¿ÉÒÔÔÚweb.xmlÖвéÕÒʵ¼ÊÎļþλÖõÄΨһ±êʶ·û¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ