J2EE|jsp标签

目录

一、标签语言的特点
二、自定义标签的使用步骤
三、JSP标签生命周期图
四、JSP标签实践


一、标签语言的特点 J2EE|jsp标签
文章图片


代码展示:

Insert title here - 锐客网 true false

运行结果:
J2EE|jsp标签
文章图片

二、自定义标签的使用步骤 通过点击c:if标签,可以链接跳到c.tld文件中、而.tld文件就是c标签库的定义配置文件
该配置文件源码:
JSTL 1.1 core library JSTL core 1.1 c http://java.sun.com/jsp/jstl/core Provides core validation features for JSTL tags. org.apache.taglibs.standard.tlv.JstlCoreTLV Catches any Throwable that occurs in its body and optionally exposes it. catch org.apache.taglibs.standard.tag.common.core.CatchTag JSP Name of the exported scoped variable for the exception thrown from a nested action. The type of the scoped variable is the type of the exception thrown. var false false Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by < when> and < otherwise> choose org.apache.taglibs.standard.tag.common.core.ChooseTag JSP Simple conditional tag, which evalutes its body if the supplied condition is true and optionally exposes a Boolean scripting variable representing the evaluation of this condition if org.apache.taglibs.standard.tag.rt.core.IfTag JSP The test condition that determines whether or not the body content should be processed. test true true boolean Name of the exported scoped variable for the resulting value of the test condition. The type of the scoped variable is Boolean. var false false Scope for var. scope false false Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'. import org.apache.taglibs.standard.tag.rt.core.ImportTag org.apache.taglibs.standard.tei.ImportTEI JSP The URL of the resource to import. url true true Name of the exported scoped variable for the resource's content. The type of the scoped variable is String. var false false Scope for var. scope false false Name of the exported scoped variable for the resource's content. The type of the scoped variable is Reader. varReader false false Name of the context when accessing a relative URL resource that belongs to a foreign context. context false true Character encoding of the content at the input resource. charEncoding false true The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality forEach org.apache.taglibs.standard.tag.rt.core.ForEachTag org.apache.taglibs.standard.tei.ForEachTEI JSP Collection of items to iterate over. items false true java.lang.Object If items specified: Iteration begins at the item located at the specified index. First item of the collection has index 0. If items not specified: Iteration begins with index set at the value specified. begin false true int If items specified: Iteration ends at the item located at the specified index (inclusive). If items not specified: Iteration ends when index reaches the value specified. end false true int Iteration will only process every step items of the collection, starting with the first one. step false true int Name of the exported scoped variable for the current item of the iteration. This scoped variable has nested visibility. Its type depends on the object of the underlying collection. var false false Name of the exported scoped variable for the status of the iteration. Object exported is of type javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested visibility. varStatus false false Iterates over tokens, separated by the supplied delimeters forTokens org.apache.taglibs.standard.tag.rt.core.ForTokensTag JSP String of tokens to iterate over. items true true java.lang.String The set of delimiters (the characters that separate the tokens in the string). delims true true java.lang.String Iteration begins at the token located at the specified index. First token has index 0. begin false true int Iteration ends at the token located at the specified index (inclusive). end false true int Iteration will only process every step tokens of the string, starting with the first one. step false true int Name of the exported scoped variable for the current item of the iteration. This scoped variable has nested visibility. var false false Name of the exported scoped variable for the status of the iteration. Object exported is of type javax.servlet.jsp.jstl.core.LoopTag Status. This scoped variable has nested visibility. varStatus false false Like < %= ... > , but for expressions. out org.apache.taglibs.standard.tag.rt.core.OutTag JSP Expression to be evaluated. value true true Default value if the resulting value is null. default false true Determines whether characters < ,> ,& ,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true. escapeXml false true Subtag of < choose> that follows < when> tags and runs only if all of the prior conditions evaluated to 'false' otherwise org.apache.taglibs.standard.tag.common.core.OtherwiseTag JSP Adds a parameter to a containing 'import' tag's URL. param org.apache.taglibs.standard.tag.rt.core.ParamTag JSP Name of the query string parameter. name true true Value of the parameter. value false true Redirects to a new URL. redirect org.apache.taglibs.standard.tag.rt.core.RedirectTag JSP The URL of the resource to redirect to. url false true Name of the context when redirecting to a relative URL resource that belongs to a foreign context. context false true Removes a scoped variable (from a particular scope, if specified). remove org.apache.taglibs.standard.tag.common.core.RemoveTag empty Name of the scoped variable to be removed. var true false Scope for var. scope false false Sets the result of an expression evaluation in a 'scope' set org.apache.taglibs.standard.tag.rt.core.SetTag JSP Name of the exported scoped variable to hold the value specified in the action. The type of the scoped variable is whatever type the value expression evaluates to. var false false Expression to be evaluated. value false true Target object whose property will be set. Must evaluate to a JavaBeans object with setter property property, or to a java.util.Map object. target false true Name of the property to be set in the target object. property false true Scope for var. scope false false Creates a URL with optional query parameters. url org.apache.taglibs.standard.tag.rt.core.UrlTag JSP Name of the exported scoped variable for the processed url. The type of the scoped variable is String. var false false Scope for var. scope false false URL to be processed. value false true Name of the context when specifying a relative URL resource that belongs to a foreign context. context false true Subtag of < choose> that includes its body if its condition evalutes to 'true' when org.apache.taglibs.standard.tag.rt.core.WhenTag JSP The test condition that determines whether or not the body content should be processed. test true true boolean

自己创一个.tld文件
Ctrl+f -->搜索file 出现如图
J2EE|jsp标签
文章图片

将c:if中的配置文件的内容复制一份放进自己的配置文件中 改一下路径
JSTL 1.1 core library JSTL core 1.1 z http://jsp.veryedu.cn Provides core validation features for JSTL tags. org.apache.taglibs.standard.tlv.JstlCoreTLV Catches any Throwable that occurs in its body and optionally exposes it. catch org.apache.taglibs.standard.tag.common.core.CatchTag JSP Name of the exported scoped variable for the exception thrown from a nested action. The type of the scoped variable is the type of the exception thrown. var false false Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by < when> and < otherwise> choose org.apache.taglibs.standard.tag.common.core.ChooseTag JSP Simple conditional tag, which evalutes its body if the supplied condition is true and optionally exposes a Boolean scripting variable representing the evaluation of this condition if org.apache.taglibs.standard.tag.rt.core.IfTag JSP The test condition that determines whether or not the body content should be processed. test true true boolean Name of the exported scoped variable for the resulting value of the test condition. The type of the scoped variable is Boolean. var false false Scope for var. scope false false Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'. import org.apache.taglibs.standard.tag.rt.core.ImportTag org.apache.taglibs.standard.tei.ImportTEI JSP The URL of the resource to import. url true true Name of the exported scoped variable for the resource's content. The type of the scoped variable is String. var false false Scope for var. scope false false Name of the exported scoped variable for the resource's content. The type of the scoped variable is Reader. varReader false false Name of the context when accessing a relative URL resource that belongs to a foreign context. context false true Character encoding of the content at the input resource. charEncoding false true The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality forEach org.apache.taglibs.standard.tag.rt.core.ForEachTag org.apache.taglibs.standard.tei.ForEachTEI JSP Collection of items to iterate over. items false true java.lang.Object If items specified: Iteration begins at the item located at the specified index. First item of the collection has index 0. If items not specified: Iteration begins with index set at the value specified. begin false true int If items specified: Iteration ends at the item located at the specified index (inclusive). If items not specified: Iteration ends when index reaches the value specified. end false true int Iteration will only process every step items of the collection, starting with the first one. step false true int Name of the exported scoped variable for the current item of the iteration. This scoped variable has nested visibility. Its type depends on the object of the underlying collection. var false false Name of the exported scoped variable for the status of the iteration. Object exported is of type javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested visibility. varStatus false false Iterates over tokens, separated by the supplied delimeters forTokens org.apache.taglibs.standard.tag.rt.core.ForTokensTag JSP String of tokens to iterate over. items true true java.lang.String The set of delimiters (the characters that separate the tokens in the string). delims true true java.lang.String Iteration begins at the token located at the specified index. First token has index 0. begin false true int Iteration ends at the token located at the specified index (inclusive). end false true int Iteration will only process every step tokens of the string, starting with the first one. step false true int Name of the exported scoped variable for the current item of the iteration. This scoped variable has nested visibility. var false false Name of the exported scoped variable for the status of the iteration. Object exported is of type javax.servlet.jsp.jstl.core.LoopTag Status. This scoped variable has nested visibility. varStatus false false Like < %= ... > , but for expressions. out org.apache.taglibs.standard.tag.rt.core.OutTag JSP Expression to be evaluated. value true true Default value if the resulting value is null. default false true Determines whether characters < ,> ,& ,'," in the resulting string should be converted to their corresponding character entity codes. Default value is true. escapeXml false true Subtag of < choose> that follows < when> tags and runs only if all of the prior conditions evaluated to 'false' otherwise org.apache.taglibs.standard.tag.common.core.OtherwiseTag JSP Adds a parameter to a containing 'import' tag's URL. param org.apache.taglibs.standard.tag.rt.core.ParamTag JSP Name of the query string parameter. name true true Value of the parameter. value false true Redirects to a new URL. redirect org.apache.taglibs.standard.tag.rt.core.RedirectTag JSP The URL of the resource to redirect to. url false true Name of the context when redirecting to a relative URL resource that belongs to a foreign context. context false true Removes a scoped variable (from a particular scope, if specified). remove org.apache.taglibs.standard.tag.common.core.RemoveTag empty Name of the scoped variable to be removed. var true false Scope for var. scope false false Sets the result of an expression evaluation in a 'scope' set org.apache.taglibs.standard.tag.rt.core.SetTag JSP Name of the exported scoped variable to hold the value specified in the action. The type of the scoped variable is whatever type the value expression evaluates to. var false false Expression to be evaluated. value false true Target object whose property will be set. Must evaluate to a JavaBeans object with setter property property, or to a java.util.Map object. target false true Name of the property to be set in the target object. property false true Scope for var. scope false false Creates a URL with optional query parameters. url org.apache.taglibs.standard.tag.rt.core.UrlTag JSP Name of the exported scoped variable for the processed url. The type of the scoped variable is String. var false false Scope for var. scope false false URL to be processed. value false true Name of the context when specifying a relative URL resource that belongs to a foreign context. context false true Subtag of < choose> that includes its body if its condition evalutes to 'true' when org.apache.taglibs.standard.tag.rt.core.WhenTag JSP The test condition that determines whether or not the body content should be processed. test true true boolean

借用一下c标签的配置文件
Insert title here - 锐客网 true false

运行结果:
J2EE|jsp标签
文章图片

结论:
1、自定义标签是与tld文件相关的
2、标签库中的标签与tld中的tag元素有关,也就是跟tag元素对应的助手类有关
自己的配置文件
JSTL 1.1 core library JSTL core 1.1 z http://jsp.veryedu.cn Provides core validation features for JSTL tags. org.apache.taglibs.standard.tlv.JstlCoreTLV demo1com.cdl.tag.DemoTag1JSP

测试代码:
Insert title here - 锐客网 xx

①当没标签体时的结果
J2EE|jsp标签
文章图片


②、有标签体时的结果
J2EE|jsp标签
文章图片

三、JSP标签生命周期图 J2EE|jsp标签
文章图片

结论:
1、有标签体的情况下 默认会调用助手类的doStartTag,doAfterBody,doEndTag方法
2、如果将doStartTag的返回值改为skip_body,那么doAfterBody就不会调用执行(路线1)
3、如果如果将doStartTag的返回值改为EVAL_BODY_INCLUDE,那么doAfterBody就不会调用执行 (路线2)
4、如果如果将doAfterBody的返回值改为EVAL_BODY_AGAIN,那么就会循环 (路线3)
四、JSP标签实践 写一个if标签
tld文件
JSTL 1.1 core library JSTL core 1.1 z http://jsp.veryedu.cn Provides core validation features for JSTL tags. org.apache.taglibs.standard.tlv.JstlCoreTLV if com.cdl.tag.IfTag JSPtest true true

助手类:
package com.cdl.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * if标签: * 分析: * 如果满足条件,就打印标签体-->dostarttag的返回值eval_body_include * 如果不满足条件,就不输出标签体-->dostarttag的返回值skip_body * 需要获取到是否满足条件的结果值,那么该标签就有一个属性,属性值是Boolean * * @author 陈冬丽 * */ public class IfTag extends BodyTagSupport { private boolean test; public boolean isTest() { return test; } public void setTest(boolean test) { this.test = test; } @Override public int doStartTag() throws JspException { return test ? EVAL_BODY_INCLUDE :SKIP_BODY; } }

运用:
Insert title here - 锐客网 true false

结果图:
J2EE|jsp标签
文章图片

set和out标签
ltd的配置文件
JSTL 1.1 core library JSTL core 1.1 z http://jsp.veryedu.cn Provides core validation features for JSTL tags. org.apache.taglibs.standard.tlv.JstlCoreTLV if com.cdl.tag.IfTag JSPtest true true set com.cdl.tag.SetTag JSPvar true falsevalue true true out com.cdl.tag.OutTag JSPvalue true true

助手类:
settag:
package com.cdl.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * 数据标签:存贮数据的 * 作用域:pagecontext,request,session,application(servletcontext) * * 要存贮数据:以键值对的方式进行存贮,分析得来该标签有两个属性 * * @author 陈冬丽 * */ public class SetTag extends BodyTagSupport{ private String var; private Object value; public String getVar() { return var; } public void setVar(String var) { this.var = var; } public Object getValue() { return value; } public void setValue(Object value) { this.value = https://www.it610.com/article/value; } @Override public int doStartTag() throws JspException { //要存贮数据:以键值对的方式进行存贮,分析得来该标签有两个属性 pageContext.setAttribute(var, value); return super.doStartTag(); } }

outtag:
package com.cdl.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; /** * servlet中后台代码前台输出内容 out.print * 将数据输出到前台 首先拿到输出流 * @author 陈冬丽 * */ public class OutTag extends BodyTagSupport{ private Object value; public Object getValue() { return value; } public void setValue(Object value) { this.value = https://www.it610.com/article/value; } @Override public int doStartTag() throws JspException { JspWriterout = pageContext.getOut(); try { out.print(value); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return super.doStartTag(); } }

测试代码:
Insert title here - 锐客网 true false

运行结果:
J2EE|jsp标签
文章图片

【J2EE|jsp标签】

    推荐阅读