Download presentation
Presentation is loading. Please wait.
Published byAlexandrina Dickerson Modified over 9 years ago
1
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.
2
本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL
3
Review: Use JavaBeans with JSP JSP Web Container Request Response BrowserServer Database JavaBeans JSP JSP handles the face. JavaBean handles the logic and complexity.
4
Custom JSP Tag 1.Simple Custom Tag: 2.Attribute Custom Tag: "/> Use tld file to config the tag attributes 3. Body Custom Tag: ……
5
Custom JSP Tag (Con’t) 4.Custom Tag can be used in Script:
6
Custom Tag Development 采用特定的 API 编写实现标签处理的程序 位置 /WEB-INF/classes or /WEB-INF/lib 建立一个 tag library descriptor ( TLD) 文件 用来描述 custom tag 位置 /WEB-INF
7
Tag Library Descriptor <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web - jsptaglibrary_1_2.dtd"> 1.0 1.2 ics http://www.icconcept.com/taglibs/sampleTag IcsampleTag Sample TLD in JWAD Book
8
Tag Library Descriptor (Con’t) wordcount jwadbook.taglib.WordCount empty inputname true false warnEmpty true boolean ……
9
A Simple Tag public class TimeTag implements Tag { … public int doStartTag() throws JspException { try { calendar = Calendar.getInstance(); pageContext.getOut().print( DateFormat.getDateInstance().format(calendar.getTime())+" "+ DateFormat.getTimeInstance().format(calendar.getTime()) ); } catch (IOException tage) { throw new JspException("TimeTag Error: "+ tage.getMessage()); } return SKIP_BODY; } public int doEndTag() throws JspException { return SKIP_PAGE; }
10
Simple Tag Result Current Time is:
11
A Attribute Tag public class WordCount extends TagSupport { public void setInputname(String _inputname) { this.inputname = _inputname; } public void setWarnEmpty(boolean _warnempty) { this.warnEmpty = _warnempty; } public int doEndTag() throws JspException { JspWriter out = pageContext.getOut(); inputword = pageContext.getRequest().getParameter(inputname); try{ if ((inputword==null)&&(warnEmpty==true)) { ……
12
A Attribute Tag else { StringTokenizer st = new StringTokenizer(inputword); int wordnumber = st.countTokens(); out.println(wordnumber); } } catch (IOException ioe) { System.out.println ("Error: " + ioe.getMessage()); } return EVAL_PAGE; }
13
Tld for the Example wordcount jwadbook.taglib.WordCount empty inputname true false warnEmpty true boolean
14
JSP with Custom Tag Enter A Brief Description Here Your have entered words in the above text area.
15
BodyTagSupport Sequences
16
BodyTagSupport Example public class ListTag extends BodyTagSupport { private int times; public void setTimes(int _times) { if(_times>0) { times = _times; } else { times = 1; } public int doStartTag() { if (times >= 1) { String timeString = String.valueOf(times); pageContext.getSession().setAttribute("ListTagTime", timeString); return(EVAL_BODY_INCLUDE); } else { return(SKIP_BODY); }
17
BodyTagSupport Example public void doInitBody() { } public int doAfterBody() { if (times-- > 1) { String timeString = String.valueOf(times); pageContext.getSession().setAttribute("Lis tTagTime", timeString); return(EVAL_BODY_AGAIN); } else { return(SKIP_BODY); }
18
RowTag inside ListTag public class RowTag extends TagSupport { public int doStartTag() { try { JspWriter out = pageContext.getOut(); int rowtime = 0; rowtime = Integer.parseInt((String)pageContext.getSession().getAt tribute("ListTagTime")); out.print(" Row Number: "+String.valueOf(rowtime)+" Created in RowTag "); } catch(IOException ioe) { System.err.println("RowTag error"); } return(EVAL_BODY_INCLUDE); }
19
BodyTagSupport Example Result
20
XML and Web Components Server Client DB RDMS EIS XMLDB XML EJB Servlet/Jsp XML DOM/SAX Query EJB HTML XSL XML WML XSL JDBC
22
XML Parsing using SAX JOB101 Prepare the design Requirements 10/1/2002 10/10/2002 …… Parser startElement(“tasklist”, …) startElement(“task”, …) startElement(“taskid”, …) characters(char[],start,length) endElement(“taskid”, …) startElement(“name”, …) endElement(“tasklist”, …) …… XML Events
23
XML Parsing using DOM JOB101 Prepare the design Requirements 10/1/2002 10/10/2002 …… Parser XML DOM Tree
24
A Example of Using DOM public static void main(String argv[]) { if (argv.length != 1) { System.err.println("Usage: java ProcessParser filename"); System.exit(1); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse( new File(argv[0]) ); makeFrame(); } catch (SAXException sxe) { // Error generated during parsing) Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } // main
25
JSP using XML Web Server XML JSP Custom Tag JavaBeans SAX/DOM
26
JSP using XSLT to Convert XML Client HTML XML WML Web Server XML JSP Custom Tag JavaBeans XSLT
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.