/*
案例一:开发一个if标签
日期:20130930
作者:烟大阳仔
*/
1.编写一个实现tag接口的JAVA类
public class IFTagLib extends SimpleTagSupport
{
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
if(test)
{
this.getJspBody().invoke(null);
}
}
}
2.在tld文件中对标签处理器进行描述(tld文件的位置WEB-INF里面)
<?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>IFTagLib</short-name>
<uri>/IFTagLib</uri>
<tag>
<name>if</name>
<tag-class>cn.com.web.ifTag.IFTagLib</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3.在jsp页面中使用标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/IFTagLib" prefix="IFTagLib" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'IfDemo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
session.setAttribute("user", "as");
%>
<IFTagLib:if test="${user==null}">
Hello world!
</IFTagLib:if>
<IFTagLib:if test="${user!=null}">
一点不好啊!
</IFTagLib:if>
</body>
</html>
----------------------------------------------------------------------------------------------------------
/*
案例二:开发一个嵌套的标签
<IFElse:choose>
<IFElse:when test="${user==null}">
大家好!
</IFElse:when>
<IFElse:otherwise>
我是阳仔!
</IFElse:otherwise>
</IFElse:choose>
日期:20130930
作者:烟大阳仔
*/
1.编写三个实现tag接口的JAVA类
public class IfElseTagLib extends SimpleTagSupport {
private boolean isDo;
public boolean isDo() {
return isDo;
}
public void setDo(boolean isDo) {
this.isDo = isDo;
}
@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}
}
public class WhenTag extends SimpleTagSupport
{
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
IfElseTagLib parent=(IfElseTagLib) this.getParent();
if(test&& !parent.isDo())
{
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}
public class otherTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
IfElseTagLib parent =(IfElseTagLib) this.getParent();
if(!parent.isDo())
{
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}
2.在tld文件中对标签处理器进行描述(tld文件的位置WEB-INF里面)
<?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>IFElse</short-name>
<uri>/IFElse</uri>
<tag>
<name>choose</name>
<tag-class>cn.com.web.ifTag.IfElseTagLib</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>cn.com.web.ifTag.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>cn.com.web.ifTag.otherTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
3.在jsp页面中使用标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/IFElse" prefix="IFElse" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'IfElse.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
session.setAttribute("user", "as");
%>
<IFElse:choose>
<IFElse:when test="${user==null}">
大家好!
</IFElse:when>
<IFElse:otherwise>
我是阳仔!
</IFElse:otherwise>
</IFElse:choose>
</body>
</html>
----------------------------------------------------------------------------------------------------------
/*
案例三:开发一个防盗链标签
日期:20130930
作者:烟大阳仔
*/
1.编写一个实现tag接口的JAVA类
public class RefererTagLib extends SimpleTagSupport {
private String site;
private String page;
public void setSite(String site) {
this.site = site;
}
public void setPage(String page) {
this.page = page;
}
@Override
public void doTag() throws JspException, IOException {
PageContext pageContext=(PageContext) this.getJspContext();
HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
HttpServletResponse response=(HttpServletResponse) pageContext.getResponse();
//得到未访问者referer
String referer=request.getHeader("referer");
if(referer==null||!referer.startsWith(site))
{
if(page.startsWith(request.getContextPath()))
{
response.sendRedirect(page);
return ;
}
else if(page.startsWith("/"))
{
response.sendRedirect(request.getContextPath()+page);
}
else
{
response.sendRedirect(request.getContextPath()+"/"+page);
}
throw new SkipPageException();
//response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}
}
2.在tld文件中对标签处理器进行描述(tld文件的位置WEB-INF里面)
<?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>RefererTagLib</short-name>
<uri>/RefererTagLib</uri>
<tag>
<name>referer</name>
<tag-class>cn.com.web.Referer.RefererTagLib</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3.在jsp页面中使用标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/RefererTagLib" prefix="RefererTagLib" %>
<RefererTagLib:referer site="http://localhost" page="index.jsp"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Referer</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
</body>
</html>
---------------------------------------------------------------------------------------------------------------
三个标签案例:帮你深入学习JSP自定义标签
最新推荐文章于 2024-05-02 21:56:05 发布