National Academy for Software Development - PowerPoint PPT Presentation

About This Presentation
Title:

National Academy for Software Development

Description:

(Custom Tag Libraries) Dreamix Ltd. – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 44
Provided by: devb150
Learn more at: https://www.devbg.org
Category:

less

Transcript and Presenter's Notes

Title: National Academy for Software Development


1
????????? ?? ??? ??????????
(Custom Tag Libraries)
Dreamix Ltd.
????? ?????
2
??????????
  • ????? ?? ??????????????? ??????
  • ??????? ??????????????
  • ??? ??????? ?? ????????? ?? ????????????? ??????
  • Java Handler
  • JSP ??? ????
  • ????????, ???? ?? ???, ??????????????? ??????.

3
????? ?? Custom Tags
  • ???????? ?? ??????????? JSP ???????
  • ?????????? ?????????????
  • ????? ? ??????? ?? ???? ??????, ???? ? ? ????????
    ???????
  • ??????? ???????? ?? ?????????????
  • JSP-?? ?????? ??-?????? ? ??????????
  • ??????? ???????? ?? ???????????? ?? Java Web
    Frameworks

4
??????? ??????????????
  • ????? ?? ?? ??????????? ???? ????????
  • ????? ?? ?????? ?????????? ??? ???????????
    ???????? (JSP)
  • ????? ?? ????????? ?????? ?????? ???????? ?? JSP
    ?????????
  • ????? ?? ??????????? ???? ? ????
  • ????? ?? ?? ???????
  • ????? ?? ???? ???? (body)

5
??? ??????? Java ????
  • Java ??????
  • ???-? ???????????? java ????
  • ?????? ?? JSPWriter ?????
  • ???????? ??, ?????? ???-? ??????? ????? ?????????
    ????????
  • ?????????? ?? ???????????? ?? ????????

6
??? ??????? JSP ????
  • JSP ??????
  • ???? JSP 2.0
  • ??????? ?? ? ???? ? ?????????? .tag
  • ???????? JSP ?????????
  • ???????? ??, ?????? ??? ?????? ???????? ????????
    ?? ??????? ??????
  • ?????????? ?? ???????????? ?? JSP

7
Custom Tag Libraries
Java ????
8
??????????
  • Java ????
  • ??? ?? ???????????? ???????? ?? ???? ? ????? ??
    ????????
  • ?????? ?? ???????????? javax.servlet.jsp.tagext.S
    impleTag
  • ?????????? ?? ????????? javax.servlet.jsp.tagext.
    SimpleTagSupport
  • ??? ??????????
  • XML ????, ? ?????????? .tld
  • ??????? ?? ????? ?? ????, ?????????? ?? ? java
    ??????

9
??? ???? - ??????
package eu.dreamix.test.tags import
javax.servlet.jsp. import javax.servlet.jsp.tage
xt. import java.io. public class
SimpleGreetingTag
extends SimpleTagSupport public
void doTag() throws JspException,
IOException
JspWriter out getJspContext().getOut()
String greeting "Hello"
out.print(greeting)
10
??? ??????????
  • ????? ?? XML-a e taglib
  • ???????? ?? ??? ???????????? ltdescriptiongt,
    ltdisplay-namegt, lttlib-versiongt, lturigt,
    ltshort-namegt, lttaggt
  • ????? ??????? ??? ?? ?????? ? tag ???????, ?????
    ???????
  • description - ?????? ????????
  • name ????? ?? ???-?
  • tag-class ??????? ??? ?? ??? ?????
  • body-content ?????? ???? ???? ??? ??????????
    ????? ????????? ? ?????????? ???

11
??? ?????????? - ??????
lt?xml version"1.0" encoding"UTF-8" ?gt lttaglib
xmlns"http//java.sun.com/xml/ns/j2ee"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" xsischemaLocation"http//java.sun.com/xml/n
s/j2ee http//java.sun.com/xml/ns/j2ee/web-jsptag
library_2_0.xsd" version"2.0"gt
lttlib-versiongt1.0lt/tlib-versiongt
ltshort-namegtTestTagslt/short-namegt
lturigthttp//dreamix.eu/web/tags/jwclt/urigt
lttaggt ltdescriptiongt Displays
a simple greeting message lt/descriptiongt
ltnamegtgreetinglt/namegt lttag-classgt
eu.dreamix.test.tags.SimpleGreetingTag
lt/tag-classgt ltbody-contentgtemptylt/
body-contentgt lt/taggt lt/taglibgt
12
????????? ? JSP-??
  • ?????? ?? ??? ???????????? (??? ??????)
  • ?????? ?? ???? ?? .tld ???????????
  • ?????? ?? URI-?? ?? ??? ???????????? (???????? ??
    lturigt ? taglib ????)

lt_at_ taglib uri"/WEB-INF/tlds/my-taglib.tld"
prefix"mytaglib" gt
lt_at_ taglib uri"http//dreamix.eu/web/tags/jwc"
prefix"mytaglib" gt
13
??? ?????????? Java ????
  • ????????????

SimpleGreetingTag
14
????????
  • ?? ????? ??????? xxx ?? ??? ?, ???????? ????? ?
    java ????? setXxx(? value)
  • ??????
  • ?? ?? ????????? ???-? ? ???????
  • ? java ????? ?????? ?? ?????

ltprefixtagName attribute1"Test" /gt
public void setAttribute1(String value1)
doSomethingWith(value1)
15
??????? Java ??????????
... ...
public class SimpleGreetingTag
extends SimpleTagSupport private String
person public void setPerson(String value)
this.person person public void
doTag() throws JspException,
IOException JspWriter out
getJspContext().getOut() String greeting
"Hello, " person out.print(greeting)
16
???????? ??? ??????????
  • ?? ????? ???????,tag ???????? ?????? ?? ???????
    ??????? attribute
  • attribute ??????? 3 ????????
  • name, ??? ?? ????????
  • required, ???? ???????? ? ???????????? ???
    ???????? ?? ????
  • rtexprvalue(truefalse), ??????? ???? ?????????
    ?? ???????? ???? ?? ? JSP ????? (lt?????gt) ???
    ? ???????? ??????.

17
???????? TLD ??????
... lttaggt ltdescriptiongt Displays a
simple greeting message lt/descriptiongt
ltnamegtgreetinglt/namegt lttag-classgt
eu.dreamix.test.tags.SimpleGreetingTag
lt/tag-classgt ltbody-contentgtemptylt/body-content
gt ltattributegt ltnamegtpersonlt/namegt
ltrequiredgttruelt/requiredgt lt/attributegt lt/taggt

18
??? ?????????? Java ????
  • ????????????

????????
19
Custom Tag Libraries
JSP ????
20
????????????? ??????
  • ??? Java ??????? ?? ??????????
  • Java ????, ????? ???????????? doTag()
  • ??? ?????????? (TLD)
  • JSP, ? ????? ?? ??????? ??????????? ?? ???
    ????????????
  • ??? JSP ??????? ?? ??????????
  • JSP ??? (??? ????)
  • JSP, ? ????? ?? ??????? ????????????, ?????????
    ??? ?????

21
??? ???? - ??????
/WEB-INF/tags/greeting.tag
lt!-- Display greeging message --gt lth2gtHello! I'm
the simpliest tag in the world!!lt/h2gt
  • ????????????, ? ????? ?? ?????? ???? ?? ?
    ??????????!
  • ???? ?????? ?? ?? ?????? ?
    /WEB-INF/tags ??? ????????????? ?? ???

22
????????? ?? ???-???? ? JSP
Greeting.jsp
... lt_at_ taglib tagdir"/WEB-INF/tags"
prefix"mytags" gt ... ltbodygt ...
ltmytagsgreeting /gt ... lt/bodygt
  • ?????? uri ?? ???????? tagdir
  • ?????? ?? ????????????, ????? ??????? ????????

23
????????
  • ?? ????? ??????? ?? ??????
  • ? ???????? ?? ??? ?????

lt_at_ attribute name"XXX" required"(truefalse)"
gt
/WEB-INF/tags/greeting.tag
lt!-- Display greeging message --gt lt!--
Attributes--gt lt_at_ attribute name"location"
required"true" gt lth2gt Hello! I'm the simpliest
tag in the location!! lt/h2gt
24
??? ?????????? Java ????
  • ????????????

25
?????? ? ????
  • ?????? ??????????? ???? ?????? ??? ????
  • ?????? ? ????

ltprefixtag attribute1"value1"/gt
ltprefixtagName attr1"value1"gt Scriptless JSP
content lt/prefixtagNamegt
26
??? ???????????????????? ?? ???? ?? ???
  • ltbody-contentgt

lttaggt ltnamegt...lt/namegt lttag-classgt...lt/tag
-classgt ltbody-contentgtscriptlesslt/body-content
gt lt/taggt
  • ????????? ?????????
  • empty ???-? ???? ???? (???? ? ?? ??? ?? ????????
  • scriptless ????-? ???????? ????, ????? ???? JSP
    ????????? ????????
  • tagdependent ?????? ?? ????????? ?? ????

27
??????????? ?? ?????? -Java ???? ??????
  • ??????? ?? getJspBody().invoke()
  • invoke() ?????? ???? ???????? java.io.Writer
    ?????, ? ????? ?? ???????? ?????? ?? ????.
  • invoke(null) ????????? ?????? ???????? ? JSP
    ??????????.

public void doTag() throws ... JspWriter
out getJspContext().getOut()
out.print("...") getJspBody().invoke(null)
out.print("...")
28
?????? ? ???? JSP ????
  • ???????? ?? ltjspdoBody /gt ?? ?? ?? ????????
    ??????.

/WEB-INF/tags/note.tag
lt_at_ attribute name"author" required"true"
gt lttable border"1"gt lttrgt ltthgtAuthorlt/thgt ltthgt
Notelt/thgt lt/trgt lttrgt lttdgtauthorlt/tdgt
lttdgtltjspdoBody /gtlt/tdgt lt/trgt lt/tablegt
29
?????? ? ???? JSP ???? (2)
notes.jsp
ltmytagsnote author"Mojo Jojo"gt I will rule
the world ! lt/mytagsnotegt
Browser
30
??? ?????????? Java ????
  • ????????????

31
??-?????? ???????????
  • ?? ???????? ???????? ???
  • ?????? ?? ????????? ?????? ?? ?????? ?? ??????
  • ?????? ?????? ?? ??????? ???????
  • ????????
  • ???????? ?????? ?? ? ?? ??? ???????? ??? ?????
  • ? ?????? ?????? ?? ????? ?????? ?? ????? ???????

32
???????? ?? ?????? ???
  • ?????????? ?? ??????? ?? ???, ???????? ?? String
    ??? ????????
  • setXXX ?????? ? ?????????? ??? (??? Object)
  • rtexprvalue true
  • ?????????? ?? ???? ???????? ???? EL ?????

lttagsloop items"someList" ... /gt
33
???????? ?? ???? ? ??????
  • ?? ?? ???????? ????? ????? ???????? ? ?????? ??
    ????
  • ?????????? ??????
  • key ????? (????) ?? ??????
  • object ?????? ?????
  • ?? ?? ???????? ?????? ? ??????
  • ?????????? EL ????? key

getJspContext().setAttribute(key, object)
34
Loop Tag Handler
public class LoopTagHandler extends
SimpleTagSupport private Collection items
private String var public void
setItems(Collection items) this.items
items public void setVar(String var)
this.var var
???????? ??????????
35
Loop Tag Handler
... ???????????
public void doTag() throws JspException,
IOException
Iterator iter items.iterator() while
(iter.hasNext()) Object current
iter.next() //???????? ?? ???????
????? ? ?????? ?? ???? getJspContext().setAt
tribute(var, current) //???????????
?? ?????? ? JSP ??????????
getJspBody().invoke(null)
36
??? ?????????? Java ????
  • ????????????

37
??????? ??????
  • ?????? ??????????? ?? ???? ??? ?????? ?? ???????
    ?????????? (?????????)
  • getParent() - ????? ??????????? ???
  • findAncestorWithClass(this, OuterTag.class)
    ????? ????????? ??? ?? ??????? ??? (???
    ??????????)
  • ?????? ?? ???????? If, Then ? Else ??????

38
IfTag
public class IfTag extends SimpleTagSupport
private boolean test public void
setTest(boolean test) this.test test
public boolean getTest() return(test)
public void doTag() throws JspException,
IOException
getJspBody().invoke(null)
39
ThenTag
public class ThenTag extends SimpleTagSupport
public void doTag() throws JspException,
IOException //
Get parent tag (if tag) IfTag ifTag
(IfTag)getParent() if (ifTag ! null)
// Decide whether to output body of then
if (ifTag.getTest()) getJspBody().invoke
(null) else String msg
"Error 'then' must be inside 'if'." throw new
JspTagException(msg)
40
ElseTag
public class ElseTag extends SimpleTagSupport
public void doTag() throws JspException,
IOException
// Get parent tag (if tag) IfTag ifTag
(IfTag)getParent() if (ifTag ! null)
// Decide whether to output body of else if
(!ifTag.getTest()) getJspBody().invoke(null)
else String msg
"Error 'else' must be inside 'if'." throw new
JspTagException(msg)
41
If Then Else - ??????
  • ???????? ?????????

ltmytagsif test"lt Math.random() lt 0.5
gt"gt ltmytagsthengtHeadslt/mytagsthengt ltmytagsel
segtTailslt/mytagselsegt lt/mytagsifgt
42
??????????
  • ??????????????? ??????
  • ????????? java ???? ?? JSP ??????????
  • ?? ?????? JSP ?? ???????????
  • ????? ?????????? ????? ????????? ?? ??
    ???????????
  • ????????? ?? ?????? ???? ?? ???? ?????
  • ??????? ???????? ?? ?????????????
  • Online ??? ??????????
  • http//jakarta.apache.org/taglibs/

43
????????? ?? ??? ??????????
  • ????????
Write a Comment
User Comments (0)
About PowerShow.com