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

J2EE--JSP

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

J2EE--JSP

J2EE--JSP
  • 一、标签语言特点
  • 二、自定义标签的开始及使用步骤
  • 三、标签生命周期
  • 四.set与out标签

一、标签语言特点 1.标签形式

<开始标签 属性=“属性值”>标签体
如:
true
开始标签
true 标签体
结束标签

2.分类

空标签:例如:br、hr
ui标签:例如:input、table
控制标签:例如:if、foreach
数据标签:例如:out标签

二、自定义标签的开始及使用步骤 步骤:

1.首先导入标签库

    
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

2.通过点击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
    
  

3.自己建立一个.tld文件,把内容复制.tld文件
如:xlb.tld 一定要放到WEB-INF目录下面留一个标签




    
  JSTL 1.1 core library
  JSTL core
  1.1
  x
  http://jsp.xlb.like.cn

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

  
  	
    demo1
    
    com.xlb.tag.Demo1Tag1
  	
    JSP
    
    
		
        
    	
        
		
        
    
  

建议一个助手类 必须继承bodytagSupport

package com.xlb.tag;

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


public class Demo1Tag1 extends BodyTagSupport{

	//开始标签
	@Override
	public int doStartTag() throws JspException {
		System.out.println("================doStartTag=============");
		
		//SKIP_BODY跳过
		//return SKIP_BODY;
		//EVAL_BODY_INCLUDE  相当于调用默认方法
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		System.out.println("================JspException========");
		return super.doAfterBody();
		//会进入死循环
		//return EVAL_BODY_AGAIN;
	}
	
	//结束标签
	@Override
	public int doEndTag() throws JspException {
		System.out.println("============doEndTag===========");
		return super.doEndTag();
		//跳过结束标签以后的表示值
		//return SKIP_PAGE;
	}
}

建立一个类来规范if标签规则

package com.xlb.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 {
		//如果满足条件,就打印标签体-》dostarttag的返回值eval_body_include
		//如果不满足条件,就不输出标签体->dostarttag的返回值skip_body
		return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
	}
	
}

Demo2

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.xlb.like.cn" prefix="x" %>    




Insert title here



true
false




运行结果

三、标签生命周期


返回值:

SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并(输出)
EVAL_BODY_AGAIN:在计算主体一次
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面后续部分

标签开发场景:

doStarTag()------------------》doEndTag()
doStartTag()---------------------》doAfterBody-------------》doEndTag()
doStatTag()---------------》doAfterBody(循环)-----------------》doEndTag()

例如:Demo1Tag

package com.xlb.tag;

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


public class Demo1Tag1 extends BodyTagSupport{

	//开始标签
	@Override
	public int doStartTag() throws JspException {
		System.out.println("================doStartTag=============");
		
		//SKIP_BODY跳过
		//return SKIP_BODY;
		//EVAL_BODY_INCLUDE  相当于调用默认方法
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		System.out.println("================JspException========");
		return super.doAfterBody();
		//会进入死循环
		//return EVAL_BODY_AGAIN;
	}
	
	//结束标签
	@Override
	public int doEndTag() throws JspException {
		System.out.println("============doEndTag===========");
		return super.doEndTag();
		//跳过结束标签以后的表示值
		//return SKIP_PAGE;
	}
	
}

四.set与out标签

首先建立两个对应的实体类outTag,setTag

package com.xlb.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();
	}
	
}

package com.xlb.tag;

import javax.servlet.jsp.JspException;
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();
	}
}

再在tld文件中加入两个标签


  	
    set
    
    com.xlb.tag.SetTag
  	
    JSP
    
    
        var
        true 
      	false 
    
    
        value
        true 
      	true 
    
  
  
  
   
  	
    out
    
    com.xlb.tag.OutTag
  	
    JSP
    
    
        value
        true 
      	true 
    
  

最后调用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.xlb.like.cn" prefix="x" %>         




Insert title here



x
true
false




结果

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

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

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