JavaWeb -- Jsp 自定义标签的使用
Jsp中不要有一行Java代码, 需要的Java代码都要封到自定义标签中。
自定义标签的作用:
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>
JavaWeb -- Jsp 自定义标签的使用的更多相关文章
- javaweb学习总结(二十三)——jsp自定义标签开发入门
一.自定义标签的作用 自定义标签主要用于移除Jsp页面中的java代码. 二.自定义标签开发和使用 2.1.自定义标签开发步骤 1.编写一个实现Tag接口的Java类(标签处理器类) 1 packag ...
- javaweb(二十三)——jsp自定义标签开发入门
一.自定义标签的作用 自定义标签主要用于移除Jsp页面中的java代码. 二.自定义标签开发和使用 2.1.自定义标签开发步骤 1.编写一个实现Tag接口的Java类(标签处理器类) 1 packag ...
- JSP 自定义标签
0 标签技术的API继承体系 1 作用 jsp自定义标签用于移除页面中的java代码 2 实现 2.1 标签处理类ViewIPTag.java package com.zsm.util; import ...
- JSP自定义标签开发入门
一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发 ...
- 一个简单的jsp自定义标签
学到了一个简单的jsp自定义标签,后面有更多的例子,会更新出来: 例子1: 步骤: 1.编写标签实现类: 继承javax.servlet.jsp.tagext.SimpleTagSupport; 重写 ...
- jsp自定义标签分析
jsp自定义标签的优势体现在于jsp页面上面减少了java代码. jsp自定义标签有三大部分组成,首先是类继承TagSupport,实现doStartTag方法. public int doStart ...
- JSP自定义标签库
总所周知,JSP自定义标签库,主要是为了去掉JSP页面中的JAVA语句 此处以格式化输出时间戳为指定日期格式为例,简单介绍下JSP自定义标签的过程. 编写标签处理类(可继承自javax.servlet ...
- JSP自定义标签配置
JSP自定义标签配置 JSP自定义标签 <taglib> <taglib-uri>/WEB-INF/you.tld</taglib-uri> ...
- jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题
jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...
随机推荐
- FastExcel遇到的问题
第一次使用FastExcel发现在创建excel文件的时候不成功,一直报这个问题: org.apache.poi.EmptyFileException: The supplied file was e ...
- base64文件上传的问题
package com.zhicall.media.util; import java.io.FileInputStream; import java.io.FileOutputStream; imp ...
- Springboot中读取自定义名称properties的
Springboot读取自定义的配置文件时候,使用@value,一定要指定配置文件的位置! 否则报错参数化异常!
- redis add 'vm.overcommit_memory = 1' to /etc/sysctl.conf
w root@well:/etc# vim sysctl.conf #kernel.domainname = example.com # # /etc/sysctl.conf - Configurat ...
- SQLServer中exists和except用法
一.exists 1.1 说明 EXISTS(包括 NOT EXISTS)子句的返回值是一个BOOL值.EXISTS内部有一个子查询语句(SELECT ... FROM...),我将其称为EXIST的 ...
- shell一则-按文件每行长度排序
按文件每行长度排序 awk -F: '{print length($0) " " $0}' /etc/shadow | sort -r -n | awk '{print $2} ...
- Ubuntu部署jmeter
一:ubuntu部署jdk 1:先下载jdk-8u74-linux-x64.tar.gz,上传到服务器,这里上传文件用到了ubuntu 下的 lrzsz. ubuntu下直接执行 sudo apt-g ...
- Linux中的服务管理
RPM包默认安装的服务 查看已安装的服务: chkconfig --list 默认安装位置: /etc/init.d 启动脚本 /etc/sysconfig 初始化环境配置文件 /etc 配置文件位 ...
- 《UNI|X环境高级编程》 源代码配置
代码下载地址:http://www.apuebook.com/ 下的第二版,里面有个readme文件: root@iZ23onhpqvwZ:~/ms/linux/apue/apue.2e# cat R ...
- 流量分析系统---kafka集群部署
1.集群部署的基本流程 Storm上游数据源之Kakfa 下载安装包.解压安装包.修改配置文件.分发安装包.启动集群 2.基础环境准备 安装前的准备工作(zk集群已经部署完毕) 关闭防火墙 chk ...