jsp自定义标签-----嵌套标签

开发类似sun公司提供的<c:if> <c:else>标签 

实现步骤: 

第一步:编写一个父标签处理器Choose.java 

public class Choose extends SimpleTagSupport { 

//标记执行那个标签 
private boolean isDo; 

public boolean isDo() { 
return isDo; 


public void setDo(boolean isDo) { 
this.isDo = isDo; 

//父标签控制标签体执行 
public void doTag() throws JspException, IOException { 

getJspBody().invoke(null); 





第二步:编写一个 子标签WhenTag.java 、OtherwithTag.java 类似 if else 
public class WhenTag extends SimpleTagSupport { 

private boolean test; 

public void setTest(boolean test) { 
this.test = test; 


public void doTag() throws JspException, IOException { 

Choose parent=(Choose) getParent(); 
if(test && !parent.isDo()) 

getJspBody().invoke(null); 
parent.setDo(true); 





public class OtherwithTag extends SimpleTagSupport { 

public void doTag() throws JspException, IOException { 

Choose parent=(Choose) this.getParent(); 
if(!parent.isDo()) 

getJspBody().invoke(null); 
parent.setDo(true); 




第三步:编写一个*.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>SimpleTagLibrary</short-name> 
    <uri>http://www.liyong.nesttag</uri> 
    <tag> 
        <description>show client IP</description> 
        <name>choose</name> 
        <tag-class>com.liyong.nestTag.Choose</tag-class> 
        <!-- 标签体不为空 这与传统标签不同 JSP --> 
        <body-content>scriptless</body-content> 
    </tag> 
    <tag> 
        <description>show client IP</description> 
        <name>when</name> 
        <tag-class>com.liyong.nestTag.WhenTag</tag-class> 
        <body-content>scriptless</body-content> 
        <attribute> 
        <name>test</name> 
        <required>true</required> 
        <rtexprvalue>true</rtexprvalue> 
        </attribute> 
    </tag> 
    
    <tag> 
        <description>show client IP</description> 
        <name>otherwith</name> 
        <tag-class>com.liyong.nestTag.OtherwithTag</tag-class> 
        <body-content>scriptless</body-content> 
       
    </tag> 
</taglib> 

第四步:编写一个jsp 使用标签并且导入自定义标签 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.liyong.nesttag" prefix="fix" %> 
<% 
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 'nestTag.jsp' starting page</title> 
    
<!-- 
<link rel="stylesheet" type="text/css" href="styles.css"> 
--> 

  </head> 
  
  <body> 
<br> 
<%request.setAttribute("user","user"); %> 
  <fix:choose> 
<fix:when test="${user==null }"> 
                                          未登陆. <br> 
</fix:when> 
<fix:otherwith> 
              welcome用户已经登录. <br> 
</fix:otherwith> 
  </fix:choose> 

  </body> 
</html> 

第五步:测试...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值