自定义标签

package model;

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

/**
 * 
 * @author sally
 * s:if test
 */
public class IFTag extends TagSupport {
private boolean test;
	
	@Override
	public int doAfterBody() throws JspException {
		// TODO Auto-generated method stub
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return EVAL_PAGE;
	}

	/**
	 * 遇到自定义开始标签时,会自动回调doStartTag方法
	 */
	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		if(test){
			return EVAL_BODY_INCLUDE;
		}else{
			return SKIP_BODY;
		}
	}
	/**
	 * 
	 * @param test the test to set
	 * 通过set方法,给属性赋值
	 */
	public void setTest(boolean test){
		this.test=test;
	}

}
package model;

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

public class LoopTag extends TagSupport {

	private int count;
	public LoopTag(){
		System.out.println("创建对象");
	}
	@Override
	public int doAfterBody() throws JspException {
		System.out.println("执行到了doAfterBody");
		--count;
		if(count>0){
			return EVAL_BODY_AGAIN;
		}else{
			return SKIP_BODY;
		}
	}

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {
		if(count>0){
			return EVAL_BODY_INCLUDE;
		}else{
			return SKIP_BODY;
		}
	}
	/**
	 * 
	 * @param count the count to set
	 */
	public void setCount(int count){
		System.out.println("给count赋值");
		this.count=count;
	}

}
<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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-jsptaglibrary_2_1.xsd"
    version="2.1">
    
   <description>JSP custom tag</description>
  <display-name>jsp custom</display-name>
  <tlib-version>1.1</tlib-version>
  <short-name>s</short-name>
  <uri>http://www.deruntechonology.com</uri>
  
  	<tag>
  		<description>s:if</description>
  		<name>if</name>
  		<tag-class>model.IFTag</tag-class>
  		<body-content>JSP</body-content>
  		<attribute>
  			<name>test</name>
  			<required>true</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
  	</tag>
  	<tag>
  		<description>s:loop</description>
  		<name>loop</name>
  		<tag-class>model.LoopTag</tag-class>
  		<body-content>JSP</body-content>
  		<attribute>
  			<name>count</name>
  			<required>true</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
  	</tag>
  </taglib>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.deruntechonology.com" prefix="s"%>
<%
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>JSP 自定义标签 </title>
	
  </head>
  
  <body>
  <%
  	request.setAttribute("c", 2);
   %>
    <s:if test="${5>4 }">真的</s:if>
    
    <s:loop count="${c }">
    	hello
    </s:loop>
    <h3>标题</h3>
  </body>
</html>


你可能感兴趣的:(自定义标签)