如何在JavaWeb程序中使用tld文件

 tld定义格式






1.0

1.2
Cms Tag

A simple appbase tag library



page
com.cms.common.tag.PageTag
empty




cmsform
true
true







定义Tag对应类

此类必须重写doStartTag以及doEndTag方法

/**
 * 
 */
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对应文件配置

如:


http://mytag.sf.net
/WEB-INF/mytag.tld


这样就表示了http://mytag.sf.net对应WEB-INF/mytag.tld文件

 

在Jsp页面中引用

如:

<%@ taglib uri="http://mytag.sf.net" prefix="myTag"%>

 

在Jsp页面中使用

 

示例:

定义myTag.tld标签文件




	1.0
	1.1
	MyJSPTag Library
	http://mytag.sf.net
	我的示例标签库
	
	
		demo.Viewport
		com.myapp.web.tag.DemoViewTag
		JSP
		demo.Viewport标签
		
			northTitle
			true
			true
		
		
			westTitle
			true
			true
			
	


定义标签类

/**
 * 
 */
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添加配置


http://mytag.sf.net
/WEB-INF/mytag.tld


测试页面

<%@ 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+"/";
%>



  
    
    
    My JSP 'tagtldDemo.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    This is my JSP page. 


 

 

 

你可能感兴趣的:(web.xml)