top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to build custom tags in java?

+2 votes
401 views
How to build custom tags in java?
posted Sep 24, 2013 by Manish Negi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Custom tag is a tag you define in jsp. You define how a tag, its attributes and its body are interpreted and then group your tags into collections called tag libraries that can be used in any number of JSP files. So basically it is a reusable and extensible JSP only solution.

Step 1: Create a Custom tag class using only doStartTag()

 package tagPkg;
  public class MyTag extends TagSupport
   {
    int attr = null;
    public int setAttr(int a ttr){this.attr = a ttr}
    public int getAttr(){return attr;}
    public int doStartTag() throws JspException {
    .......
    return 0;
    }
    public void release(){.....}
   }

Step 2: The Tag library descriptor file (*.tld) maps the XML element names to the tag implementations. The code sample MyTagDesc.tld is shown below:

<taglib>
    <tag>
    <name>tag1</name>
    <tagclass>tagPkg.MyTag</tagclass>
    <bodycontent>empty</bodycontent>
    <attribute>
    <name>attr</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    </tag>
</taglib>

Step 3: The web.xml deployment descriptor maps the URI to the location of the *.tld (Tag Library Descriptor) file. The code sample web.xml file is shown below:

 <web-app>
  <taglib>
   <taglib-uri>/WEB-INF/MyTagURI</taglib-uri>
   <taglib-location>/WEB-INF/tags/MyTagDesc.tld</taglib-location>
  </taglib>
 </web-app>

STEP 4: The JSP file declares and then uses the tag library as shown below:

<%@ taglib uri="/WEB-INF/ MyTagURI" prefix="myTag" %>
< myTag:tag1 attr=?abc? />
<taglib>
    <tag>
    <name>tag1</name>
    <tagclass>tagPkg.MyTag</tagclass>
    <bodycontent>empty</bodycontent>
    <attribute>
    <name>attr</name>
    <required>false</required>
    <rtexprvalue>false</rtexprvalue>
    </attribute>
    </tag>
</taglib>
answer Sep 25, 2013 by Arvind Singh
Similar Questions
+1 vote

I have a requirement to populate the drop down list I need to load data from a database table to drop down list in java

my database table is this

CREATE TABLE CITY_MASTER_PL(
CITYID BIGINT NOT NULL,
INVENTORY_SOURCE CHAR,
CITY_NAME VARCHAR(250),
STATE_CODE VARCHAR(250),
STATE_NAME VARCHAR(250),
COUNTRY_CODE VARCHAR (250),
COUNTRY_NAME VARCHAR (250),
HOTEL_COUNT BIGINT,
LATITUDE BIGINT,
LONGITUDE BIGINT,
PRIMARY KEY (CITYID)
); 

in this table I need to load all country name state name and city name can any one help me out this thanks in advance

...