jsp自定义标签: jsp自定义标签(switch{}功能)



定义switch

package tag;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

public Switch extends TagSupport {
private boolean subtagsuccess;

public Switch{
subtagsuccess=false;a
}

/*
* 当遇到switch标签时所有子标签都不执行
* @see javax.servlet.jsp.tagext.TagSupport#doStartTag
*/
public doStartTagthrows JspTagException{
subtagsuccess=false;
EVAL_BODY_INCLUDE;
}

/*
* 该思路方法有子标签表示是否可以执行自身标签
*/
public synchronized boolean getpermission{

(!subtagsuccess);
}

public synchronized boolean subtagsuccessed{
subtagsuccess=true;
}

/*
* 销毁到该思路方法
* @see javax.servlet.jsp.tagext.TagSupport#release
*/
public void release{
subtagsuccess=false;
}
}






定义

package tag;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public Case extends TagSupport{
private boolean cond;

public Case{
cond=false;
}
public void release{
cond=false;
}

public void Cond(boolean cond){
this.cond=cond;
}

public doStartTagthrows JspTagException{
Tag parse=getParent;
/*
* 判断是否可以执行本身
*/
(!((Switch)parse).getpermission)
SKIP_BODY;
/*
* 如果条件是真通知父类表示已经有了个符合条件
* 否则忽略标签体
*/
(cond){
((Switch)parse).subtagsuccessed;
EVAL_BODY_INCLUDE;
}
{
SKIP_BODY;
}
}
}




定义default



package tag;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public Default extends TagSupport {
public void Default{
}

public doStartTagthrows JspTagException{

// 0;
Tag default=getPareant;
(!((Switch)default).getpermission)
SKIP_BODY;

((Switch)default).subtagsuccessed;
EVAL_BODY_INCLUDE;

}

}








tld文件



<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<-name>swicth</-name>
<display-name>simpley swicth</display-name>
<description>simple jsp tag3 </description>


<tag>
<name>swicth</name>
<tag->tag.Switch</tag->
<body-content>jsp</body-content>
<description>simple jsp tag 03</description>

</tag>


<tag>
<name></name>
<tag->tag.Case</tag->
<body-content>jsp</body-content>
<description>simple jsp tag 03</description>

<attribute>
<name>cond</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<!-- true表示可以用接受表达式;false:反的 -->
</attribute>
</tag>


<tag>
<name>default</name>
<tag->tag.Default</tag->
<body-content>jsp</body-content>
<description>simple jsp tag 03</description>

</tag>
</taglib>








webx.xml文件



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
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-app_2_5.xsd">



<jsp-config>
<taglib>
<taglib-uri>/jsptag3.tld</taglib-uri>
<taglib-location>/WEB-INF/jsptag3.tld</taglib-location>
</taglib>
</jsp-config>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>







定义jsp页面



<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/jsptag3.tld" prefix="djl" %>
<%
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 'index.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">
-->
a
</head>

<body>
<% String name=request.getParameter("name"); %>
<djl:swicth>
<djl: cond="<%=name.equals("dengjianli")%>">
<%out.pr(" 您输入是:dengjianli"); %>
</djl:>
<djl: cond="<%=name.equals("dengxiaoping") %>">
<%out.pr(" 您输入是:dengxiaoping"); %>
</djl:>

<djl:default>
<%out.prln("没有您合适输入数据!"); %>
</djl:default>
</djl:swicth>
</body>
</html>
Tags:  struts2自定义标签 自定义标签 jsp自定义标签库 jsp自定义标签

延伸阅读

最新评论

发表评论