【问题标题】:Passing an action property to a JavaScript function将动作属性传递给 JavaScript 函数
【发布时间】:2014-03-24 12:45:05
【问题描述】:

当我尝试将 Struts2 <s:property> 值作为名为 handleChange 的 JavaScript 函数的参数时,我的 JSP 页面出现问题,以便我可以保存该集合的新值,这是我的表单:

<form name="monF" method="get">
    <display:table name="menaces" pagesize="1" requestURI="loadLesCauses.action" decorator="configuration.MyDecorator" class="table table-striped table-bordered table-hover" id="dataTables-example">
        <display:column property="idmenace" title="id menace" />
        <display:column property="lienmenace" title="lien de la menace" media="html" />
    </display:table>
    <s:iterator value="causesmenace" status="rowStatus">
        <h3 class="intro-text text-center">
            <strong><li><s:property value="libellecause" /></li></strong>
        </h3>
        <s:hidden id="idcause" value="idcause" />
        <h2 class="intro-text">impact sur metier</h2>
        <s:select id="impact" name="impact_sur_metier" list="impactsurmetier" value="defaultValue_im" onchange="handleChange_impact(this.value,%{#rowStatus.index});" cssClass="form-control"></s:select>
        <br>
        <h2 class="intro-text">niveau de probabilité</h2>
        <s:select id="probabilite" name="niveau_de_probabilité" list="niveaudeprobabilite" value="defaultValue_np" onchange="handleChange_probabilite('<s:property value=" idcause "/>',this.value,%{#rowStatus.index});" cssClass="form-control"></s:select>
    </s:iterator>
    <button type="submit" onclick="radarRisk()" title="radarRisk">radarRisk</button>
</form>

这是我的 JS 块:

<script type="text/javascript">
    var tableName = document.getElementByClassName("menaceList");

    function handleChange_probabilite(id, value, nbr) {
        JSSauvegarderCause.sauvegarderCauseProbabilite(value, nbr, afficherValidation);
        alert(id + value + nbr);
    }
    }
</script>

错误信息:

févr. 22, 2014 9:27:03 PM org.apache.catalina.core.ApplicationDispatcher invoke
Grave: "Servlet.service()" pour la servlet jsp a lancé une exception
org.apache.jasper.JasperException: /vues/menacesView.jsp(129,70) Symbole égal (equal)                 attendu

如果我在onchange 事件中删除&lt;s:property value="idcause"/&gt;,代码可以正常工作。

【问题讨论】:

    标签: java javascript jsp properties struts2


    【解决方案1】:

    Struts2 不允许像&lt;s:select ... &lt;s:property .../&gt;/&gt; 这样的嵌套标签。您可以在标签属性中使用 OGNL 表达式。当你用过

    <s:hidden id="idcause" value="idcause" />
    

    value属性计算为OGNL表达式,需要为实际值提供getter

    public String getIdcause(){return idcause;} 
    

    然后使用javascript函数从隐藏字段中获取值

    onchange="handleChange_probabilite(document.getElementById('idcause').value,this.value,%{#rowStatus.index});"
    

    【讨论】:

    • 感谢您的重播,实际上页面上没有更多错误但 getElementById 没有获取属性的值,
    • 这是一个文档功能。您使用过类似的函数通过类名获取元素,因此您应该也熟悉此函数。
    • 在 s 中都没有类属性,加上每一行都有自己的 id,所以我应该动态传递参数
    • 是的,代码似乎不完整,所以我要关闭这个问题吗?
    • 为什么?我想说我试图添加一个类名,但是没有,如果我发布这个问题是为了有一些答案不要像那样关闭它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2017-02-07
    相关资源
    最近更新 更多