【问题标题】:How can I use s:property in JavaScript function如何在 JavaScript 函数中使用 s:property
【发布时间】:2013-11-25 15:43:51
【问题描述】:

在我的 struts2 应用程序中,我有一个 JavaScript 函数来执行取消操作。此方法位于标头中,仅对我的返回 URL 执行 window.location。由于我们从 struts 2.3.8 版本的 2.0.11 版本进行了迁移,它不再有效。当我查看生成的 HTML 代码时,我们看到无法解释标记 s:property,因为 URL 为空。我看不出什么不起作用。

在我的 IDE 中:

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

Firebug 的结果:

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

在 JSP 文件中:

<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>

【问题讨论】:

  • 也可以直接使用参数化的url标签。

标签: java javascript jsp struts2


【解决方案1】:

&lt;s:property value="#urlBack"/&gt; -- 在urlBack 之前是否需要“#”。我们所需要的只是带有public String getUrlBack() 方法的动作类中的urlBack 属性。并且变量应该在 Action 返回成功/结果之前被初始化。

【讨论】:

    【解决方案2】:

    首先,您需要创建一个变量urlBack,然后将其用于呈现 URL。例如

     <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>
    

    还有

    • 自 Struts 2.1.3 起,includeParams 默认为 none
    • id 属性被 var 替换
    • javascript: 是 URL 协议,不适用于 JS 函数,请参阅 this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-20
      相关资源
      最近更新 更多