栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

jsp标签

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jsp标签

哈喽啊铁子们今天分享的是jsp标签

老规矩 先看思维导图

目录

一,标签语言特点

二,自定义标签的开发及使用步骤

​编辑

三、标签生命周期

四,c:if标签 c:set与c:out标签


一,标签语言特点

1.标签形式

对   true     进行拆分         

  开始标签
 true                 标签体
 
            结束标签

2.标签分类:

  标签分类:控制标签:例如if foreach  数据标签:out   UI标签:input table   空标签:例如br,hr
  没有标签体也能在网页中输出内容 称为UI标签

代码案例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




Insert title here



true
false



下面是运行效果

 

二,自定义标签的开发及使用步骤

自定义标签的开发分为:

助手类(继承BodyTagSupport) 

标签库描述文件(.tld)(按住Ctrl键点击c:if即可进入,.tld文件必须放在WEB_INF目录下或者其子目录下)

JSP页面通过taglib指令导入标签库

实例:z:if

 助手类:继承BodyTagSupport 实现三个开发场景

jsp页面

运行结果

 

 

三、标签生命周期

 

     ① 返回值
             SKIP_BODY       跳过主体

             EVAL_BODY_INCLUDE        计算标签主体内容并输出

             EVAL_BODY_AGAIN             再计算主体一次 

             SKIP_PAGE                      跳过页面的后续 

        ② 标签开发场景

 

package com.tsq.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;


public class Demotag1 extends BodyTagSupport {
@Override
public int doStartTag() throws JspException {
	System.out.println("=========== doStartTag==========");
	// TODO Auto-generated method stub
	return SKIP_BODY;
}

public class doAfterBody extends BodyTagSupport {
@Override
public int doStartTag() throws JspException {
	System.out.println("=========== doAfterBody==========");
	// TODO Auto-generated method stub
//	int i=1;
//	return EVAL_BODY_AGAIN;
	return SKIP_PAGE;
	
}
}
@Override
	public int doEndTag() throws JspException {
		System.out.println("=========doEndTag===========");
		return super.doEndTag();
	}

}

四,c:if标签 c:set与c:out标签

了解了那么多理论知识,大多都是试验测试,不怎么好看运行结果,

而接下来我们就来利用我们了解的理论知识来实践一下:

4.1自定义几个属于自己的标签吧~
4.2 if标签的的定义
我们需要先建一个if标签类,继承助手类:定义一个if属性test:boolean类型

package com.tsq.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;


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 {
//	* 如果满足条件 就打印标签签体——dosarttag的返回值eval_boby_include
//	 * 如果不满足条件 就不输出标签体
	return test ? EVAL_BODY_INCLUDE: SKIP_BODY;
}
}

 我们写好这个标签类后就要去文件里写好相关的配置,我们把其他的配置全部删除,

 留下一个范本:




    
  JSTL 1.1 core library
  JSTL core
  1.1
  z
  http://jsp.tsq1.cn

  
    
        Provides core validation features for JSTL tags.
    
    
        org.apache.taglibs.standard.tlv.JstlCoreTLV
    
  

  
   
    demo1
    
    com.tsq.tag.Demotag1
    
    JSP
    
    
        var
        
        false
         
        false
    
  


  
   
    if
    
    com.tsq.tag.iftag
    
    JSP
    
    
        test
        
        false
         
        true
    
  
  
  
   
    set
    
    com.tsq.tag.settag
    
    JSP
    
    
        var
        
        true
         
        false
    
  
   
    out
    
    com.tsq.tag.outtag
    
    JSP
    
    
        value
        
        true
         
        true
    
  
  
   
    value
    
    com.tsq.tag.outtag
    
    JSP
    
    
        test
        
        false
         
        true
    
  
  
    
	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
    
  


  我们发现没有问题!

接着我们继续来试一下Set以及Out标签的定义:

Set标签类:这个我们就是拿到一个存储数据的地方:pageContext

package com.tsq.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;


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 = value;
}

@Override
public int doStartTag() throws JspException {
//	PageContext.setAttribute(var,value);
	return super.doStartTag();
}
}

我们继续定义标签类:

Out标签类:这个我们主要就是拿到一个输出流:pageContext.getOut();

package com.tsq.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class outtag extends BodyTagSupport {
	private Object value;

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.print(value);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doStartTag();
	}
}

写好标签类后我们就统一tld文件配置一下:

  
   
    demo1
    
    com.tsq.tag.Demotag1
    
    JSP
    
    
        var
        
        false
         
        false
    
  


  
   
    if
    
    com.tsq.tag.iftag
    
    JSP
    
    
        test
        
        false
         
        true
    
  
  
  
   
    set
    
    com.tsq.tag.settag
    
    JSP
    
    
        var
        
        true
         
        false
    
  
   
    out
    
    com.tsq.tag.outtag
    
    JSP
    
    
        value
        
        true
         
        true
    
  
  
   
    value
    
    com.tsq.tag.outtag
    
    JSP
    
    
        test
        
        false
         
        true
    
  

true
    false
    
    

好了本次分享到此结束了我们下次见

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号