tld定义格式
[java] view plain copy print?
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
A simple appbase tag library
定义Tag对应类
此类必须重写doStartTag以及doEndTag方法
[java] view plain copy print?
/**
*
*/
package com.cms.common.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author louisliao
*
*/
public class DemoViewTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String cmsform = "";
public String getCmsForm() {
return cmsform ;
}
public void setCmsForm(String cmsform ) {
this.cmsform = cmsform ;
}
/**
*
*/
public DemoViewTag() {
// TODO Auto-generated constructor stub
}
public int doStartTag()
{
return super.SKIP_BODY;
}
public int doEndTag() throws JspException
{
JspWriter writer=pageContext.getOut();
try {
writer.print("这是我的标签示例
"+"cmsform :"+this.cmsform);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
在web.xml中加入taglib对应文件配置
如:
[java] view plain copy print?
这样就表示了http://mytag.sf.net对应WEB-INF/mytag.tld文件
在Jsp页面中引用
如:
<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>
在Jsp页面中使用
示例:
定义myTag.tld标签文件
[java] view plain copy print?
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
定义标签类
[java] view plain copy print?
/**
*
*/
package com.myapp.web.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author louisliao
*
*/
public class DemoViewTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String northTitle = "";
private String westTitle = "";
public String getNorthTitle() {
return northTitle;
}
public void setNorthTitle(String northTitle) {
this.northTitle = northTitle;
}
public String getWestTitle() {
return westTitle;
}
public void setWestTitle(String westTitle) {
this.westTitle = westTitle;
}
/**
*
*/
public DemoViewTag() {
// TODO Auto-generated constructor stub
}
public int doStartTag()
{
return super.SKIP_BODY;
}
public int doEndTag() throws JspException
{
JspWriter writer=pageContext.getOut();
try {
writer.print("这是我的标签示例
westTitle:"+this.westTitle+"
northTitle:"+this.northTitle);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return super.EVAL_PAGE;
}
}
web.xml添加配置
[java] view plain copy print?
测试页面
[java] view plain copy print?
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
This is my JSP page.
直接用,前提是你已经定义好了标签
额,你这不是在JSP页面上执行的啊。如果要用JSP调用自定义的类的话,可以这样的: