Jsp ---- label programming (JSTL), jsp ---- jstl

Source: Internet
Author: User

Jsp ---- label programming (JSTL), jsp ---- jstl
Introduction to label Programming

JSP development is embedded with a large number of Java code in HTML code, but this will make the JSP page full of Java programs, It is very inconvenient to modify or maintain,

Define a simple tag-empty tag

To implement a tag, you can inherit javax directly. servlet. jsp. tagext. tagSupport class. If the label to be defined does not contain a TAG body, overwrite the doStartTag () method in the TagSupport class.

Perform the first hello program

Operation class for defining tags -- HelloTag. java

Package com. oumyye. tagdemo; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspWriter; import javax. servlet. jsp. tagext. tagSupport; public class HelloTag extends TagSupport {@ Override public int doStartTag () throws JspException {JspWriter out = super. pageContext. getOut (); // get the page output stream object try {out. println ("

Define the label description file --/WEB-INF/hellotab. tld

<? Xml version = "1.0" encoding = "UTF-8"?> <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/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd" version = "2.1"> <tlib-version> 1.0 </tlib-version> <! -- Indicates the version of the tag library --> <short-name> firsttag </short-name> <! -- Is the description name of the tag library in the TLD --> <tag> <name> hello </name> <! -- Indicates the name used by the tag in JSP --> <tag-class> com. oumyye. tagdemo. HelloTag </tag-class> <! -- Indicates the class file pointed to by the tag --> <body-content> empty </body-content> <! -- Indicates that the tag body is empty --> </tag> </taglib>
Element function:
  • <Taglib>: root element of the TLD file. Multiple <tag> elements can be defined;
  • <Tlib-version>: the version of the tag library for development and configuration management;
  • <Short-name>: short name of a tag, mainly used for tag writing;
  • <Tag>: describes each tag in the tag library;
  • <Name>: Tag name;
  • <Tag-class>: Path of the tag processing class;
  • <Body-content>: whether the tag contains the TAG body. If it is empty, the TAG body is empty.

Compile the JSP page and call the tag -- hellotag. jsp

<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ taglib prefix = "mytag" uri = "/WEB-INF/hellotag. tld "%> 

Web. xml configuration

<?xml version="1.0" encoding="UTF-8"?><web-app version="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>
Tag Components
  • Tag processing class: HelloTag. java;
  • Tag description file: hellotag. tld;
  • JSP page: Click <% @ taglib %> to define a tag;
  • (Optional) configure the ing name in the web. xml file.

The following describes the standard tag Library (JSTL) of JSP)

JSTL Introduction: JSTL (JSP Standard Tag Library, JSP Standard Tag Library) is an open-source label component developed by the Apache Jakarta team. It can be downloaded directly from http://atat.apache.org/taglibs. is in the form of jar package exists, directly save this Jar package in the WEB-INF/lib directory, then you can directly open this JAR package through WINRAR tool, and put the META-INF folder in the main label configuration file: c. tld, fmt. tld, fn. tld, SQL. tld, x. tld is saved in the WEB-INF folder

JSTL is a third-party open-source tag library. You need to manually configure JSTL during operations. If you use MyEclipse for development, you can directly use JSTL. Main label categories of JSTL

Core tag library CORE tag library is the most important part in JSTL and is also the most commonly used part in development, in the core tag library, operations such as process control and iterative output are completed. The International tag library is an important part of the program. A program can display relevant information based on the region, for example: the numbers and date display styles in different regions are different. fmt is used in JSTL. tld is used as the definition file for formatting the tag library. In the development of the SQL tag library XML tag library, XML parsing operations are very cumbersome. Fortunately, JSTL provides the XML parsing operations, in this way, users can easily Parse XML files without having to study the use of operations such as SAX or DOM. Function tag Library

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.