从struts标签调用javascript函数

时间:2016-02-26 06:40:36

标签: javascript jsp struts2

您好我有一个JSP页面,它是从struts动作调用并设置会话变量但是当我执行时,javascript函数没有被调用if if else条件。变量的值为true,因为文本正在打印但函数未被调用。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<script type="text/javascript">

function test(){
    alert('inside test');
}

function test2(){
    alert('inside 222');
}
</script>

<div id="body_content">
  <div id="content_cotainer">


<s:if test="%{ #session.testVariable}">     
                                qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
                                <script type="text/javascript"> test2()</script>
                        </s:if>
                        <s:else><script type="text/javascript"> test()</script>ddddddddddddddddd</s:else>
    </div>                  
</div>      

2 个答案:

答案 0 :(得分:0)

当DOM可用时,将函数调用放在正文的末尾

<script>
(function(testVariable) {
  if (testVariable)
    test2();
  else
    test();
})(<s:property value="%{#session.testVariable}"/>);
</script>
</body>
</html>

答案 1 :(得分:0)

将您的第一个<script>块放入<head>