Embedding Tag Files in a JAR
Asked Answered
T

2

3

Is it possible to do this? What I'm trying to accomplish here is the creation of an extensible Struts 2 plugin with customizable screens to avoid code duplication in similar projects.

Tarnopol answered 5/3, 2012 at 20:14 Comment(0)
T
4

Yes, it is possible, but it has nothing to do with Struts 2: http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags6.html#wp90207 (under "Packaged Tag Files").

Here is an example: http://www.examulator.com/moodle/mod/resource/view.php?id=473

Tarnopol answered 16/3, 2012 at 20:28 Comment(1)
examulator source is really good. Upvoted and added a description in case its URL changes and the community is devoid of its contents ;)Aerostat
A
0

Quoting steps from this source(taken from prev. answer) in case URL changes. (Simplest solution I found on the internet)

When wrapped in a jar (Java archive) tag files require a tld.

menu.tld placed in the META-INF directory.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
    <tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>menutagfile</shortname>

    <uri>www.examulator.com/menutagfile</uri>
    <tag-file>
        <name>menu</name>
        <path>/META-INF/tags/menu.tag</path>
    </tag-file>
</taglib>

menu.tag placed in the META-INF\tags directory.

<%@ tag body-content="tagdependent" %>
<%@ attribute name="menutext" rtexprvalue="true"%>

<h1>This is my tag file</h1>
<jsp:doBody/>

The command to package these into JAR(Auto package in case of maven)

Jar cvf menutagfile.jar .\META-INF\*.*

Usage in the parent project

<%@ taglib prefix="mytagfile" uri="www.examulator.com/menutagfile" %>
<html>
<head>
<title>Demonstration of Tag Files</title>
</head>
<body>
<h1> What is going down? </h1>
<mytagfile:menu/>
</body>
</html>

Note: In case you do not have a META-INF folder in your project, create one inside src/main/resources.

Aerostat answered 20/12, 2018 at 6:49 Comment(2)
this is only one way of doing it. Another, arguably more common, is to compile those tag files as java classes.Endowment
Also, directory src/main/resources is specific to maven. If you're not using maven (or gradle configured that way), then that advise is not applicable.Endowment

© 2022 - 2024 — McMap. All rights reserved.