260 likes | 411 Views
蔡 剑 , Ph.D. Java Web应用开发:J2EE和Tomcat. 本讲内容. Web 层技术 ( III) Custom Tags JSP and XML JSTL. Request. JSP. JavaBeans. JSP. JavaBeans. Response. Database. Web Container. Browser. Server. Review: Use JavaBeans with JSP. JSP handles the face. JavaBean handles the logic and complexity.
E N D
蔡 剑, Ph.D. Java Web应用开发:J2EE和Tomcat
本讲内容 • Web层技术 (III) • Custom Tags • JSP and XML • JSTL
Request JSP JavaBeans JSP JavaBeans Response Database Web Container Browser Server Review: Use JavaBeans with JSP JSP handles the face. JavaBean handles the logic and complexity.
Custom JSP Tag • Simple Custom Tag: • <ics:tagname/> • Attribute Custom Tag: • <ics:checkinputdatabasename="<%=userBean.getDatabase()%>"/> • Use tld file to config the tag attributes • 3. Body Custom Tag: • <ics:display format="smalltable"> • <% user.checkstatus(); %> • <table ……><strong> • …… • <table> • <ics:log message ="Table printed"/> • </ics:display>
Custom JSP Tag (Con’t) 4. Custom Tag can be used in Script: <ics:connection id="icdb" type="DataSource" name="icworkdatabase" /> <% icdb.getConnection(); %>
Custom Tag Development • 采用特定的API编写实现标签处理的程序 • 位置 /WEB-INF/classes or /WEB-INF/lib • 建立一个tag library descriptor(TLD)文件用来描述custom tag • 位置 /WEB-INF
Tag Library Descriptor <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>ics</short-name> <uri>http://www.icconcept.com/taglibs/sampleTag</uri> <display-name>IcsampleTag</display-name> <description>Sample TLD in JWAD Book</description>
Tag Library Descriptor (Con’t) <tag> <name>wordcount</name> <tag-class>jwadbook.taglib.WordCount</tag-class> <body-content>empty</body-content> <attribute> <name>inputname</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>warnEmpty</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>boolean</type> </attribute> </tag> …… </taglib>
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; }
Simple Tag Result <HR/> <CENTER><H3> Current Time is: <ics:time/> </H3></CENTER>
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)) { ……
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; } }
Tld for the Example <tag> <name>wordcount</name> <tag-class>jwadbook.taglib.WordCount</tag-class> <body-content>empty</body-content> <attribute> <name>inputname</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>warnEmpty</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <type>boolean</type> </attribute> </tag>
JSP with Custom Tag <TEXTAREA rows="10" cols="58" name="input">Enter A Brief Description Here</TEXTAREA> <BR></BR> Your have entered <ics:wordcount inputname="input" warnEmpty="true" /> words in the above text area. <BR></BR> <INPUT type="submit" name="Submit" value="Count Words"></FORM>
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); } } <TABLE> <ics:list times="4"> <ics:row/> </ics:list> </TABLE>
BodyTagSupport Example public void doInitBody() { } public int doAfterBody() { if (times-- > 1) { String timeString = String.valueOf(times); pageContext.getSession().setAttribute("ListTagTime", timeString); return(EVAL_BODY_AGAIN); } else { return(SKIP_BODY); } } }
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().getAttribute("ListTagTime")); out.print("<TR><TD>Row Number: "+String.valueOf(rowtime)+" Created in RowTag</TD></TR>"); } catch(IOException ioe) { System.err.println("RowTag error"); } return(EVAL_BODY_INCLUDE); } }
Query XMLDB Client HTML XSL XML Client XML XML XSL XML XML Servlet/Jsp Server Servlet/Jsp Server Client WML DOM/SAX EJB EJB JDBC EIS DB RDMS JDBC XML and Web Components
XML Events <tasklist> <task> <taskid> JOB101 </taskid> <name> Prepare the design Requirements </name> <start> 10/1/2002 </start> <end> 10/10/2002 </end> </task> …… </tasklist> Parser startElement(“tasklist”, …) startElement(“task”, …) startElement(“taskid”, …) characters(char[],start,length) endElement(“taskid”, …) startElement(“name”, …) …… endElement(“tasklist”, …) XML Parsing using SAX
XML DOM Tree <tasklist> <task> <taskid> JOB101 </taskid> <name> Prepare the design Requirements </name> <start> 10/1/2002 </start> <end> 10/10/2002 </end> </task> …… </tasklist> Parser XML Parsing using DOM
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
Web Server Custom Tag XML JSP XML SAX/DOM XML JavaBeans JSP using XML
Client HTML Web Server Custom Tag Client XML XML XSLT JSP XML XML JavaBeans Client WML JSP using XSLT to Convert XML