In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use SimpleTag in JSP2.0 tag library extension, the article is very detailed, has a certain reference value, interested friends must read it!
In the jsp1.2 era there has been a tag library, and powerful, but the tag library programming and call are more complex, resulting in the real use of WEB development or not much. JSP 2.0 introduced a simple tag library extension that addresses these issues. Compared with the tag library in JSP 1.2, the advantages of simple tag library are: For background programmers, the structure is simpler, the implementation interface is less, and the background program can be easily implemented.
JSP 2.0 includes a new API for creating custom tags: javax.servlet.jsp.tagext.SimpleTag, which defines an interface for implementing simple tags. Unlike the existing interfaces in JSP 1.2, the SimpleTag interface does not use doStartTag() and doEndTag() methods, but provides a simple doTag() method. This method is used only once when the tag is called. All logic processes, loops, and evaluation of the tag body that need to be implemented in a custom tag are implemented in this method. In this respect, SimpleTag can achieve the same effect as IterationTa g. But SimpleTag's method and processing cycle is much simpler. There are also setJspBody() and getJspBody() methods in SimpleTag to set JSP content. The Web container uses the setJspBody() method to define a JspFragment object that represents JSP content. Programs that implement SimpleTag tags can call getJspBody().invoke() as many times as needed in the doTag method to process JSP content. For foreground WEB page producers: In the JSP1.2 era,Taglib page calls are actually more complex, SimpleTag +EL expression language greatly simplifies Taglib calls, and truly achieves the purpose of writing JSP pages for people who do not understand JAVA.
Writing and invoking a Taglib page involves three processes:
1. The background programmer writes the program to be invoked
Simple flag extension implementation example:
java program RepeatSimpleTag.java://put it under WEB-INF/classes/jsp2/examples/simpletag package jsp2.examples.simpletag;
package jsp2.examples.simpletag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; import java.util.HashMap; import java.io.IOException; public class RepeatSimpleTag extends SimpleTagSupport { private int num; public void doTag() throws JspException, IOException { for (int i=0; i
< taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2e e web-jsptaglibrary_2_0.xsd" version="2.0"> < description>A tag library exercising SimpleTag handlers.
< /description> < tlib-version>1.0
< /tlib-version> < short-name>SimpleTagLibrary
< /short-name> < uri>/SimpleTagLibrary
< /uri> < tag> < !--这里是开始标志描述--> < name>repeat
< /name> < !--这里设定的标志名称,供jsp文件调用--> < tag-class>jsp2.examples.simpletag.RepeatSimpleTag
< /tag-class> < !--对应的java文件路径--> < body-content>scriptless
< /body-content> < variable> < !--设置要获取的变量返回值--> < description>Current invocation count (1 to num)
< /descri ption> < name-given>count
< /name-given> < /variable> < attribute> < !--设置java类中变量,调用java文件中的setNum()方法--> < name>num
< /name> < required>true
< /required> < rtexprvalue>true
< /rtexprvalue> < /attribute> < /tag> < !--//这里是结束标志描述--> < /taglib>----------------------------
3.JSP page personnel can call directly.
< %@ taglib prefix="repeattag" uri="/WEB-INF/jsp2/repeatTaglib.tld" %> < html> < body> < br> < repeattag:repeat num="5">//assign value to repeat tag in tag library file Get return value {count} of 5
< br>//return result (loop implemented in java program)
< /repeattag:repeat> < /body> < /html>It's so easy to get the results you want.
----------------------------
Using tag files (JSP fragments): Another easy way to use the simple tag extension mechanism is through tag files. Tag file is a kind of resource file, web page author can use it to extract a JSP code, through custom function to achieve code reuse. In other words, tag files allow JSP page authors to create reusable tag libraries using JSP syntax. Tag file extension must be ".tag". On the surface, the label file and the jsp:include directive in jsp 1.2 seem to be the same, but in fact there are still some different, label files generally function relatively short and simple and reuse some code.
----------------------------
A simple tag file SimpleTag.tag is placed under WEB-INF/tags.
< h5>hello,welcome to here ,here is a simple tag Example
< /h5>char.jsp
< %@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> < HTML> < BODY> < H2>Tag File Example
< /H2> < P> < B>The output of my first tag file is
< /B>:
< tags:SimpleTag/> < /BODY> < /HTML>If you want to use this tag file in other JSP pages, you can also call this tag library file to achieve the purpose of simple code reuse.
----------------------------
The basic feature of a tag file (JSP fragment) is that it allows the container processing JSP to defer evaluation of JSP tag attributes. We know that general JSP is to first evaluate the attributes of JSP tags, and then use these attributes when processing JSP tags, and JSP fragments provide dynamic attributes. That is, these attributes can be changed as JSP processes its tag body. Tag files are self-made tags implemented in text file format (JSP syntax), which is also a major new feature of JSP 2.0.
----------------------------
Dynamic multiplexing of label files Label files can be used as templates. directive attribute is similar to in TLD
< attribute>Element that allows you to declare custom action attributes.
< %@ attribute name="color" %> < %@ attribute name="bgcolor" %> < %@ attribute name="title" %> < TABLE border="0" bgcolor="${color}"> < TR> < TD> < B>${title}
< /B> < /TD> < /TR> < TR> < TD bgcolor="${bgcolor}"> < jsp:doBody/> < /TD> < /TR> < /TABLE>Here is the jsp file that calls this Tag file
< %@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> < HTML> < BODY> < TABLE border="0"> < TR valign="top"> < TD> < tags:display color="#ff0000" bgcolor="#ffc0c0" title="Travel">Last French Concorde Arrives in NY Another Travel Headline Yet Another Travel Headline
< /tags:display> < /TD> < TD> < tags:display color="#00fc00" bgcolor="#c0ffc0" title="Technology">Jav a for in-flight entertainment Another Technology Headline Another Technology Headline
< /tags:display> < /TD> < TD> < tags:display color="#ffcc11" bgcolor="#ffffcc" title="Sports">America n Football NBA Soccer
< /tags:display> < /TD> < /TR> < /TABLE> < /BODY> < /HTML>Each time you set the relevant attributes in the Tag file, the Tag file displays the corresponding results according to the set attributes. It can be said that the use of tag libraries in JSP 2.0 is a great convenience.
The above is "JSP2.0 tag library extension SimpleTag how to use" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.