Problem with '&' in a URL string

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
schnocky
Beginner
Beginner
Posts: 5
Joined: Mon Aug 30, 2004 7:39 pm

Problem with '&' in a URL string

Post by schnocky »

I have read through another lengthy dialog on this issue (Can't get link with '&' to work), but the solution doesn't seem to work for me.

I am dynamically generating content for the menu using data from the database (JavaServer Pages and JSTL). I have a JSP that I create which contains several request parameters (SEPARATED BY &)

http://www.foo.com/bar.jsp?parm1=bar1&p ... arm3=bleah

This appears on the page as:
http://www.foo.com/bar.jsp?parm1=bar1&a ... arm3=bleah

which promptly has the URL truncated to:
http://www.foo.com/bar.jsp?parm1=bar1&amp

which loses any request parameter after the first one.

I have tried replacing '&' with '%26' in the string (doesn't work). I believe that it is the output of the <c:out> JSTL tag that is converting the '&' character to '&', but that it is the menu code that is truncating the URL when it encounters the semicolon.

Thanks for any help on this problem.

steve
User avatar
Andy
Milonic
Milonic
Posts: 3308
Joined: Sun May 19, 2002 8:23 pm
Location: Menu Developer
Contact:

Post by Andy »

Hi,

This is where backquotes come into the equation.

Because the menu relies on both semi-colons and the equals sign for it's text manipulation, the menu has trouble knowing that the text ;parm2=foo3&parm3=bleah is part of the url.

All you need to do is tell it to leave it alone and it will. This is done by enclosing the url inside backquotes. Backquotes are the little reversed single quote character. On my UK keyboard it is next to the number 1 on the top row but could be somewhere else on other International keyboards.

Anyway, here's how you should include the aI() string:

Code: Select all

aI("text=sample;url=`http://www.foo.com/bar.jsp?parm1=bar1&parm2=foo3&parm3=bleah`;");
Hope this helps
Andy
schnocky
Beginner
Beginner
Posts: 5
Joined: Mon Aug 30, 2004 7:39 pm

Post by schnocky »

OK, sounds good. Right now I only have relative URLs, will they work?

I have an output of

aI("text= Other;url='/bar/reports/startpage.jsp?p1=2&p2=404;");

and the menus don't show up.

steve
perldev
Mega User
Mega User
Posts: 115
Joined: Thu Aug 26, 2004 5:23 pm
Location: Chicago

Post by perldev »

Try this out:

Code: Select all

aI("text=Other;url=/bar/reports/startpage.jsp?p1=2"+"&p2=404");
User avatar
John
 Team
 Team
Posts: 5967
Joined: Sun May 19, 2002 8:23 pm
Location: Phoenix, AZ
Contact:

Post by John »

schnocky wrote:I have an output of

aI("text= Other;url='/bar/reports/startpage.jsp?p1=2&p2=404;");

and the menus don't show up.
Looks like you're using a regular quote there rather than a backquote, and it's not closed anyway.

Try...

Code: Select all

aI("text= Other;url=`/bar/reports/startpage.jsp?p1=2&p2=404`;");
John
schnocky
Beginner
Beginner
Posts: 5
Joined: Mon Aug 30, 2004 7:39 pm

Post by schnocky »

Thanks for the suggestions, I finally found a combination that will work.

Here are my observations.

I did use the backquote character '`' to surround the URL. This works as long as you have a fully qualified URL. When I added it to a relative URL, the menus did not show up.

I also tried the suggestion by perldev, but the JSP processor converts the output to

"/abc/page.jsp?foo=1" + "&bar=2"

(e.g. translates '&' to "&" for me)
schnocky
Beginner
Beginner
Posts: 5
Joined: Mon Aug 30, 2004 7:39 pm

Post by schnocky »

OK, it took one more iteration to solve my problem. It seems that the issue was the JSP engine replacing '&' with "&" that was the root cause of the problem. The solution was to modify the JSP page and have the '&' explicitly on the page while using the JSP engine to generate the rest of the string. Then, the menu code didn't have an additional semicolon to contend with and things were quite happy and tranquil. My code does something like this

For the example string attribute 'outputString of my class,

/main/foo/bar.asp?parm1=it&parm2=worked&parm3=swell

I was using

aI("<c:out value='${theItem.outputString}'/>");

which resulted in

aI("text=Menu1;url=/main/foo/bar.asp?parm1=it&parm2=worked&parm3=swell;");

which was parsed as /main/foo/bar.asp?parm1=it&amp

While adding backquotes enabled the menus to be displayed (and retain the entire URL string), clicking on the link resulted in the loss of all request parameters (the "&" in the string screwed that up.
---------------------------------------------------------

I now use the following code to create the output

<c:forEach items='${menu.menuItem}' var='theItem'>
<c:set var='accessibleItem' value='${theItem}' scope='request'/>
<% String s = ((DhtmlMenuItemData) request.getAttribute("accessibleItem")).getOutputString();
int strpos = s.indexOf("&");
out.write("aI(\"");
while (strpos > 0) {
strpos++;
out.write(s.substring(0, strpos - 1));%>&<%
s = s.substring(strpos, s.length()-1);
strpos = s.indexOf("&");
}
out.write(s + "\");");
%>
</c:forEach>


Thanks for the suggestions.

steve
Post Reply