0


In my controller class, I'm setting some attributes to the request as below.

for(ConsumerVO dep:quoteVO.getConsumerFamily().getDependents())
            {
                request.setAttribute("dependent_"+depCount+"_birthdateMM", dep.getBirthdateMM());
                request.setAttribute("dependent_"+depCount+"_birthdateDD", dep.getBirthdateDD());
                request.setAttribute("dependent_"+depCount+"_birthdateYYYY", dep.getBirthdateYYYY());
                depCount++;
            }

In the jsp, I'm trying to access it by setting the variable name dynamically. But itsn't working. No data is in ${depMM}, ${depDD} etc. I'm looking for something like ${${depMM}}. How can I access the request variables in a loop ?

    jQuery(function(){

        var dependent_data = [];
        for(j=1;j<16;j++){
            var depMM = "dependent_"+j+"_birthdateMM";  
            var depDD = "dependent_"+j+"_birthdateDD"; 
            var depYY = "dependent_"+j+"_birthdateYYYY"; 
            if (typeof '${depMM}' !== 'undefined' && '${depMM}' !=="") {
            dependent_data.push('${depMM}','${depDD}','${depYYYY}');
            }
        }

PS: When I did this, it printed the data.

var dependent1MM_data = '${dependent_1_birthdateMM}';
alert(dependent1MM_data);
2
  • Can you show how you are setting the variables in the JSP? Commented Jun 5, 2017 at 18:01
  • This variable is not directly used in jsp. Its passed to another jquery function which will show/create some divs based on this data. Commented Jun 5, 2017 at 18:13

1 Answer 1

1

You can't access variables like this. You are mixing JavaScript and JSP. The for loop will run in the client side once the page is loaded. You can't access the variables inside JavaScript. You can instead turn it into a jsp for loop and access the variables in that.

Here are some ways of doing it 1. Create a for loop (JSP for loop or JSTL each) and then access the request parameters and create an input hidden tag, access that in the JS. (OR) 2. you can write a JSON String in the JSP and access that in your JavaScript.

var myObject = <%= JSON output of dependedents %>; 

The reason why the following statement works is

var dependent1MM_data = '${dependent_1_birthdateMM}';
alert(dependent1MM_data);

When the JSP template is rendered the template engine sets the value in this variable. It's not tied with JS engine it's tied to your JSP rendering engine.

What I suggest is create a JSON string because if the data is too big then you will be creating more variables and input.hidden elements. So writing JSON will be a good solution

Note: I am not good in JSP or Spring. But I do have some experience in template languages. SO I am just pointing out the mistake here

Sign up to request clarification or add additional context in comments.

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.