Milonic provide full featured pull down web menus for some of the worlds largest companies
click here to see what it can do for you

Download Milonic DHTML Menu
Buy Milonic DHTML Menu

Back To Start Of Archive
Taken From The Forum: Help & Support for DHTML Menu Version 5+
Forum Topic: Click to view post
Last Updated: Saturday July 14 2012 - 06:07:51

How to make subimage context insensitive?


Poster: halcyon
Dated: Friday December 19 2003 - 17:36:50 GMT

Hi we are using a webapp in Tomcat with these menus and the context it is deployed on can and will change. So far I've had to hardcode in what the exact link is, ie:
subimage="/salesweb/images/arrow.gif";

with salesweb being the app context. Now with jsp's I can simply use request.getContextPath() but that isnt going to work here. Also the working directory does and will change so I cannot really use that as a basis. Is there anything I can do short of parsing this file through ant and setting the context path?

Thanks in advance,
David


Poster: kevin3442
Dated: Friday December 19 2003 - 20:48:45 GMT

Hi David,

I'm no expert on JSP or on deploying web applications, but I have a couple of thoughts I'll share in case they might help.

(1) Could you bulid the the path and filename for the image in a string, then use that string in assigning a value to the style property? I.e., (and keep in mind that I'm not a JSP user, so the syntax may be completely off), something like:

To create the path string:
Code:
<% String imagePath = request.getContextPath() +  "/images/"%>

To assign the style property:
Code:
subimage = "<%=imagePath%>arrow.gif";

-or-
Code:
subimage = "<%=imagePath%>" + "arrow.gif";


(2) I'm wondering if you could code the path relative to the path of your servlet. I.e., I assume the servlet is located below the context path. So, if the servlet were /salesweb/servlet/yourServlet, then the subimage might be:
Code:
subimage="../images/arrow.gif";

Potential problem here might be if you map a different url to your servlet, breaking the relative path.

I believe Dave (Hergio) is a web app developer, so his expertise might be more illuminating here. Meantime, maybe the above will help in some small measure.

Kevin


Poster: halcyon
Dated: Monday December 29 2003 - 22:23:12 GMT

1) Could be done but the problem is I would have to embed my entire javascript style file inside a jsp for it to execute those commands.

2) Relative pathing is what we are shooting to completely avoid.

Any other ideas from the devs? Is there someway for JS to determine the context its running under and user a variable to insert that?


Poster: halcyon
Dated: Tuesday December 30 2003 - 18:13:33 GMT

Ok here is the resolution I ended up with for anyone down the road with a similar problem:

I had the menu style data contained in a file named menu_data.js, I chose to rename that to menu_data.jsp so our servlet engine (Tomcat) would process any jsp calls within the file. And on the offending line with pathing to my image I put:

subimage="<%=request.getContextPath()%>/images/arrow.gif";

then to include this menu_data.jsp in my actual webpage that contains the menu:
Code:
<script language=JavaScript type=text/javascript>
<% JspRuntimeLibrary.include(request, response,
       request.getContextPath() + "/tiles/scripts/menu_styles.jsp", out,
       true); %>
</script>


I had to actually write the java code in there instead of using the jsp:include tag because for some reason it would error out when I tried page="<%=request.getContextPath%>/tiles/scripts/menu_styles.jsp".

Anyway it works, HIH.
-David


Poster: Hergio
Dated: Saturday January 3 2004 - 17:35:55 GMT

Thats what I was going to tell you...
Many people (myself and Andy included) have changed the .js file for the menu data to something else, like .php or .asp. And in your case .jsp. This is perfectly fine as long as when it gets down to the browser (after whatever server parsing has finished) it looks like a javascript file so that the menu can begin parsing it up. So any preprocessing you have Tomcat do can get the context paths just as you have done and then the js file is always using the correct paths. Nice work.


Poster: rpray
Dated: Thursday September 29 2005 - 23:56:34 BST

I've done the same thing using JSP tag files. This method will only work if your servlet engine supports JSP 2.0. Tag files didn't exist before then.

1. copy the men_data.js file to men_data.tag file and put it in your WEB-INF/tags directory or in /META-INF/tags in a jar file if you are putting it in a library.

2. In the menu_data.tag file put an attribute line to pass in your app context:
<% __at__ attribute name="appContext" required="true" %>

3. Use appContext variable in the men_data.tag file where you need it. In this case:

subimage="<%= appContext %>/images/arrow.gif";

4. (Optional: only for tag library ) Create a .TLD file for your tag in the /META-INF folder of your jar file. It doesn't really matter what you call the file as
long as it ends in .TLD. For this discussion just say that we save it
as myTagFiles.tld.

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">

<description>My Tag Files</description>
<tlib-version>1.1</tlib-version>
<short-name>mtf</short-name>
<uri>myTagFiles</uri>
<tag-file>
<name>men_data</name>
<path>/META-INF/tags/menu_data.tag</path>
</tag-file>
</taglib>

When your done, the jar file should be organized like this:

<META-INF>
<tags>
menu_data.tag
myTagFiles.tld

To make the jar file create the above structure in another folder (taglib for our discussion) and do the following:

c:\> cd c:\taglib
c:\taglib> jar cvf myTagFiles.jar *

Once the jar file is created, put it in your WEB-INF\lib folder when you deploy.

(You can put as many tag-file tags in the .tld as you want. So if you have
more than one menu_data.js file then make a tag file for each one.)


5. Replace the javascript include statement in your .JSP with a call to the tag.

a) First tell the .JSP file where to find your tag.

If you're tag is in the WEB-INF/tags folder then:
<% __at__ taglib tagdir="/WEB-INF/tags" prefix="mtf" %>
else if your tag is in a tag library whose uri is "myTagFiles"
<% __at__ taglib uri="myTagFiles" prefix="mtf" %>

b) To use the tag:

<script type="text/javascript">
<mtf:menu_data appContext="<%= myAppContext %>" />
</script>

--------------------------------------------------------------------------------
This worked really slick for me. I used the tag library method because we have multiple web apps that use the same menu settings so it makes it really nice to have this stuff in an external reusable archive.

That's my two cents worth.


Poster: rpray
Dated: Friday September 30 2005 - 0:01:37 BST

I just noticed my text formatting got messed up. I'll use _ for spaces. The directory structure of the tag library should be as follows:

<META-INF>
__<tags>
____menu_data.tag
__myTagFiles.dlt

Sorry, about that. I don't type on forums much.


Poster: Ruth
Dated: Friday September 30 2005 - 11:39:45 BST

Thanks for the information. We appreciate it.

Ruth


Poster: Shap5202
Dated: Friday September 30 2005 - 13:55:43 BST

We're facing a similar issue in that we allow for different image folder (if you want to change the style of the app).

I haven't started converting all the hard-coded img src's yet, one thought we had was to store the location in a global javascript variable at the top of the page, and try to reference it when building the menu's since they are done in javascript.

though havent actually tried it yet so don't know if it will work. I'll let you know if you care. Just cause our thought was it saves a bunch of scriptlet calls to the server to get the location

That worked fine for me, not sure which is actually more effecient