Question

I'm using Struts 2.3.16.

How do I push an action URL onto the value stack?

I've tried:

<s:push value="methodUrl">
  <s:url action="action" method="method" />
</s:push>

<s:push value="methodUrl">
  <s:url action="action" method="method" var="methodUrl" />
</s:push>

<s:push>
  <s:url action="action" method="method" var="methodUrl" />
</s:push>

All the above fail without logging any errors even though I have struts.devMode=true. Only the failure of the third option was truly expected since value is required for s:push.

Background: I need to pass the URL to a JavaScript function that is in a separate .js file from a select tag like the following:

<s:select name="actionAttribute" list="attributeList"
          listKey="key" listValue="value"
          onchange="ajaxFunction('%{methodUrl}')" />

I know that I can use '<s:url action="action" method="method" />' in the JS function if the function is defined in my .jsp file, but that doesn't work when the JavaScript is in a different file.

As I typed the above paragraph I realized that I can define a JavaScript function in my JSP that will pass the URL to the ajaxFunction.

So now my question is purely academic.

Is there a way to s:push an s:url onto the value stack?

Was it helpful?

Solution

If you use var attribute of the url tag it will put the value to the value stack.

<s:url action="action" method="method" var="methodUrl" />
<s:select name="actionAttribute" list="attributeList"
          listKey="key" listValue="value"
          onchange="ajaxFunction('%{#methodUrl}')" />

you can also use a function without parameter

<s:select name="actionAttribute" list="attributeList"
          listKey="key" listValue="value"
          onchange="ajaxFunction()" />
<script type="text/javascript">
  function ajaxFunction(){
    var methodUrl = '<s:property value="%{#methodUrl}"/>';
    // or even better
    var methodUrl2 = '<s:url action="action" method="method" var="methodUrl" />';
  }
</script>

ok, if you want to push a value into the value stack you should use this tag, but the value should be a reference to the object, that would convert to string url and passed as a parameter to JS function

<s:url action="action" method="method" var="methodUrl" />
<s:push value="%{#methodUrl}">
  <s:select name="actionAttribute" list="attributeList"
          listKey="key" listValue="value"
          onchange="ajaxFunction('%{top}')" />
  <script type="text/javascript">
    function ajaxFunction(url){
      alert(url);
    }
  </script>
</s:push>

OTHER TIPS

From the lack of positive responses, it appears that using s:push to push an s:url onto the value stack is not possible. Roman's responses are not quite what I was looking for.

Based on Roman's responses, and my own experiments, it is apparent that one must use the var attribute of s:url in order to get the URL on the value stack. In order to keep the URL from appearing on the page one must use JavaScript in the JSP, such as this based on Roman's second code block:

<script type="text/javascript">
  function noNeedToExecute(){
    var dummy = '<s:url action="action" method="method" var="methodUrl" />';
  }
</script>

Or, one must use CSS like this:

<style>
  .hidden {display: none;}
</style>
...
<span class=hidden>
  <s:url action="action" method="method" var="methodUrl" />
</span>

I was hoping for a pure Struts implementation, but it is apparent that I must live with disappointment. ;-)

I'm leaving the above to not hide the fact that I was totally wrong. I don't know what wrong move I made lead me to believe that <s:url ... var="varName" .../> would display the URL on the page which would then need to be hidden. I was wrong and I apologize for wasting Roman C 's time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top