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文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...
随机推荐
- Native VLAN打上标记
802.1Q和ISL都知道两者的区别在于前者对native vlan的流量不打标记,而后者统一都打标记. 配置成Native VLAN的Trunk端口,收到Native VLAN的帧后,不打标记直接从 ...
- CodeIgniter框架——创建一个简单的Web站点(include MySQL基本操作)
目标 使用 CodeIgniter 创建一个简单的 Web 站点.该站点将有一个主页,显示一些宣传文本和一个表单,该表单将发布到数据库表中. 按照 CodeIgniter 的术语,可将这些需求转换为以 ...
- C#处理MySql多个返回集
关于Mysql返回多个集java和Php的较多,但是C#的完整代码好像没见过,研究了一下做个封装以后用 做一个Mysql的简单分页查询,有两个返回集 Sql语句如下 SELECT COUNT(*) f ...
- Docker容器/镜像查看及删除操作
列出所有正在运行的容器 docker ps 暂停容器 docker stop <name> 删除容器 docker rm <name> 停止所有container docker ...
- 我的Android进阶之旅------>Android中高低API版本兼容使用@TargetApi或者@SuppressLint("NewApi")
Android中高低API版本兼容使用@TargetApi或者@SuppressLint("NewApi") 例如:AndroidManifest.xml中,我们配置了sdk版本的 ...
- C语言中的const,free使用方法具体解释
注意:C语言中的const和C++中的const是有区别的,并且在使用VS编译測试的时候. 假设是C的话.请一定要建立一个后缀为C的文件.不要是CPP的文件. 由于.两个编译器会有区别的. 一.C语言 ...
- 0201-开始使用Spring Cloud实战微服务准备工作
1.Spring Cloud是什么 基于spring boot,之上快速构建分布式系统的工具集 服务注册和发现[eureka.Consul.Zookeeper].服务负载均衡[Ribbon,feign ...
- java中使用axis发布和调用webService及dom4j解析xml字符串
工作中需要调用webService服务,这里记录一下如何在java中发布和调用webService. 需要的jar包: webService服务端: import javax.jws.WebMetho ...
- oracle11g参数的简单查看方法
1.查看processes和sessions参数show parameter processesshow parameter sessions 2.修改processes和sessions值alter ...
- PyQt4调用UI文件
方法1,转换到py调用 指令 pyuic4 test.ui -o testUi.py 方法2.直接调用ui文件 # -*- coding: utf-8 -*- """ - ...