热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Jsp中不要有一行Java代码,需要的Java代码都要封到自定义标签中。自定义标签的作用:a.自定义标签除了可以移除jsp页面java代码外,它也

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文件中对标签处理器进行描述



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">
A tag library exercising SimpleTag handlers.
1.0
kevin //简写
http://www.kevin.com //需要用的uri


viewIP //标签名
com.kevin.web.tag.ViewIpTag //类全名
empty



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

<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%@taglib uri="http://www.kevin.com" prefix="kevin"%> //命名最好和tld文件 一样的名字








你的IP是: //自定义标签使用


 

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

控制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文件中标签体不再为空 JSP


Demo1 测试自定义标签 控制标签体不显示

 

控制整个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" %>你的IP是: Demo1 测试自定义标签 控制标签体不显示Demo2 测试自定义标签 控制整个页面是否显示, 自定义标签加在最上面


控制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;
}
}
}
Demo3 测试自定义标签, 重复执行5次 

修改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;
}
}
Demo4 将小写转为大写 aaaaa 



-------------------------------------------- 下面用简单标签实现上面的功能-----------------------------
控制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


SimpleTagDemo1
com.kevin.web.tag.SimpleTagDemo1
scriptless

控制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文件:











简单标签 控制标签体输出




简单标签 控制标签体重复输出5次




简单标签 控制输出改变 全部转换为大写 aaaaaa





带属性的自定义标签: 标签处理类中编写属性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{
jf.invoke(null);
}
}
}
            SimpleTagDemo2        com.kevin.web.tag.SimpleTagDemo2        scriptless                      count       true     //属性是否是必需属性       true   //属性接收值 是否能用表达式                            date       false       true           
控制标签体显示10次 

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

  description 

  aaaa

  true 

  true

  ObjectType








 


推荐阅读
author-avatar
zxsaqw
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有