3

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

2
  • Is that Javascript file/snippet being parsed by the JSP engine? You might check the source and make sure the value is actually being set. Commented Jul 15, 2014 at 0:43
  • If the javascript file is external (not embedded in the page) then it won't be parsed by the JSP engine. At the same time, if the variable isn't defined, it won't be inserted even if it is embedded in the page. Commented Jul 15, 2014 at 19:13

4 Answers 4

4

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;
Sign up to request clarification or add additional context in comments.

5 Comments

is there any way around using a placeholder such as the input that you have?
Put a text input element on the page simply to access a value from Java? That seems a bit much.
i had in my mind that he would also want to display his <c:set> block. so my solution came out to this. you can always provide a better answer if have one in mind though.
@user3777012 is your <c:set> block need not to be shown on your jsp page?
The OP wants to use the products_per_page value in a Javascript snippet by putting that value into the Javascript's variable. My thoughts are the way the JSP variable is being called is incorrect, or the file isn't being parsed (correctly?) by JSP, or the assignment is happening after the script is outputted. I don't know JSP well enough to provide an answer, but seeing the OP's comment, using an input within the page is not the way he/she would prefer to go.
0

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>

Comments

0

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}"/>

Comments

0

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}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.