1

In my struts2 Application I have a JavaScript function to do the cancel. this method is in the header and only do a window.location to my return URL. Since we did a migration from version 2.0.11 of struts 2.3.8 release, it no longer works. When I look at the generated HTML code we see that fails to interpret the tag s:property because the URL is empty. I do not see what does not work.

in my IDE :

function cancel() {
        if (!isModified || (isModified && askConfirmCancel())) {
            window.location.replace('<s:property value="#urlBack"/>');
        }
    }

result with Firebug :

function cancel() {
        if (!isModified || (isModified && askConfirmCancel())) {
            window.location.replace('');
        }
    }

in JSP file :

<tr>
    <td height="6%"align="center">
      <s:submit cssClass="button" key="common.initDelegate.label" align="center" theme="simple"/>
      <s:url id="urlBack" action="myAction" includeParams="none" escapeAmp="false">
        <s:param name="period.periodId" value="%{period.periodId}"></s:param>
      </s:url>
      <input type="button" onclick="javascript:cancel()" value="<s:text name="common.button.cancel"/>"/>
    </td>
  </tr>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Ousmane MBINTE
  • 664
  • 4
  • 15
  • 37

2 Answers2

0

<s:property value="#urlBack"/> -- do we need "#" before urlBack. All we need is urlBack property in action class with public String getUrlBack() method. And the variable should get initialized before Action returns the success/result.

ms03
  • 101
  • 4
0

First you need to create a variable urlBack then use it for render URL. For example

 <s:url var="urlBack" action="myAction" escapeAmp="false">
   <s:param name="period.periodId" value="%{period.periodId}"></s:param>
 </s:url>
 <input type="button" onclick="cancel();" value="<s:text name="common.button.cancel"/>"/>
 <script type="text/javascript">
   function cancel() {
     if (!isModified || (isModified && askConfirmCancel())) {
        window.location.replace('<s:property value="#urlBack"/>');
     }
   }
 </script>

also

  • includeParams by default is none since Struts 2.1.3.
  • id attribute is replaced by var
  • javascript: is a protocol for URLs not for JS functions, see this.
Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176