2015-09-23 Jstl label (ii)

Source: Internet
Author: User
Tags tld

2015-09-23 Jstl label (ii) One: FMT Label:formatdateTo use the FMT tag in a JSP page:<%@ taglib prefix= "FMT" uri= "http://java.sun.com/jsp/jstl/fmt"%>The 1&GT;FMT tag library is used toLattice-type output ofThe label for the formatted date is:<fmt:formatdate value= "xxx" pattern= "xxx"/>Where value specifies a variable of date type; pattern is used to specify the date template for the output, for example: yyyy-mm-dd hh:mm:ss<%date date = new Date (), Request.setattribute (" Date ", date";%><p> the normal output of the current time format:</p>${date}<p> using the FMT formatted time format output: </p><fmt:formatdate Value= "${date}" pattern= "Yyyy-mm-dd HH:mm:ss"/> Normal output current time format: Wed Sep 21:09:37 CST 2015 using FMT formatted time format output: 2015-09-23 The 21:09:37FMT Tag Library also provides a label for formatted numbers, as follows:<fmt:formatnumber value= "xxx" pattern= "xxx"/>Tag Value: Specifies a variable or constant of a numeric type; pattern: Specifies the type of the number that is formatted, and also rounds for example: 0.00,#.## use 0 and # are different <p> use FMT formatted output number: &LT;/P&GT;&LT;FMT: FormatNumber value= "3.1415926" pattern= "0.00"/> Formatted output numbers using FMT: 3.14 Two: Custom labelstraditional tag implementation tag interfaceSimple label Implementation Simpletag interfaceCustom labels are used to encapsulate Java code A custom label is an action label, and each label is a function of an object custom label the steps to create a custom label are implemented in the corresponding class method: 1: Define a label handling class
2: Create a TLD file, which is itself an XML file
3: Use the <%@ taglib%> directive in the page to specify the location of the TLD file. Traditional tag class Tag interface: Tag is the interface of the classic tag handler, defines the basic protocol between the tag handler and the JSP page implementation class, defines the life cycle and the method that is called at the start and end tags. However, it is more complex than other label processing interfaces. Simple Label class Simpletag interface: So we define the label processing class will not implement the tag interface, but to implement the Simpletag interface, the interface of the parent interface is Jsptag, which and tag

It's a brother relationship .

when creating custom labels in practice, we extends Simpletagsupport

Common methods for Simpletag interfaces:void Dotag (): This method is called every time the label is executed;Jsptag getParent (): Returns the parent label of the current label (rarely used); void SetParent (Jsptag parent): Set parent tag; void Setjspbody (Jspfragment jspbody): Set label body void Setjspcontext (Jspcontext pc): Sets the JSP context object where the subclass of the Jspcontext class is PageContext (get built-in objects and scopes) setParent (jsptag parent), The Setjspbody (jspfragment jspbody), Setjspcontext (Jspcontext pc) method is determined by the

Tomcat Services Device Calls the Dotag () method is called by the Tomcat server after the above three methods have been executed.

Create the TLD file under the Web-inf directory: cannot be built under Lib and class subdirectories, create a subdirectory of the TLD to saveJSP requires more than 2.0 version of the default set to modify the schema path--copy Http://java.sun.com/xml/ns/j2ee to the following file path and "/" EMPTY: The label does not have a label body JSP:JSP2.0 is no longer supporting this type, it means that the tag body content can be: Java script, tag, also can be El expression; Scriptless: Indicates that the tag body content can be an El expression or another label, including dynamic tags and HTML tags; tagdependent: Represents a Label The body content is not executed, but is assigned directly to the label processing class. 1. Label Handling class: Package Com.cissst.simpletag;import java.io.ioexception;import Java.io.writer;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.simpletagsupport;public class MyTag extends Simpletagsupport {@Overridepublic void Dotag () throws Jspexception, IOException {Writer writer = This.getjspcontext (). Getout ();Writer.write ("<br/> ...<br/> before executing the label body");This.getjspbody (). Invoke (writer);Writer.write ("<br/> ...<br/> after tag body Execution");}}2.tld file: <?xml version= "1.0" encoding= "UTF-8"? ><taglib version= "2.0" xmlns= "http://java.sun.com/xml/ns/ Java ee " 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_0.xsd "> <tlib-version>1.0</ tlib-version> <short-name>mytag</short-name> <uri>http://www.mariahcat.com/ Mytag</uri>  <tag> <name>mytag</name> <tag-class> com.cissst.simpletag.mytag</tag-class> <body-content>scriptless</body-content>  </tag></taglib>3.jsp file: <%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><% @ taglib uri= "Http://www.mariahcat.com/MyTag" prefix= "AOAO"%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML Public "-//w3c//dtd HTML 4.01 transitional//en" ><meta http-equiv= "Pragma" content= "No-cache" ><meta http-equiv= "Cache-control" content= "No-cache" ><meta http-equiv= "Expires" content= "0" ><meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" ><meta http-equiv= "description" content= "This is my page" ><!--<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >-private int counts;public void setcounts (int counts) {This.counts = counts;}@Overridepublic void Dotag () throws Jspexception, IOException {for (int i = 0; i < counts; i++) {Getjspbody (). Invoke (null);}}}2. Then add the configuration of the properties to the TLD file <?xml version= "1.0" encoding= "UTF-8"? ><taglib version= "2.1" 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-jsptaglibrary_2_1.xsd" > < Tlib-version>1.0</tlib-version> <short-name>mytag2</short-name> <uri>http ://www.mariahcat.com/mytag2</uri>  <tag> <name>mytag2</name> < tag-class>com.cissst.simpletag.mytag2</tag-class> <body-content>scriptless</ Body-content>  <attribute> <name>counts</name> <required>true </required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>3.jsp file <%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><%@ taglib uri= " Http://www.mariahcat.com/MyTag2 "prefix=" AOAO "%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><meta http-equiv= "Pragma" content= "No-cache" ><meta http-equiv= "Cache-control" content= "No-cache" ><meta http-equiv= "Expires" content= "0" ><meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" ><meta http-equiv= "description" content= "This is my page" ><!--<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >-

The exception is skipping the page exception, and when the server calls the Dotag () method, catches The skippageexception exception, then the following content on the page

No longer executed

2015-09-23 Jstl label (ii)

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.