package com.rain.tag;
import Java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/*
* if tag
* usage:<tag:if value=true>
* ...
* </tag:if>
*/
public class IfTag extends BodyTagSupport {
private boolean value;
public void setValue(boolean value){
this.value=value;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
try{
if(bodyContent!=null){
bodyContent.writeOut(bodyContent.getEnclosingWriter());
}
}catch(IOException e){
throw new JspTagException("IO Error:"+e.getMessage());
}
return EVAL_PAGE;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
if(value){
System.out.println("value is true");
return EVAL_BODY_INCLUDE;
}else{
System.out.println("value is false");
return SKIP_BODY;
}
}
}
package com.rain.tag;
import Java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
public class OutTag extends TagSupport {
private Object value;
public void setValue(Object value){
this.value=value;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return EVAL_BODY_INCLUDE;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
try{
System.out.println(value);
pageContext.getOut().write(value.toString());
}catch(IOException e){
throw new JspTagException("IO Error:"+e.getMessage());
}
return EVAL_PAGE;
}
}
<tag>
<name>if</name>
<tag-class>com.rain.tag.IfTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.rain.tag.OutTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "
http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<taglib>
<taglib-uri>/demotag</taglib-uri>
<taglib-location>/WEB-INF/mytag.tld</taglib-location>
</taglib>
</web-app>
<%@ page language="
Java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/demotag" prefix="mt" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<%
boolean flag=true;
String outValue="Hello World!";
%>
<mt:if value="<%=flag%>">
<mt:out value="<%=outValue%>">
这是mt:out...>打印的内容。
</mt:out>
</mt:if>
<br>
<mt:if value="false">
<mt:out value="<%=outValue%>">
这些内容不会显示在客户端。
</mt:out>
</mt:if>
</body>
</html>
结果显示:
控制台
value is true
Hello World!
value is false
页面
Hello World! 这是mt:out...>打印的内容。
posted on 2007-01-22 15:27
周锐 阅读(313)
评论(0) 编辑 收藏 所属分类:
Jsp