jsp2.0新特性<%@ page isELIgnored="true|false"%> 如果设定为真,那么JSP中的表达式被当成字符串处理。比如下面这个表达式<p>${2000 % 20}</p>在isELIgnored="true"时输出为${2000 % 20},而isELIgnored="false"时输出为100。Web容器默认isELIgnored="false"。 虽然JSP 2.0可以使JSP中完全使用表达语言而避免scriptlet,在实际编程中,应该根据程序的功能要求和编程人员的自身条件选择合适的方式。使用表达语言的JSP比较方便规整,但是由于需要将标记进行转换,在第一次被调用时会比较慢;有些编程人员由于对Java比较了解,因而更习惯JSP 1.2之前的编程方式,因此,在使用中应因地制宜地选择适用的编程方法。 三)SimpleTag JSP 2.0中加入了新的创建自制标记的API,javax.servlet.jsp.tagext.SimpleTag定义了用来实现简单标记的接口。和 JSP 1.2中的已有接口不同的是,SimpleTag接口不使用doStartTag()和doEndTag()方法,而提供了一个简单的doTag()方法。这个方法在调用该标记时只被使用一次。而需要在一个自制标记中实现的所有逻辑过程、循环和对标记体的评估等都在这个方法中实现。从这个方面来讲, SimpleTag和IterationTag可以达到同等的作用。但SimpleTag的方法和处理周期要简单得多。在SimpleTag中还有用来设置JSP内容的seUspBody()和getJspBody()方法。Web容器会使用setJspBody()方法定义一个代表JSP内容的 JspFragment对象。实现SimpleTag标记的程序可以在doTag方法中根据需要多次调用getJspBody().invoke()方法以处理JSP内容。 例如,程序例2 SimpleTag根据指定的次数(times)进行循环并输出当前序号(sequence)。程序的结构比较简单,所有逻辑都在doTag方法中实现。 例2: packageICW.taglib; importjavax.servlet.jsp.JspException; importjavax.servlet.jsp.tagext.SimpleTagSupport; importjava.util.HashMap; importjava.io.IOException; public class IterationSimpleTag extends SimpleTagSupport{ privateint times; public void setTimes(int_times){ this.times=_times; } public void doTag() throws JspException,IOException{ HashMapparams=new HashMap(); for(inti=0; i<times;i++){ params.put("sequence",String.valueOf(i+1)); getJspBody().invoke(null,params); } } } 这个标记的TLD文件内容如下,它使用了XML schcma定义标记的使用方法。 程序例3如下: <?xml version="1.0" encoding="UTF-8" ?> <taglibxmlns=http://java.sun.com/xml/ns/i2ee xmlns:xsi=http://WWW.w3.org/2001/XMLSchema-instance xsl:schemaLocation=http://java.sun.com/xml/ns/j2ee web-jsptaglihrary_2_0.xsd version="2.0"> <taglib> <tiib-version>1.0</tlib-version> <short-name>Jwad book simple tag</short-name> <uri>/JwadSimpleTag</uri> <description>Simple Tag Handler</description> <tag> <name>iteration</name> <tag-class>ICW.taglib.IterationSimpleTag</tag-class> <body-content>scriptless</body-content> <description>Iteration Tag</description> <variable> <description>Current iterationnumber</description> <name-given>sequence</name—given> </variable> <attribute> <name>times</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> 程序例4中的JSP使用上面例3中定义的IterationSimpleTag,它根据Web请求参数中给定的“times”的值进行一定次数的循环。在每次循环中将输出"sequence"的值。 例4: <%@ taglib prefix="ictag" uri="/WEB-INF/ics-jsp2.tld" %> <HTML><HEAD><TITLE>Simple Tag Sample</TITLE></HEAD> <BODY> <CENTER> <FONT COLOR='#009999' SIZE='4' face='Arial'> <STRONG>Interation Simple Tag</STRONG> </FONT> </CENTER> <HR> <c:setvar="time" value="${param.times}"/> <p><B>Reminder:</B></p><br> <ictag:iteration times="${times}"> This is the ${sequence} Of ${times} times of reminder<br> </ictag:iteration> </body> </html> 四)使用JSP fragment 上一篇:Servlet和JSP迈上新台阶 下一篇:没有了 更多相关文章
|
推荐文章
精彩文章
· jsp计数器代码
|