How to make subimage context insensitive?
How to make subimage context insensitive?
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
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
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:
To assign the style property:
-or-
(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:
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
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: Select all
<% String imagePath = request.getContextPath() + "/images/"%>
Code: Select all
subimage = "<%=imagePath%>arrow.gif";
Code: Select all
subimage = "<%=imagePath%>" + "arrow.gif";
Code: Select all
subimage="../images/arrow.gif";
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
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?
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?
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:
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
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: Select all
<script language=JavaScript type=text/javascript>
<% JspRuntimeLibrary.include(request, response,
request.getContextPath() + "/tiles/scripts/menu_styles.jsp", out,
true); %>
</script>
Anyway it works, HIH.
-David
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.
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.
Dave Hergert
Software Engineer
"Helping to make the menu better, one
at a time."
Software Engineer
"Helping to make the menu better, one

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:
<%@ 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:
<%@ taglib tagdir="/WEB-INF/tags" prefix="mtf" %>
else if your tag is in a tag library whose uri is "myTagFiles"
<%@ 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.
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:
<%@ 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:
<%@ taglib tagdir="/WEB-INF/tags" prefix="mtf" %>
else if your tag is in a tag library whose uri is "myTagFiles"
<%@ 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.
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
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