Jstl Tag Library, custom labels

Source: Internet
Author: User
Tags tld

Jstl
    1. Overview of Jstl

      • Apache stuff that relies on El
      • Use Jstl to import Jstl1.2.jar
      • Four tag libraries:

      Core: Key Library, focus
      FMT: Formatting: date, number
      SQL: Obsolete
      XML: Obsolete

    2. Import Tag Library

      • Jar Package
      • In the JSP page: <% @taglib prefix= "prefix" uri= "path"%>
Core–> C Tag!
out和setremoveurlifchooseforeach
  1. Out and set

    • : Output

    Value: Can be a string constant or an El expression
    Default: The value specified by default is output when the content to be output is null
    EscapeXML: The default value is true, which means escape!
    Example://escapexml is the default and does not write

  2. : Set (Create a domain's properties)
    var: variable name
    Value: variable values, which can be an El expression
    Scope: domain, default to page, optional value: page, request, session, application
    Cases:
  3. Remove

    • : Delete a domain variable

    var: variable name
    Scope: If the scope is not given, the variable that is the name of all domains is deleted, and if the domain is specified, only the variables of that domain are deleted.

  4. Url

    • Value: Specify a path! It automatically adds the project name in front of the path.
      <>, it will output/day13_1/index.jsp
    • Child Tags:, used to add parameters to the URL after, for example:



      The result is:/day13_1/index.jsp?username=%ed%2c%3f%ed%2c%3f
    • var: Specifies the variable name, and once this attribute is added, the URL tag is no longer exported to the page, but the generated URL is saved to the domain.
    • Scope: It is used with Var to save the URL.
  5. If: corresponds to the IF statement in Java

    • Tag body content, when the test value is true, the tag body content is executed!
  6. Choose: it corresponds to If/else if/in Java .../else

    • For example:

      ...
      ...
      ...
      ...
      ...

      Equivalent to
      if (...) {
      } else if (...) {
      } else if (...) {
      } else if (...) {
      } ...
      else {...}
  7. Foreach
    It is used to iterate through arrays, collections!
    It can also be looped in a counting way!

    Counting method:

    for (int i = 1; i <=; i++) {
    ...
    }

    Step can be omitted
    ${i}

    Property:

    • var: loop variable
    • Begin: Sets the loop variable from several beginnings.
    • End: Sets the loop variable to a few ends.
    • Step: Set the step size! Equivalent to i++ in Java, or i+=2. Step defaults to 1

Used to output arrays, collections!


${STR}

Equivalent to

for (String str:strs) {
...
}

Property:
* Items: Specifies who to loop, which can be an array or a collection
* var: assigns each element in the array or collection to the variable specified by var.
Note: There must be no spaces after items= "${list}"//"}"

Loop state

You can use Varstatus to create loop state variables!

The loop state variable has the following properties:
* Count: Number of looping elements
* Index: The subscript of the current loop element
* First: Whether it is a single element
* Last: Is the final element
* Current: Element


v s i n Span style= "Font-family:mathjax_math; Font-style:italic, "id=" mathjax-span-202 "class=" Mi ">d e x {Vs.count} v s f i r s t {Vs.last} ${vs.current}

====================================

FMT Library

It is a format library

Value: Specifies a variable of type date
Pattern: The template used to specify the output! Example: Yyyy-mm-dd HH:mm:ss


Keep 2 digits after the decimal point and it will be rounded! If it is less than 2 bits, fill it with 0!


Keep 2 digits after the decimal point and it will be rounded! If less than 2 bits, do not fill the bit!

Custom labels
    1. Steps

      • Label processing class (tag is also an object, then you need to have class!) )
      • TLD file, which is an XML
      • Use <% @taglib%> in the page to specify the location of the TLD file
    2. Label Processing class
      Simpletag Interface:

      • void Dotag (): This method is called every time the label is executed;
      • Jsptag getParent (): Returns the parent tag (non-life cycle method)//sometimes calls itself
      • void SetParent (Jsptag): Set parent tag
      • void Setjspbody (jspfragment): Set label body
      • void Setjspcontext (Jspcontext): Sets the JSP context object, whose son is PageContext

        where Dotag () is called by Tomcat after the other three methods of the Setxxx () method.

    3. configuring TLD Files

TLD files are generally placed under web-inf so that clients cannot access them!

MYTAG1 Specifies the name of the current label
CN.ITCAST.TAG.MYTAG1 Specifies the label handling class for the current label!
Empty specifies the type of label body, we use an empty label here!

    1. Specify the TLD file location on the page

<%@ taglib prefix= "It" uri= "/web-inf/tlds/itcast-tag.tld"%>
Guide Tag Library, is to specify the location of the TLD file for the page!

Custom Label Mode two:
The Simpletagsupport class implements the Simpletag interface, which has saved all the data that Tomcat has passed, and also provides a GET method for the subclass to invoke. We just need to rewrite the Dotag () method to

Advanced

Tag Body Content
* Empty: no tag body!
* jsp:jsp2.0 is no longer in support of this type! Indicates that the tag body content can be: Java script, can be a tag, can be an El expression
* Scriptless: Can only be an El expression or another tag or string
* Tagdependent: Tag body content will not be executed, but direct assignment tag processing Class!

Do not label the contents below the tag!

Use Skippageexception to end in Dotag () in the Label handling Class!
Tomcat invokes the Dotag () method of the label-handling class, and Tomcat gets skippageexception, which skips the rest of the page!

Label Properties

Steps:
1. Add properties to your label handling class!
Add a property to the label processing class, and the property must be at least one set Method! This set method will be executed by Tomcat before the Dotag () Method! You can use attributes in the Dotag () area.

    1. To configure properties in a TLD file


      Test Specifies the property name
      True to specify whether the property is required
      True to specify whether the property can use the El

Mvc

It is not Java-specific, all the B/s structure of the project are using it!

M–model model (write your own code)
V–view View (JSP)
C–cotroller Controller (Servlet)

Javaweb three-layer frame

Web Layer –> Web-related content (Servlet,jsp,servlet related api:request, response, session, ServletContext)
Business Layer –> business object (service), this layer is part of JavaBean
Data-tier –> operations database (DAO Data Access Object) (all operations on the database cannot jump out of the DAO), another part of this layer of data JavaBean

Jstl Tag Library, custom labels

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.