JSP----Label Programming (JSTL)

Source: Internet
Author: User

Introduction to label programming

JSP development is embedded in the HTML code a lot of Java code, but this will make the JSP page is full of Java programs, modification or maintenance is very inconvenient,

Define a simple label----an empty label

To implement a label, you can directly inherit the Javax.servlet.jsp.tagext.TagSupport class, if there is no label body in the label you want to define, then overwrite the doStartTag () method in the TagSupport class directly.

Make the first Hello program

Define the action class for the label--hellotag.java

 PackageCom.oumyye.tagdemo;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.JspWriter;ImportJavax.servlet.jsp.tagext.TagSupport; Public classHellotagextendsTagSupport {@Override Public intdoStartTag ()throwsjspexception {JspWriter out=Super. Pagecontext.getout ();//get the page output stream object        Try{out.println ("//for page Output}Catch(Exception e) {//exceptions are generated here and need to be handledE.printstacktrace (); }        returnTagsupport.skip_body;//No label body    }}

Define a label description file--/web-inf/hellotab.tld

<?XML version= "1.0" encoding= "UTF-8"?><taglibxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"version= "2.1">    <tlib-version>1.0</tlib-version>        <!--represents the version of the tag library -    <Short-name>Firsttag</Short-name>        <!--description name for the tag library in the TLD -    <Tag>        <name>Hello</name>        <!--represents the use name of the label in the JSP -        <Tag-class>Com.oumyye.tagdemo.HelloTag</Tag-class>        <!--indicates the class file to which this label is pointing -        <body-content>Empty</body-content>    <!--indicates that the contents of the tag body are empty -    </Tag></taglib>
Element effect:
    • The root element of the <taglib>:tld file, in which multiple <tag> elements can be defined;
    • <tlib-version>: Represents the version of the tag library for development and configuration management;
    • <short-name>: A short name of a label, mainly used for the writing of labels;
    • <tag>: Describes each label in the tag library;
    • <name>: Name of the label;
    • <tag-class>: The path of the label processing class;
    • <body-content>: Indicates whether the label contains a label body, if empty indicates that the label body is empty.

Write a JSP page and invoke the label--hellotag.jsp

<%@ Page ContentType="Text/html;charset=utf-8"%><%@ taglib Prefix="MyTag"URI="/web-inf/hellotag.tld"%><HTML>    <Head>        <title>Even my Yes</title>    </Head>    <Body>        <H1><Mytag:hello/></H1>            <!--Access Tags -     </Body></HTML>

Web. XML configuration

<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">      <Jsp-config>         <taglib>             <Taglib-uri>Http://www.helloweenvsfei.com/tags</Taglib-uri>             <taglib-location>/web-inf/hellotag.tld</taglib-location>         </taglib>     </Jsp-config></Web-app>
Label components
    • Label processing class: Hellotag.java;
    • Tag description file: Hellotag.tld;
    • JSP page: Define tags by <% @taglib%>;
    • Optionally, configure the mapping name in the Web. xml file.

Here we introduce the standard tag library for JSPs (JSTL)

JSTL Introduction: JSTL (JSP standard tag library,jsp Standards Tag Library) is an open source label component, developed by Apache's Jakarta team, and can be directly from http://tomcat.apache.org/ taglibs/download, download the Jstl. is in the form of a jar package, the jar package is directly in the Web-inf/lib directory, then you can directly open the jar package through the WinRAR tool, And will be inside of the Meta-inf folder of several major tag profiles: C.tld, Fmt.tld, Fn.tld, Sql.tld, x.tld in the Web-inf folder

Jstl is a third-party open source Tag library, JSTL operation requires its own manual configuration, if developed through MyEclipse, it can be used directly. Jstl Main Label Categories

Core Tag Library Core Tag library is the most important part of the JSTL, is also the most commonly used in the development of the section, in the core tag library is the main completion of the process control, iterative output and other operations Internationalized Tag Library internationalization is an important part of the program, a program can be based on the region to display the corresponding information, For example, the numbers in each region and the date display style are different, using FMT.TLD as the definition file for formatting the tag library in Jstl. SQL Tag Library XML tag library in the development of XML parsing operation is very cumbersome, fortunately, in the Jstl dedicated to XML parsing operations, so that users can not laborious to study sax or DOM and other operations, can easily parse the XML file processing. Function Tag Library

JSP----Label Programming (JSTL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.