user3777012
user3777012

Reputation: 93

Pass variable from JSP to javascript file

I have an int variable in that gets passed into a jsp file from a java class. I now want to pass this int value from the jsp to a javascript file I have. I am stuck with a coming up with a solution. I have tried the following but still does not work:

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>

I just want to pass the the "products_per_page" value to the javascript. should be very simple...

Upvotes: 3

Views: 15336

Answers (4)

Yash P Shah
Yash P Shah

Reputation: 809

Try doing it this way->

servlet_file.java

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

.jsp file

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

.js file

alert("${idList}");

Upvotes: 0

Rafał
Rafał

Reputation: 590

I passed the request argument to my onclick handler in js the following way:

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

Upvotes: 0

Analyst
Analyst

Reputation: 945

You can use this, is not very recommendable.... but works.. Hard-coding the value into the html file like this:

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

Upvotes: 0

Yaje
Yaje

Reputation: 2831

you can put your <c:set> block into an HTML element as its value then get the value easily using Javascript or jQuery

example :

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

js:

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

Upvotes: 4

Related Questions