jsp 自定义标签

《demo.jsp》
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/taget/hellotag.tld" prefix="mytag"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<mytag:hello value="dddd" />


《demo.tld》

<?xml version="1.0" encoding="UTF-8"?>
<!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>tagSample</short-name>
	<uri>/hellotag</uri>
	<tag>
		<name>hello</name>
		<tag-class>org.macpro.alone.web.taget.demo</tag-class>
		<body-content>empty</body-content>
		<attribute>
			<name>value</name><!-- 参数名 -->
			<required>false</required><!-- 是否必填 -->
			<rtexprvalue>false</rtexprvalue><!-- 是否能够以${}方式传值 -->
		</attribute>
	</tag>
</taglib>


《demo.java》

package org.macpro.alone.web.taget;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class HelloTaget extends TagSupport  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		System.out.println("dddd");
		JspWriter jspw= this.pageContext.getOut();
		try {
			jspw.print(value);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	private String value;
	public void setValue(String value) {
		this.value = value;
	}
	

}

你可能感兴趣的:(java,Web,jsp,servlet,sun)