您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > Servlets-&-Jsp-实验12:用户自定义标签
12实验十二用户自定义标签一.实验目的1.了解什么是自定义标签;2.掌握使用SimpleTag和SimpleTagSuppot开发简单标签;3.了解传统自定义标签的接口和类的使用。二.实验内容1.开发一个简单的SimpleTag标签;【步骤1】创建简单标签类packagemytags;importjava.io.*;importjavax.servlet.jsp.*;importjavax.servlet.jsp.tagext.*;publicclassMathTagextendsSimpleTagSupport{intx=289;publicvoiddoTag()throwsJspException,IOException{getJspContext().getOut().print(Thesquarerootof+x+is+Math.sqrt(x)+.);}}【步骤2】创建taglib.tld文件,将其存放在WEB-INF目录中,内容如下:?xmlversion=1.0encoding=UTF-8?taglibxmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsdversion=2.0uri【步骤3】编写JSP页面math.jsp访问该标签,代码如下:%@taglibprefix=mathuri=:sqrt//body/html该页面的运行结果如下:图11.1math.jsp页面的运行结果2.开发一个空标签,该标签显示一个红色星号【步骤1】创建标签处理类下面是RedStarTag标签的处理类的源代码:packagesampleLib;importjava.io.*;importjavax.servlet.jsp.*;importjavax.servlet.jsp.tagext.*;publicclassRedStarTagextendsSimpleTagSupport{publicvoiddoTag()throwsJspException,IOException{JspWriterout=getJspContext().getOut();out.print(fontcolor='#FF0000'*/font);}}【步骤2】创建标签库描述文件(TLD)在WEB-INF目录中创建一个名为sampleLib.tld标签库描述文件,内容如下:?xmlversion=1.0encoding=UTF-8?taglibxmlns=:xsi=:schemaLocation=://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsdversion=2.0descriptionAsimpletaghandler/descriptiontlib-version1.0/tlib-versionjsp-version2.0/jsp-versionshort-namemytaglib/short-nameuri/WEB-INF/sampleLib.tld/uritagnamestar/nametag-classsampleLib.RedStarTag/tag-classbody-contentempty/body-contentdescriptionPrints*whereveritoccurs/description/tag/taglib【步骤3】在JSP文件中引入标签库和使用标签%@pagecontentType=text/html;charset=gb2312%%@tagliburi==test%htmlbody请输入客户信息,带test:star/的域必须填写。formaction=validateCustomer.jspmethod=posttabletrtdName/tdtdinputtype='text'name='custName'test:star//td/trtrtdEmail/tdtdinputtype='text'name='email'test:star//td/trtrtdPhone/tdtdinputtype='text'name='phone'test:star//td/tr/tableinputtype='submit'/form/body/html3.开发带属性的标签【步骤1】创建标签处理类packagesampleLib;importjava.io.*;importjavax.servlet.jsp.*;importjavax.servlet.jsp.tagext.*;publicclassWelcometTagextendsSimpleTagSupport{privateStringuser;publicvoidsetUser(Stringuser){this.user=user;}publicvoiddoTag()throwsJspException,IOException{JspWriterout=getJspContext().getOut();try{if(user==null)out.print(Hello!Guest!);elseout.print(Hello!+user+!);}catch(Exceptione){thrownewJspException(ErrorinGreet.doTag());}}}【步骤2】在TLD文件中对标签的定义tagnamewelcome/nametag-classsampleLib.WelcomeTag/tag-classbody-contentempty/body-contentdescriptionPrintsHellouser!/descriptionattributenameuser/namerequiredfalse/requiredrtexprvaluetrue/rtexprvalue/attribute/tag【步骤3】标签测试JSP页面welcome.jsp%@taglibprefix=testuri=:welcome//h3h3demo:welcomeuser=john//h3h3demo:welcomeuser='%=request.getParameter(username)%'//h3/body/html使用=Wang访问该页面。4.开发带标签体(JSP代码)的标签完成教材中程序9.9和程序9.10,学习带标签体标签的开发。5.开发迭代标签完成教材中程序9.13和程序9.14,学习迭代标签的开发。三.思考题1.简述简单标签的开发使用过程。2.试述开发自定义标签的步骤。
本文标题:Servlets-&-Jsp-实验12:用户自定义标签
链接地址:https://www.777doc.com/doc-5336462 .html