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

转载 2013年12月06日 09:20:16
开发类似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> 

第五步:测试...
举报

相关文章推荐

JSP自定义标签-嵌套

01、定义标签实现类 1.1、父类ParentTag.java import java.io.IOException;   import java.util.HashMap...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)