自定义jsp页面标签过程

1.       编写标签实现类HelloWorldTag extends TagSupport

package com.adam.easyTag.test;

import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
public class HelloWorldTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	public HelloWorldTag() {
	  }
	  public int doStartTag() throws JspTagException{
	    return EVAL_BODY_INCLUDE;
	  }
	  public int doEndTag() throws JspTagException{
	    try {
	      pageContext.getOut().write("Hello World");
	    }catch (IOException ex) {
	      throw new JspTagException("错误");
	    }
	    return EVAL_PAGE;
	  }
}

2.       编写自定义标签定义文件*.tld

<?xml version="1.0" encoding="GBK"?>
<!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>
   <short-name>mytag</short-name>
   <tag>
       <name>helloworld</name>
       <tag-class>com.adam.easyTag.test.HelloWorldTag</tag-class>
       <body-content>empty</body-content>
   </tag>
</taglib>


3.       在web.xml中配置自定义标签

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

4.       编写jsp页面

<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="/helloworld" prefix="mytag" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>jsp1</title>
</head>
<body style="background-color:pink">
	<form action="">
	<h1>下面显示的是自定义标签中的内容</h1>
	<br><br>
	<mytag:helloworld></mytag:helloworld>
	<br>
	</form>
</body>
</html>


5.       tamcat测试发布

自定义jsp页面标签过程_第1张图片



你可能感兴趣的:(html,jsp,action,include,library,encoding)