Jsp custom tag Case Analysis

Source: Internet
Author: User
Tags return tag

1. demonstrate traditional custom labels (earlier than jsp2.0)

Use custom tags to control whether the page content (TAG body) is output, and use the doStartTag () return value to control

Return this. SKIP_BODY; // ignore the label body.

Return this. EVAL_BODY_INCLUDE; // execute the label body

Control the output of the entire jsp using the doEndTag () Return Value Control

Return this. SKIP_PAGE; // The remaining jsp code after the page tag is skipped

Return this. EVAL_PAGE; // continue executing the remaining jsp code

Custom Tag implementation content (Tag body) loop output using the doAfterBody () defined in Tag sub-interface Iteration and the returned value EVAL_BODY_AGAIN, SKIP_BODY implementation

Overwrite the doStartTag () method and return EVAL_BODY_INCLUDE.

Override doAfterBody ()

Public int doAfterBody () throws JspException {
Times ++;
Int result = this. EVAL_BODY_AGAIN;
If (times> 4 ){
Result = this. SKIP_BODY;
Return result;
Custom tag modification content (TAG body) EVAL_BODY_BUFFERED;
Tag processing class:
Inherit BodyTagSupport
Overwrite doStartTag () and return EVAL_BODY_BUFFERED;
Overwrite doEndTag ()
Public int doEndTag () throws JspException {
BodyContent bc = this. getBodyContent ();
String c = bc. getString ();
C = c. toUpperCase ();
JspWriter out = this. pageContext. getOut ();
Try {
Out. write (c );
} Catch (IOException e ){
Throw new RuntimeException (e );
Return this. EVAL_PAGE;
2. Requirement: implement a custom label function: Judge the date of a YYYY-MM-DD format modified to the following format output
Year: YYYY
Month: MM
Day: DD
Custom tag processing class (Demo5.java)
Package com. csdn. web. tag;
Import java. io. IOException;
Import javax. servlet. jsp. JspException;
Import javax. servlet. jsp. JspWriter;
Import javax. servlet. jsp. tagext. BodyContent;
Import javax. servlet. jsp. tagext. BodyTagSupport;
Import javax. servlet. jsp. tagext. Tag;
Public class Demo5 extends BodyTagSupport {

@ Override
Public int doEndTag () throws JspException {
BodyContent bc = this. getBodyContent ();
String B = bc. getString ();
String [] data = B. split ("-");
JspWriter out = this. pageContext. getOut ();
Try {
Out. println ("year:" + data [0] + "<br> ");
Out. println ("month:" + data [1] + "<br> ");
Out. println ("day:" + data [2] + "<br> ");
} Catch (IOException e ){
E. printStackTrace ();
Return Tag. EVAL_PAGE;
Display time to be converted 6.jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Taglib uri = "/csdn" prefix = "csdn" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Title> convert and output the date format </title>

<Csdn: demo5> 2012-11-17 </csdn: demo5>
Corresponding tag template csdn. 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
Version = "2.0">
<Tlib-version> 1.0 </tlib-version>
<Short-name> csdn </short-name>
<Uri>/csdn </uri>
<Name> demo5 </name>
<Tag-class> com. csdn. web. tag. Demo5 </tag-class>
<Body-content> JSP </body-content>


Related Article

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.