Evgeny  Bovykin
Evgeny Bovykin

Reputation: 3069

Create JSP tag without creating new .tag file

I'd like to create a JSP tag that would be used multiple times within a single .jsp file. I don't want to create separate .tag file, but rather place this tag in the same .jsp file. Is this possible?

Upvotes: 0

Views: 572

Answers (2)

redhatvicky
redhatvicky

Reputation: 1930

AFAIK the answer would be no, because as per the custom tags documentation, in order to create and use a custom tag in JSP we should be having valid tag library descriptor file in order to define the custom tags and a Tag Handler Class in order to define what action should be performed when we use the custom tag in the JSP file.

Custom Tag was brought in to eliminate the need of the scriptlet tag inside JSP, separation of business logic from JSP so that it would be really easy to maintain and finally re-usability. Custom tags are an excellent way to abstract the complexity of business logic from the presentation of Web pages in a way that is easy for the Web author to use and control.

Moreover, there is one more important condition to render a Custom Tag: A Tag Library Descriptor. A Tag Library Descriptor is an XML document that contains information about a library as a whole and about each tag contained in the library. TLDs are used by the web container to validate the tags and also by JSP page development tools.

Tag library descriptor file must have the extension .tld and must be packaged in the /WEB-INF/ directory or subdirectory of the WAR file or in the /META-INF/ directory or subdirectory of a tag library packaged in a JAR. The container will take the file as Tag Descriptor fil only if its in tld format and it is packaged as part of WEB-INF directory. If a tag is implemented as a tag file and is packaged in /WEB-INF/tags/ or a subdirectory, a TLD will be generated automatically by the web container.

But there is another small approach to just avoid defining the tag lib as part of your application , you can just define the taglib in JSP and use it without having a tag library as part of your application. For example, lets assume there is a website called "www.example.com" and they have hosted a customlib as part of the application. In that case, you can use the custom lib directly in your JSP. Here below is a small Snipper which could demonstrate the same.

<%@ taglib uri = "http://www.example.com/custlib" prefix = "mytag" %>

<html>
   <body>
      <mytag:hello/>
   </body>
</html>

and here is a jsp directive equivalent for the same :

<jsp:directive.taglib uri = "http://www.example.com/custlib" prefix = "mytag" />

References and Sources :

https://docs.oracle.com/javaee/5/tutorial/doc/bnamu.html

https://www.studytonight.com/jsp/creating-a-custom-tag.php

https://www.tutorialspoint.com/jsp/taglib_directive.htm

Upvotes: 2

Shant Khayalian
Shant Khayalian

Reputation: 110

You can read or check Oracle library about Sharing Tag Libraries Across Web Applications

7.5 Sharing Tag Libraries Across Web Applications The following sections discuss the packaging, placement, and access of tag libraries and their TLD files:

Packaging Multiple Tag Libraries and TLD Files in a JAR File

Specifying Well-Known Tag Library Locations

Enabling the TLD Caching Feature

7.5.1 Packaging Multiple Tag Libraries and TLD Files in a JAR File The JSP specification allows the packaging of multiple tag libraries, and the TLD files that define them, in a single JAR file.

This section presents an example of multiple tag libraries packaged in a single JAR file. The JAR file includes tag handler classes, tag-library-validator (TLV) classes, and TLD files for multiple libraries.

The following lists the contents and structure of the JAR file. Note that in a JAR file with multiple TLD files, the TLD files must be located under the /META-INF directory or a subdirectory.

examples/BasicTagParent.class
examples/ExampleLoopTag.class
examples/BasicTagChild.class
examples/BasicTagTLV.class
examples/TagElemFilter.class
examples/TagFilter.class
examples/XMLViewTag.class
examples/XMLViewTagTLV.class
META-INF/xmlview.tld
META-INF/exampletag.tld
META-INF/basic.tld
META-INF/MANIFEST.MF

A JAR file with multiple TLD files must be placed in the /WEB-INF/lib directory or in an OC4J "well-known" tag library location as described in "Specifying Well-Known Tag Library Locations". During translation, the JSP container searches these two locations for JAR files, searches each JAR file for TLD files, and accesses each TLD file to find its <uri> element.

7.5.1.1 Key TLD File Entries In each TLD file, there is a <uri> element under the root <taglib> element. Use this feature as follows:

The <uri> element must specify a value that is to be matched by the uri setting of a taglib directive in any JSP page that wants to use the corresponding tag library.

To avoid unintended results, each <uri> value should be unique across all <uri> values in all TLD files on the server.

The value of the <uri> element can be arbitrary; however, it must follow the XML namespace convention.I t is simply used as a key and does not indicate a physical location. By convention, however, its value is of the form of a physical location.

The basic.tld file includes the following:

<taglib>

  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>basic</short-name>
  <uri>http://xmlns.oracle.com/j2ee/jsp/tld/demos/basic.tld</uri>

  ...

</taglib>

The exampletag.tld file includes the following:

<taglib>

  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>example</short-name>
  <uri>http://xmlns.oracle.com/j2ee/jsp/tld/demos/exampletag.tld</uri>

  ...

</taglib>

The xmlview.tld file includes the following:

 <taglib>
      ...
      <tlib-version>1.0</tlib-version>
      <jsp-version>1.2</jsp-version>
      <short-name>demo</short-name>
      <uri>http://xmlns.oracle.com/j2ee/jsp/tld/demos/xmlview.tld</uri>
      ...
    </taglib>

7.5.1.2 Key web.xml Deployment Descriptor Entries This section shows the elements of the web.xml deployment descriptor. These map the full URI values, as seen in the elements of the TLD files in the previous section, to shortcut URI values used in the JSP pages that access these libraries.

The <taglib> element can include two subelements:

<taglib-uri>

Contains the shortcut URI that will be used as the value of the uri attribute in the taglib directive in JSP pages that use the tag.

<taglib-location>

Contains the unique identifier for the tag library. In this case, the value actually indicates a key, not a location, and corresponds to the value in the TLD file of the desired tag library.

For the scenario of an individual TLD file, or the scenario of a JAR file that contains a single tag library and its TLD file, the subelement indicates the application-relative physical location (by starting with "/") of the TLD file or tag library JAR file. See "Specifying Well-Known Tag Library Locations" for related information.

For the scenario of a JAR file that contains multiple tag libraries and their TLD files, a subelement indicates the unique identifier of a tag library. In this case, the value actually indicates a key, not a location, and corresponds to the value in the TLD file of the desired tag library. See "Packaging Multiple Tag Libraries and TLD Files in a JAR File" for related information.

<taglib>
  <taglib-uri>/oraloop</taglib-uri> 
  <taglib-location>http://xmlns.oracle.com/j2ee/jsp/tld/demos/exampletag.tld
  </taglib-location> 
</taglib>
<taglib>
  <taglib-uri>/orabasic</taglib-uri> 
  <taglib-location>
     http://xmlns.oracle.com/j2ee/jsp/tld/demos/basic.tld
  </taglib-location> 
</taglib>
<taglib>
  <taglib-uri>/oraxmlview</taglib-uri> 
  <taglib-location>
     http://xmlns.oracle.com/j2ee/jsp/tld/demos/xmlview.tld
  </taglib-location> 
</taglib>

7.5.1.3 JSP Page taglib Directives for Multiple-Library Example This section shows the appropriate taglib directives, which reference the shortcut URI values defined in the web.xml elements listed in the preceding section.

The page basic1.jsp includes the following directive:

<%@ taglib prefix="basic" uri="/orabasic" %>

The page exampletag.jsp includes the following directive:

<%@ taglib prefix="example" uri="/oraloop" %>

The page xmlview.jsp includes the following directive:

<%@ taglib prefix="demo" uri="/oraxmlview" %>

7.5.2 Specifying Well-Known Tag Library Locations As an extension of the standard "well-known URI" functionality described in the JSP specification, OC4J supports the use of one or more directories, known as well-known tag library locations, where you can place tag library JAR files that will be shared across multiple Web applications.

The default well-known tag library location is the ORACLE_HOME/j2ee/home/jsp/lib/taglib/ directory. A tag library installed in this location will be available by default to all Web applications deployed to the OC4J instance.

You can also define additional shared tag library locations, and install tag library JAR files to be shared across applications in these directories. Defining a well-known tag library location is a two-step process:

Define each directory in the jsp-taglib-locations attribute of the of the element in the ORACLE_HOME/j2ee/home/config/global-web-application.xml file. Separate each location with a semicolon.

Add a element for each directory to ORACLE_HOME/j2ee/home/config/application.xml, the configuration file for the default application. Set the path attribute to the directory containing the tag library JAR file.

Full documentary is down the ink below

Sharing Tag Libraries Across Web Applications

Upvotes: 0

Related Questions