您的位置:首页 > 运维架构 > Apache

Apache Shiro学习笔记(八)自定义标签

2016-08-09 17:30 585 查看
鲁春利的工作笔记,好记性不如烂笔头

tld文件

在JSP规范的1.1版中增加了自定义标签库规范,开发自定义标签所涉及到的接口与类的层次结构(其中SimpleTag接口与SimpleTagSupport类是JSP2.0中新引入的)。

一般情况下开发jsp自定义标签需要引用以下两个包
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;





1、实现Tag接口
package com.highabove.crm.demo;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import org.apache.log4j.Logger;

/**
*
* @author lucl
*
*/
public class HelloTag implements Tag {

private static final Logger logger = Logger.getLogger(HelloTag.class);

PageContext pageContext;

@Override
public int doEndTag() throws JspException {
logger.info("the method doEndTag() was invoke...");
return 0;
}

@Override
public int doStartTag() throws JspException {
logger.info("the method doStartTag() was invoke...");
System.out.println("调用doStartTag()方法");
JspWriter out = pageContext.getOut();
try {
//这里输出的时候会抛出IOException异常
out.write("<font color='red'>hello</font>");
} catch (IOException e) {
//捕获IOException异常后继续抛出
throw new RuntimeException(e);
}
return 0;
}

@Override
public Tag getParent() {
logger.info("the method getParent() was invoke...");
return null;
}

@Override
public void release() {
logger.info("the method release() was invoke...");
}

@Override
public void setPageContext(PageContext pageContext) {
logger.info("the method setPageContext(PageContext pageContext) was invoke...");
this.pageContext = pageContext;
}

@Override
public void setParent(Tag tag) {
logger.info("the method setParent(Tag tag) was invoke...");
}

}
自定义标签的执行流程
  JSP引擎遇到自定义标签时,首先创建标签处理器类的实例对象,然后按照JSP规范定义的通信规则依次调用它的方法。
1、public void setPageContext(PageContext pc)
JSP引擎实例化标签处理器后,将调用setPageContext方法将JSP页面的pageContext对象传递给标签处理器,标签处理器以后可以通过这个pageContext对象与JSP页面进行通信。
2、public void setParent(Tag t)
setPageContext方法执行完后,WEB容器接着调用的setParent方法将当前标签的父标签传递给当前标签处理器,如果当前标签没有父标签,则传递给setParent方法的参数值为null。
3、public int doStartTag()
调用了setPageContext方法和setParent方法之后,WEB容器执行到自定义标签的开始标记时,就会调用标签处理器的doStartTag方法。
4、public int doEndTag()
WEB容器执行完自定义标签的标签体后,就会接着去执行自定义标签的结束标记,此时,WEB容器会去调用标签处理器的doEndTag方法。
5、public void release()
通常WEB容器执行完自定义标签后,标签处理器会驻留在内存中,为其它请求服务器,直至停止web应用时,web容器才会调用release方法。

2、继承SimpleTagSupport类
package com.highabove.crm.demo;

import java.io.IOException;

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

/**
*
* @author lucl
*
*/
public class CRMTag extends SimpleTagSupport {

private String user;

@Override
public void doTag() throws JspException, IOException {
super.doTag();

getJspContext().getOut().write("hello, " + this.getUser());
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}
}
JSP自定义标签类还有如下要求:
如果标签类包含属性,每个属性都有对应的getter和setter方法。
重写doTag()方法,这个方法负责生成页面内容。

3、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/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_0.xsd" version="2.0">
<!-- description用来添加对taglib(标签库)的描述 -->
<description>自定义标签</description>
<!-- tlib-version:指定该标签库实现的版本,这是一个作为标识的内部版本号,对程序没有太大的作用。 -->
<tlib-version>1.1.2</tlib-version>
<!-- short-name:该标签库的默认短名,该名称通常也没有太大的用处。 -->
<short-name>hello</short-name>
<!--
uri:这个属性非常重要,它指定该标签库的URI,相当于指定该标签库的唯一标识。
在Jsp页面中引用标签库时,需要通过uri找到标签库, <%@taglib uri="http://www.invicme.org/mytaglib" prefix="hello"%>
-->
<uri>http://www.invicme.org/mytaglib</uri>
<!-- taglib元素下可以包含多个tag元素,每个tag元素定义一个标签 -->
<tag>
<description>这个标签的作用是用来输出客户端的IP地址</description>
<!-- name:该标签库的名称,这个子元素很重要,JSP页面中就是根据该名称来使用此标签的。 -->
<name>header</name>
<!-- 标签对应的处理器类-->
<tag-class>com.highabove.crm.demo.HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
<!-- taglib元素下可以包含多个tag元素,每个tag元素定义一个标签 -->
<tag>
<description>这个标签的作用是用来输出客户端的IP地址</description>
<!-- name:该标签库的名称,这个子元素很重要,JSP页面中就是根据该名称来使用此标签的。 -->
<name>curUser</name>
<!-- 标签对应的处理器类 -->
<tag-class>com.highabove.crm.demo.CRMTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>user</name>
<required>true</required>
<!-- true:表示可以使用运行时表达式;fasle表示只能使用字符串 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
taglib下有如下三个子元素。
tlib-version:指定该标签库实现的版本,这是一个作为标识的内部版本号,对程序没有太大的作用。
short-name:该标签库的默认短名,该名称通常也没有太大的用处。
uri:这个属性非常重要,它指定该标签库的URI,相当于指定该标签库的唯一标识。
taglib元素下可以包含多个tag元素,每个tag元素定义一个标签。
tag元素下允许出现如下常用子元素:
name:该标签库的名称,JSP页面中就是根据该名称来使用此标签的。
tag-class:指定标签的处理类,它指定了标签由哪个标签处理类来处理。
body-content:这个子元素也很重要,它指定标签体内容。该子元素的值可以是如下几个:
tagdependent:指定标签处理类自己负责处理标签体。
empty:指定该标签只能作为空标签使用。
scriptless:指定该标签的标签体可以是静态HTML元素、表达式语言,但不允许出现JSP脚本。
JSP:指定该标签的标签体可以使用JSP脚本。
dynamic-attributes:指定该标签是否支持动态属性。只有当定义动态属性标签时才需要该子元素。

因为JSP 2规范不再推荐使用JSP脚本(可以使用JSTL),所以JSP 2自定义标签的标签体中不能包含JSP脚本。所以,实际上body-content元素的值不可以是JSP。

4、hello_tld.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib uri="http://www.invicme.org/mytaglib" prefix="hello"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello Tld</title>
</head>
<body>
<hello:header />
<br/>
<hello:curUser user="lucl"/>
</body>
</html>


tag文件
Jsp2.0后,实现tag的方式除了taglib(TLD)的方式外,还可以通过定义tag文件来代替taglib类。tag file一般放在/WEB-INF/tags目录或者其子目录,需要在jsp文件中指定uri。

可参考:
http://today.java.net/pub/a/today/2003/11/14/tagfiles.html

http://today.java.net/pub/a/today/2003/11/25/tagfiles.html

Tag File中有如下几个内置对象:
request:与JSP脚本中的request对象对应。
response:与JSP脚本中的response对象对应。
session:与JSP脚本中的session对象对应。
application:与JSP脚本中的application对象对应。
config:与JSP脚本中的config对象对应。
out:与JSP脚本中的out对象对应。


Tag File具有以下5个编译指令:
taglib:与JSP文件中的taglib指令效果相同,用于导入其他标签库。
include:与JSP文件中的include指令效果相同,用于导入其他JSP或静态页面。
tag:类似于JSP文件中的page指令,有pageEncoding、body-content等属性,用于设置页面编码等。
attribute:用于设置自定义标签的属性,类似于自定义标签处理类中的标签属性。
variable:用于设置自定义标签的变量,这些变量将传给JSP页面使用。


标签文件的使用:
在引用标签文件的JSP页面必须使用taglib指令<%@ taglib tagdir="/WEB-INF/tags" prefix="r" %>,其中tagdir属性指定标签文件的地址。


将需嵌入的文件扩展名改为.tag

header.tag
<%@ tag pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ tag import="java.util.Date" import="java.text.DateFormat"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js" ></script>
<img src="${pageContext.request.contextPath}/images/default-photo.png" width="30px" height="35px" />
<%
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
Date now = new Date(System.currentTimeMillis());
%>
<%=dateFormat.format(now) %>
<script type="text/javascript">
$(document).ready(function(){
alert("ok");
});
</script>


hello_tag.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="lucl" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HelloTag</title>
</head>
<body>
<lucl:header/>
</body>
</html>


给tag flie传递数据

simple.tag
<%@ tag pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ attribute name="userName" required="true" type="java.lang.String" description="简单示例:用户名" %>
<table style="border : 1px solid black;">
<caption><%=userName %></caption>
<%
for (int i = 0; i < 3; i++) {
%>
<tr>
<%
for (int j = 0; j < 5; j++) {
%>
<td style="width:30px;"><%=i %>.<%=j %></td>
<%
}
%>
</tr>
<%
}
%>
</table>


hello_tag.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="lucl" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>未授权</title>
</head>
<body>
<h1><lucl:header/>无访问权限</h1>
<lucl:simple userName="张三"></lucl:simple>
</body>
</html>


容器查找Tag文件的方法
WEB-INF/tags目录
WEB-INF/tags的子目录
WEB-INF/lib目录的JAR文件中的META-INF/tags目录
WEB-INF/lib目录的JAR文件中的META-INF/tags的子目录
如果tag文件部署在JAR文件中,则对应该tag文件必须有一个TLD
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tag tld