JSP CUSTOM TAG LIBRARY reminder

JSP 2.0 Tag Files

TAG structure :

<prefix:name [attribute=value]*/>

)> prefix- user assigned namespace
> name -name of the tag
)> attribute- attribute name
)>  value -attribute value
> body – any body element nested within the tag
)> prefix- user assigned namespace> name -name of the tag)> attribute- attribute name)>  value -attribute value> body – any body element nested within the tag

how to use Continue reading

TLD : TAG LIBRARY EXAMPLE with ATTRIBUTE

Example of Custom Tag Library with attribute :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC
 "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
 "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd" >

<taglib>
 <tag>
  <name>requestParameter</name>
  <tagclass>tags.GetRequestParameterTag</tagclass>
  <bodycontent>empty</bodycontent>

  <attribute>
   <name>property</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
</taglib>

Elements in “attribute” :

name – (1) – Name of attribute
required – (?) – If value of it equal true, attribute HAVE TO BE present
rtexprvalue – (?) – If value this element is true, attribute CAN BE defined by JSP expression

Example of class :


package tags;

import java.io.IOException;

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

/**
 * 
 */
public class GetRequestParameterTag extends TagSupport
{

	private String property;

	/**
	 * 
	 * @param property
	 */
	public void setProperty( String property )
	{
		this.property = property;
	}

	public int doStartTag() throws JspException
	{
		ServletRequest req = pageContext.getRequest();
		String value = req.getParameter( property );

		try
		{
			pageContext.getOut().print( value == null ? "" : value );
		}
		catch ( IOException ex )
		{
			throw new JspException( ex.getMessage() );
		}
		return SKIP_BODY;
	}
}