views:

86

answers:

1

Hi,

I'm new to Struts2 but have been struggling with a problem for 2 days now.

Basically, I want to set up an <s:param> tag within an <s:url> with the content of a div tag somewhere else in the page.

<div id="div1">My value</div>
   <s:url action="myAction" var="url">
   <s:param name="param1">...content of div1...</s:param>
</s:url>

When I tried to embed some javascript call within <s:param> tags, javascript calls are not evaluated.

My question is : Is there a way to bind a javascript call to an <s:param> tag whenever it is evaluated ?

Thanks a lot

A: 

Unfortunately it's not possible with the <s:param> tag because this tag is rendered before the page is served to the user (i.e. it has to populate the link before it makes it to the browser where Javascript can take over).

What you're trying to do would be possible with a load handler though. Below is a jQuery example of how you could accomplish it:

$(document).ready(function(){
    var theDivContents = $('#div-id').text();
    var newLinkHref= $('#link-id').attr('href') + "&divContents=" + theDivContents;

    $('#link-id').attr('href', newLinkHref);
});

Another option if you're using Struts to populate the <div> you want to use as a link param would be to capture that in a variable before you render the link and then use it directly in the <s:param> tag:

<div id="div1">${yourValue}</div>
<s:url action="myAction" var="url">
   <s:param name="param1">${yourValue}</s:param>
</s:url>
Pat