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.

2 Answers 2

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

1
  • examulator source is really good. Upvoted and added a description in case its URL changes and the community is devoid of its contents ;) Dec 20, 2018 at 6:52
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.

2
  • this is only one way of doing it. Another, arguably more common, is to compile those tag files as java classes.
    – eis
    Feb 18 at 8:34
  • 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.
    – eis
    Feb 18 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.