JSP el

How to create a variable using <c:set> whose value is

specified within the tag body . I want to use this

variable created as one of the parameters in a

javascript method call . But my problem is when I run

my application and do the view source , the HTML page

shows me the variable as it is without substituting

its value .

Also the variable(photolink) iam creating should be unique for each row, so iam appending index id to it . Is it correct ?

can anyone help ? its urgent

This is my code :

<c:forEach var="photo" items="${photos}" varStatus="status" >

<c:set var="photolink${status.index}" >

Click to view full-size:

<a href='http://147.147.2.91:7001/pfk-deploy/GetImage?photo="{photo.photoid}"' target='_New'><img style=margin-left:20px src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo="{photo.photoid}"' border=0 width=75 height=56></a>

</c:set>

doSearch( "<%=desc%>", '{c:out value="${photolink}"/}');

</c:forEach>

[1103 byte] By [Nidhi@BTa] at [2007-10-3 1:58:07]
# 1

[nobr]Is that your actual code? At several places you have missed out the '$' in the EL.

You cannot use a dynamic EL expression in the var attribute of the <c:set> tag.

So while <c:out value = "photolink${status.index}"/>

will work and display photolink1, photolink2 etc as the tag progresses its iteration, you cannot use the same attribute value in the <c:set> tag.

However if the variable is only used to pass on to a javascript function, you could create a javascript variable itself and my take is that you really dont need the index unless the doSearch() function does anything based on it. Anyways here goes,

<c:forEach var="photo" items="${photos}" varStatus="status">

<script>

var photolink${status.index} = "Click to view full-size:<br>

<a href='http://147.147.2.91:7001/pfk-deploy/GetImage?photo=\"${photo.photoid}\"' target='_New'>

<img style=margin-left:20px src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=\"${photo.photoid}\"' border=0 width=75 height=56></a>

<br><br>";

</script>

doSearch( "<%=desc%>", photolink${status.index});

</c:forEach>

Hope that helps,

ram.

PS: use the code tags when you post code. Look at the code button above the posting area.[/nobr]

Madathil_Prasada at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

[nobr]From your other post, you are using weblogic 8.1 and servlets 2.3.

EL is disabled by default and 8.1 is a j2EE 1.3 container, so you'll have to replace EL by the c:out tag.

<c:forEach var="photo" items="${photos}" varStatus="status">

<script>

var photolink<c:out value="${status.index}"/> = "Click to view full-size:<br>

<a href='http://147.147.2.91:7001/pfk-deploy/GetImage?photo=\"<c:out value="${photo.photoid}"/>\"' target='_New'>

<img style=margin-left:20px src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=\"<c:out value="${photo.photoid}"/>\"' border=0 width=75 height=56></a>

<br><br>";

</script>

doSearch( "<%=desc%>", "<c:out value="photolink${status.index}"/>");

</c:forEach>

ram.[/nobr]

Madathil_Prasada at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi Ram,

Thanks for the reply , i'd re-written the code but my problem remains the same , the variable photolink is not getting substituted in the method by the value stored in it, instead it is just printing me like this:

"photolink0"

"photolink1" and so on ,,,

Instead my requirement is that the variable should be substituted by the value stored in it .

Hope you'll be able to help again.

Thanks

Nidhi@BTa at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

doSearch() is a javascript function, right?

There is a error in my previous post. Change

doSearch( "<%=desc%>", "<c:out value="photolink${status.index}"/>");

to

doSearch( "<%=desc%>", photolink<c:out value="${status.index}"/>);

If you have further problems, post your doSeach() function. Better - If your jsp isnt too large, post it.

ram.

Madathil_Prasada at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi..

No Luck still..

the doSearch(name changed) is a javascript function , i'll post the original code for you. its as :

<script>

function load()

{

if (GBrowserIsCompatible()) {

<%

// some other code goes here ////

/*

this needs the URL that shows the latest thumbnail. In fact, perhaps the three latest thumbnails... */

%>

<c:forEach var="photo" items="${photos}" varStatus="status" >

var photolink<c:out value="${status.index}"/> = "Click to view full-size:

<a href='http://147.147.2.91:7001/pfk-deploy/GetImage?photo=<c:out value="${photo.photoid}"/>' target='_New'><img src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=<c:out value="${photo.photoid}"/>' border=0 width=75 height=56></a>

";

map.addOverlay(createMarker(mpoint, ico<%=siteType%><%=iconum%>, "<%=iconType%>", "<%=sitetypeid%>", "<%=iconum%>", "<%=desc%>", photolink<c:out value="${status.index}"));

></c:forEach>

}//end if

}//end load

function createMarker(mpoint, icon, iconType, siteTypeid, cntr, mdesc, mphotolink)

{

/* mdesc and mphotolink separated so the sidebar can use only mdesc

wrap HTML in a div of limited width to shorten the popup windows */

mdesc = '<div style="width:200px"><small>' + mdesc + '</small></div>';

mphotolink = '<div style="width:200px"><small>' + mphotolink + '</small></div>';

var infoTabs = [

new GInfoWindowTab("Site info", mdesc),

new GInfoWindowTab("Recent pic", mphotolink)];

var marker = new GMarker(mpoint, icon);

var iconsrc;

GEvent.addListener(marker, "click", function() {marker.openInfoWindowTabsHtml(infoTabs); });

gmarkers = marker;

// twin arrays to hold HTML for tabbed windows

htmls = mdesc;

photohtmls = mphotolink;

//other code

}

the photolink variable loads photos at run time so iam creating a new variable for every photoid and passing it down further . Hope iam able to explain myself .

thanks in advance

Nidhi@BTa at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

[nobr]whoa, that's a pretty complex script that you have there :)

Lets consign ourselves to check if the value of the variable is being passed correctly (which is your original problem).

Lets do this by adding a javascript alert for the variable inside the load() method.

<script>

function load()

{

//yada yada

<c:forEach var="photo" items="${photos}" varStatus="status" >

var photolink<c:out value="${status.index}"/> = " Click to view full-size:<br><a href='http://147.147.2.91:7001/pfk-deploy/GetImage?photo=<c:out value="${photo.photoid}"/>' target='_New'><img src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=<c:out value="${photo.photoid}"/>' border=0 width=75 height=56></a><br><br>";

alert (photolink<c:out value="${status.index}"/>);

//more yada yada

What does that alert display? The variable name or the value of the variable?

ram.

PS: What's map.addOverlay()? Another javascript function?[/nobr]

Madathil_Prasada at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Hi,

The alert is displaying me the value as :

click to view full size........

actually the addOverlay method is a Google Map API method which i dont know what is like , iam just using it and passing the required data .

Earlier i was using struts taglib to do the same stuff as follows:

<logic:notEmpty name="photos">

<logic:iterate id="photo" name="photos" scope="request" indexId="ctr">

<bean:define id="photolink">

Click to view full-size:

<a href='http://147.147.2.91:7001/pfk-deploy/GetImage?photo=<bean:write name="photo" property="photoid"/>' target='_New'><img style=margin-left:20px src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=<bean:write name="photo" property="photoid"/>' border=0 width=75 height=56></a>

</bean:define>

map.addOverlay(createMarker(mpoint, ico<%=siteType%><%=iconum%>, "<%=iconType%>", "<%=sitetypeid%>", "<%=iconum%>", "<%=desc%>", "<%=photolink%>"));

</logic:iterate>

</logic:notEmpty>

this was working fine , but the only problem i got was the photolink variable was just printing me the last photo id and not all as its quite obvious from the code , so someone suggested me to use EL to generate dynamic variable for each row and then all my problems started. I dont know EL much actually.

can i do this using taglibs otherwise EL is fine , whichever works

and one more thing the foll line is giving javascript error(Expected ))

photolink<c:out value="${status.index}"

thanks again>

Nidhi@BTa at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

[nobr]>The alert is displaying me the value as :

>click to view full size........

Which means that the value of the variable is being evaluated and passed, right? And that's what is expected and how it would work when you pass it to your javascript function.

And now to your index problem. I cant figure out why you need the index for the variable. Remember whatever be the variable name, its always the value that is held in the variable that is passed to the function.

Even in the struts tag post that you did, each time into the <logic:iterate> loop, a bean called 'photolink' maps to a value within the tag (Click...etc).

<logic:iterate id="photo" name="photos" scope="request" indexId="ctr">

<bean:define id="photolink">

//each time into the loop, the bean holds this value

Click.....?photo=<bean:write name="photo" property="photoid"/>..

</bean:define>

And then when you pass the defined bean to the function, it is the value of the bean (ie Click....etc) that is passed

map.addOverlay(createMarker(mpoint, ico<%=siteType%><%=iconum%>, "<%=iconType%>", "<%=sitetypeid%>", "<%=iconum%>", "<%=desc%>", "<%=photolink%>"));

And finally your javascript function uses that value for some purpose.

Each invocation of the function generates a photo, right? So that many photos would be generated, each with a different id as many times as the loop is transversed.

Either that or I am missing something here.

ram.[/nobr]

Madathil_Prasada at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

[nobr]>The alert is displaying me the value as :

>click to view full size........

Which means that the value of the variable is being evaluated and passed, right? And that's what is expected and how it would work when you pass it to your javascript function.

And now to your index problem. I cant figure out why you need the index for the variable. Remember whatever be the variable name, its always the value that is held in the variable that is passed to the function.

Even in the struts tag post that you did, each time into the <logic:iterate> loop, a bean called 'photolink' maps to a value within the tag (Click...etc).

<logic:iterate id="photo" name="photos" scope="request" indexId="ctr">

<bean:define id="photolink">

//each time into the loop, the bean holds this value

Click.....?photo=<bean:write name="photo" property="photoid"/>..

</bean:define>

And then when you pass the defined bean to the function, it is the value of the bean (ie Click....etc) that is passed

map.addOverlay(createMarker(mpoint, ico<%=siteType%><%=iconum%>, "<%=iconType%>", "<%=sitetypeid%>", "<%=iconum%>", "<%=desc%>", "<%=photolink%>"));

And finally your javascript function uses that value for some purpose.

Each invocation of the function generates a photo, right? So that many photos would be generated, each with a different id as many times as the loop is transversed.

Either that or I am missing something here.

ram.[/nobr]

Madathil_Prasada at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

yeah ur right but then why is it not loading all the photos, for eg if my bean has 4 ids then at runtime it shud load me all 4 photos but in reality it is just loading me the last iteration photo id and hence just a single photo on running the jsp .

the generate html code shud be like this :

map.addOverlay(createMarker(mpoint, icoP6, "sq", "P", "6", "<b>PCP: </b><a href='http://www.pfk.com/search?siteID=4705'>WWLAUNp14</a>

<b>Address:</b> <small>MOORFIELD RD JCTN A 388.O/S MOORFIELD HS, MOORFIELD ROAD, </small>

<b>East, North:</b> 235671, 89284

<b>Long, Lat:</b> -4.32623954677968, 50.6800939651158

Find all <a href='mapsearch.asp?siteID=&plantid=WWLAUN-14&east=&north=&radius=&postcode='>DPs</a>?

", "Click to view full-size:

<a href='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=64' target='_New'><img src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=64' border=0 width=75 height=56></a>

") );

map.addOverlay(createMarker(mpoint, icoP5, "sq", "P", "5", "<b>PCP: </b><a href='http://www.pfk.com/search?siteID=4474'>WWLAUNp13</a>

<b>Address:</b> <small>LAWHITTEN, , </small>

<b>East, North:</b> 234982, 82244

<b>Long, Lat:</b> -4.33284780911184, 50.6166353885335

Find all <a href='mapsearch.asp?siteID=&plantid=WWLAUN-13&east=&north=&radius=&postcode='>DPs</a>?

", "Click to view full-size:

<a href='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=67' target='_New'><img src='http://147.147.2.91:7001/pfk-deploy/thumbnail.do?photo=67' border=0 width=75 height=56></a>

") );

and so on ..as u can see the photoid is different for each row ..

im getting this with struts code , but then why is it displaying me just one photo and not all ..

do u think the problem may lie in createmarker() method?

one more thing i need to display only 4 photos at a time , can i restrict the bean:logic iterate in some way ..

all help is appreciated ..

well i think so...

Nidhi@BTa at 2007-7-14 18:56:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...