Core Tag Simple Examples

For using core tags in JSP, first put “jstl.jar” and “standard.jar” in the WEB-INF/lib directory of the web application. Then you have to include the taglib directive in JSP as

 <%@ taglib uri =”http://java.sun.com/jstl/core” prefix =”c” %>

Here the uri is nothing but the uri specified in the c.tld file (c.tld is there in standard.jar). You can give any value as prefix other than the reserved words jsp, jspx, java, javax, servlet, sun, sunw. Whatever we are giving as prefix, it should come before the tag. Like if you are giving a prefix ‘s’ then the tag ‘out’ will be written as ‘s:out’ rather than ‘c:out’. 

Below are some core tags and their simple usage. Though it is not required to see the tld file for using simple tags, it is good idea to do so to have a better understanding.

1) out tag. See the tag description in c.tld

 <tag>
    <name>out</name>
    <tag-class>org.apache.taglibs.standard.tag.el.core.OutTag</tag-class>
    <body-content>JSP</body-content>
    <description>
 Like &lt;%= … &gt;, but for expressions.
    </description>
    <attribute>
        <name>value</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>default</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>escapeXml</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

Here the <name> is nothing but name of the tag
<tag-class> specifies the name of the tag handler class.
<body-content> specifies whether the tag can have a body or not. Here the value ‘JSP’ tells that the tag ‘out’ can have a JSP body.
<attribute> element specifies the attributes that a tag can have.
<name> is the name of the attribute
<required> specifies whether the attribute is mandatory or not. If it is true, we have to provide a value for that attribute.
<rtexprvalue> specifies whether the attribute can take request time expressions or not.

The attribute default is being used for passing a default value to the tag. In case the value attribute is null the tag will display the default value.

escapeXml tells whether the xml has to be rendered or not while displaying the value.

From the above description it is clear that the ‘out’ tag can have a JSP body and it has a mandatory attribute value. The tag is used for display values in JSP.  The value to be displayed should be passed as the attribute value to the tag.

<%@ taglib uri =”http://java.sun.com/jstl/core” prefix =”c” %>
<html>
<body>
<c:out value =”Welcome to Core Tags” />
</body>
</html>

Usage of escapeXml.

Try this <c:out value =”<b>Welcome to Core Tags</b>” />. On running the same you can see the <b> tags being displayed on screen. The XML is displayed as a text.

Try this <c:out value =”<b>Welcome to Core Tags</b>” escapeXml = “false”/>. On running the same you can see the <b> tags are gone and the text is displayed in bold. Here the XML is being rendered. By default escapeXml is set to  true.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.