JavaWeb -- Jsp 自定义标签的使用

Jsp中不要有一行Java代码, 需要的Java代码都要封到自定义标签中。

自定义标签的作用

a.  自定义标签除了可以移除jsp页面java代码外,它也可以实现以上功能。
b.  控制jsp页面某一部分内容是否执行。例如 权限部分
c.  控制整个jsp页面是否执行。
d.  控制jsp页面内容重复执行。
e.  修改j页面内容输出。
 
简单使用示例

1、编写一个实现tag接口的标签处理器类 (java类)继承TagSupport 复写需要的方法即可

public class ViewIpTag extends TagSupport {

	@Override
	public int doStartTag() throws JspException {
		
		HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
		JspWriter out = this.pageContext.getOut();
		
		String ip = request.getRemoteAddr();
		try {
			out.print(ip);
		} catch (IOException e) {
			 throw new RuntimeException(e);
		}		
		return super.doStartTag();
	}	
}

2、在web-inf/目录下新建tld文件,在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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>A tag library exercising SimpleTag handlers.</description>
    <tlib-version>1.0</tlib-version>
    <short-name>kevin</short-name>     //简写
    <uri>http://www.kevin.com</uri>    //需要用的uri
    
    <tag>
        <name>viewIP</name>                //标签名         
        <tag-class>com.kevin.web.tag.ViewIpTag</tag-class>  //类全名
        <body-content>empty</body-content>
    </tag>
</taglib>
    


3、在jsp页面中导入并使用自定义标签

<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%@taglib uri="http://www.kevin.com" prefix="kevin"%>     //命名最好和tld文件 一样的名字
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>自定义标签的使用</title>
</head>
<body>

你的IP是:<kevin:viewIP />   //自定义标签使用

</body>
</html>

下面为传统标签示例, 实际开发中会用简单标签, 但一些框架设计是用的旧的传统标签

控制jsp页面某一部分内容是否执行: 复写doStartTag方法,“EVAL_BODY_INCLUDE if the tag wants to process body, SKIP_BODY if it does not want to process it.”

public class Demo1_display extends TagSupport {
	
	public int doStartTag() throws JspException {		
		return Tag.SKIP_BODY;
	}	
}

tld文件中标签体不再为空 <body-content>JSP</body-content>

<kevin:Demo1_display>
Demo1 测试自定义标签 控制标签体不显示
</kevin:Demo1_display>

控制整个jsp页面是否执行: 复写doEndTag方法,“If this method returns EVAL_PAGE, the rest of the page continues to be evaluated. If this method returns SKIP_PAGE, the rest of the page is not evaluated”

public class Demo2_display extends TagSupport {

	public int doEndTag() throws JspException {		
		return Tag.SKIP_PAGE;
	}	
}
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.kevin.com" prefix="kevin" %>

<kevin:Demo2_display/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>

你的IP是: <kevin:viewIP />

<kevin:Demo1_display>
Demo1 测试自定义标签 控制标签体不显示
</kevin:Demo1_display>

Demo2 测试自定义标签 控制整个页面是否显示, 自定义标签加在最上面

</body>
</html>


控制jsp页面内容重复执行:  IterationTag 接口 “The doAfterBody() method is invoked after every body evaluation to control whether the body will be reevaluated or not. If doAfterBody() returns IterationTag.EVAL_BODY_AGAIN, then the body will be reevaluated. If doAfterBody() returns Tag.SKIP_BODY, then the body will be skipped and doEndTag() will be evaluated instead. ”

public class Demo3_re extends TagSupport {
	int i=5;	
	public int doStartTag() throws JspException {
		return Tag.EVAL_BODY_INCLUDE ;
	}
		
	public int doAfterBody() throws JspException {
		i--;
		if(i>0)
			return IterationTag.EVAL_BODY_AGAIN;
		else
		{
			i=5;
			return IterationTag.SKIP_BODY;
		}
	}
}
<kevin:Demo3_re>
Demo3 测试自定义标签, 重复执行5次 <br />
</kevin:Demo3_re>

修改j页面内容输出: bodyTag接口

public class Demo4_modify extends BodyTagSupport {

	public int doStartTag() throws JspException {
		return BodyTag.EVAL_BODY_BUFFERED;
	}
	
	public int doEndTag() throws JspException {
		BodyContent bc = this.getBodyContent();
		String content = bc.getString();
		content = content.toUpperCase();
		try {
			this.pageContext.getOut().write(content);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return Tag.EVAL_PAGE;
	}	
}
<kevin:Demo4_modify>
Demo4 将小写转为大写 aaaaa <br />
</kevin:Demo4_modify>



-------------------------------------------- 下面用简单标签实现上面的功能-----------------------------
控制jsp页面某一部分内容是否执行

public class SimpleTagDemo1 extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		
		JspFragment jf = this.getJspBody();
		jf.invoke(this.getJspContext().getOut()); //获取标签体 写入到浏览器, 如果想不执行, doTag()方法体内为空即可。
	}
}

tld文件中配置 不能再写JSP

<tag>
        <name>SimpleTagDemo1</name>
        <tag-class>com.kevin.web.tag.SimpleTagDemo1</tag-class>
        <body-content>scriptless</body-content>
</tag>

控制jsp页面内容重复执行: 获得标签体 重复写入浏览器5次即可

public class SimpleTagDemo2 extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		
		JspFragment jf = this.getJspBody();
		for(int i=0; i<5; i++)
		{
			//jf.invoke(this.getJspContext().getOut());
			jf.invoke(null);  //致null就相当于上面代码,默认写给浏览器
		}
	}	
}

修改j页面内容输出

public class SimpleTagDemo3 extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		
		JspFragment jf = this.getJspBody();  //获得标签体
		StringWriter sw = new StringWriter();
		jf.invoke(sw);
		
		String content = sw.toString();  //获取标签内容
		content = content.toUpperCase();
		this.getJspContext().getOut().write(content);
	}	
}

控制整个jsp页面是否执行: 抛出SkipPageException异常即可,标签后的JSP不再执行

public class SimpleTagDemo4 extends SimpleTagSupport {

	@Override
	public void doTag() throws JspException, IOException {
		
		throw new SkipPageException();
	}	
}

测试jsp文件:

<!--kevin:SimpleTagDemo4 /-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>

<kevin:SimpleTagDemo1>
简单标签 控制标签体输出  <br/>
</kevin:SimpleTagDemo1>

<kevin:SimpleTagDemo2>
简单标签 控制标签体重复输出5次 <br />
</kevin:SimpleTagDemo2>

<kevin:SimpleTagDemo3>
简单标签 控制输出改变   全部转换为大写 aaaaaa<br />
</kevin:SimpleTagDemo3>

</body>
</html>


带属性的自定义标签: 标签处理类中编写属性set方法,tld文件中添加属性

public class SimpleTagDemo2 extends SimpleTagSupport {

	private int count;
	private Date date;
	
	public void setDate(Date date) {
		this.date = date;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	public void doTag() throws JspException, IOException {
		JspFragment jf = this.getJspBody();
		if(date!=null)
			this.getJspContext().getOut().write(date.toLocaleString());
		
		for(int i=0; i<count; i++)
		{
			jf.invoke(null);
		}
	}
}
    <tag>
        <name>SimpleTagDemo2</name>
        <tag-class>com.kevin.web.tag.SimpleTagDemo2</tag-class>
        <body-content>scriptless</body-content>
        
       	<attribute>
       		<name>count</name>
       		<required>true</required>     //属性是否是必需属性
       		<rtexprvalue>true</rtexprvalue>   //属性接收值 是否能用表达式
       	</attribute>
       	
       	<attribute>
       		<name>date</name>
       		<required>false</required>
       		<rtexprvalue>true</rtexprvalue>
       	</attribute>
    </tag>
<kevin:SimpleTagDemo2 count="10" date="<%=new Date()%>">
控制标签体显示10次 <br />
</kevin:SimpleTagDemo2>

<tag>元素的<attribute>子元素用于描述自定义标签的一个属性,自定义标签所具有的每个属性都要对应一个<attribute>元素

<attribute>

  <description>description</description> 

  <name>aaaa</name>

  <required>true</required> 

  <rtexprvalue>true</rtexprvalue>

  <type>ObjectType</type>

</attribute>








 

原文地址:https://www.cnblogs.com/xj626852095/p/3648049.html