【问题标题】:Pass variable from JSP to javascript file将变量从 JSP 传递到 javascript 文件
【发布时间】:2014-09-05 01:27:22
【问题描述】:

我有一个 int 变量,它从 java 类传递到 jsp 文件中。我现在想将这个 int 值从 jsp 传递给我拥有的 javascript 文件。我坚持想出一个解决方案。我尝试了以下方法,但仍然无法正常工作:

javascript:

jQuery(document).ready(function() {
    jQuery('div.awsmp-hero-featureSwap').each(function() {
        jQuery(this).find('div.hero-featureSwap').each(function() {
            var width = 0;
            var height = 0;
            var count = 1;
            var perpage = "${products_per_page}"; //  ***** this is the value I want from the jsp*****
            var totalcarousel = jQuery('.hero').length;
            jQuery(this).find('div').each(function() {
                width += jQuery(this).width();
                count = count + 1;
                if (jQuery(this).height() > height) {
                    height = jQuery(this).height();
                }
            });

            jQuery(this).width(((width / count) * perpage + 1) + (perpage * 12) + 60 + 'px');
            jQuery(this).height(height + 'px');
        });
    });

JSP:

    <c:set var="products_per_page" value="3" />
    <c:if test="${not null model.listSize}">
        <c:set var="products_per_page" value="${model.listSize}" />
    </c:if>

我只想将“products_per_page”值传递给 javascript。应该很简单……

【问题讨论】:

  • 该 Javascript 文件/sn-p 是否正在被 JSP 引擎解析?您可以检查源并确保实际设置了该值。
  • 如果 javascript 文件是外部文件(未嵌入页面中),则 JSP 引擎不会对其进行解析。同时,如果变量没有定义,即使嵌入到页面中也不会插入。

标签: java javascript jquery jsp


【解决方案1】:

您可以将 &lt;c:set&gt; 块作为其值放入 HTML 元素中,然后使用 Javascript 或 jQuery 轻松获取该值

示例:

<input id="ppp" type="text" value="<c:set var='products_per_page' value='3' />"/>

js:

 var products_per_page = document.getElementById("ppp").value;

【讨论】:

  • 有什么办法可以使用占位符,例如您的输入?
  • 在页面上放置一个文本输入元素只是为了从 Java 中访问一个值?好像有点多。
  • 我想他也想显示他的&lt;c:set&gt; 块。所以我的解决方案出来了。如果有想法,您总是可以提供更好的答案。
  • @user3777012 你的&lt;c:set&gt; 块不需要显示在你的jsp页面上吗?
  • OP 想要在 Javascript sn-p 中使用 products_per_page 值,方法是将该值放入 Javascript 的变量中。我的想法是调用 JSP 变量的方式不正确,或者 JSP 没有解析文件(正确吗?),或者在脚本输出后发生分配。我不太了解 JSP,无法提供答案,但看到 OP 的评论,在页面内使用 input 并不是他/她更愿意采用的方式。
【解决方案2】:

你可以使用这个,不是很推荐..但是有效.. 像这样将值硬编码到 html 文件中:

<body> 
.....  
var x = <%= some_value  %>; 
.....  
</body>

【讨论】:

    【解决方案3】:

    我通过以下方式将请求参数传递给我的 js 中的 onclick 处理程序:

    <s:submit name="kurierButton" onclick="openKurierWindow('%{#attr.OUTBOUND_KURIER_URL}');" value="%{#attr.OUTBOUND_KURIER}"/>
    

    【讨论】:

      【解决方案4】:

      试试这个方法->

      servlet_file.java

      HttpSession session = request.getSession();
      session.setAttribute("idList", idList);
      

      .jsp 文件

      <%
      HttpSession session1 = request.getSession();
      ArrayList<Integer> idList = (ArrayList<Integer>) session1.getAttribute("idList");
      %>
      

      .js 文件

      alert("${idList}");
      

      【讨论】:

        猜你喜欢
        • 2013-07-17
        • 2017-08-30
        • 2021-06-20
        • 2013-06-10
        • 2011-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多