Experience 11--jsp Custom Label Case Analysis __js

Source: Internet
Author: User
Tags return tag

1. Demo traditional Custom Label (jsp2.0 previous)

use a custom label to control whether the page content (label body) is output, using the return value of doStartTag () to control

return this. Skip_body; Ignore label body

return this. Eval_body_include; Execution label Body

control the output of the entire JSP using the return value of Doendtag ()

return this.  Skip_page; The remaining JSP code after skipping the page label

return this. Eval_page; Continue to execute the remaining JSP code

The Custom Label implementation content (tag body) Cyclic output is implemented using the Doafterbody () and return value Eval_body_again,skip_body defined in the tag sub-interface iteration

First overwrite the doStartTag () method and return Eval_body_include

Overlay Doafterbody ()

public int doafterbody () throws jspexception {times++; int result = this.
	Eval_body_again; if (times>4) {result = this.
	Skip_body;
return result;
Custom Label modification content (tag body) eval_body_buffered;
Label processing class: Inherit Bodytagsupport overlay 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, requirements: To implement a custom label function: To determine a YYYY-MM-DD format date modified to the following format output year: YYYY month: MM Day: DD Analysis: Custom Label 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; The display time to convert 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" >  


 

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.