Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 863875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:22:54+00:00 2026-05-15T09:22:54+00:00

Ok so perhaps someone can help me with a problem I’m trying to solve.

  • 0

Ok so perhaps someone can help me with a problem I’m trying to solve. Essentially I have a JSP page which gets a list of Country objects (from the method referenceData() from a Spring Portlet SimpleFormController, not entirely relevant but just mentioning in case it is). Each Country object has a Set of province objects and each province and country have a name field:

public class Country  {
    private String name;
    private Set<Province> provinces;

    //Getters and setters
}

public class Province {
    private String name;

    //Getters and setters
}

Now I have two drop down menus in my JSP for countries and provinces and I want to filter the provinces by country. I’ve been following this tutorial/guide to make a chain select in JavaScript.

Now I need a dynamic way to create the JavaScript array from my content. And before anyone mentions AJAX this is out of the question since our project uses portlets and we’d like to stay away from using frameworks like DWR or creating a servlet. Here is the JavaScript/JSP I have so far but it is not populating the Array with anything:

var countries = new Array();
<c:forEach items="${countryList}" var="country" varStatus="status">
    countries[status.index] = new Array();
    countries[status.index]['country'] = ${country.name};
    countries[status.index]['provinces'] =
    [
        <c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus">
            '${province.name}'
            <c:if test="${!provinceStatus.last}">
              ,
            </c:if>
        </c:forEach>
    ];
</c:forEach>

Does anyone know how to create an JavaScript array in JSP in the case above or what the ‘best-practice’ would be considered in this case? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:22:55+00:00Added an answer on May 15, 2026 at 9:22 am
    var countries = new Array();
    <c:forEach items="${countryList}" var="country" varStatus="status"> 
        countryDetails = new Object();
        countryDetails.country = ${country.name}; 
        var provinces = new Array();
    
            <c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus"> 
               provinces.push(${province.name});
            </c:forEach> 
        countryDetails.provinces = provinces;
        countries.push(countryDetails);
    </c:forEach> 
    

    now what you have is something like this in javascript

    var countries = [
      {country:"USA",
      provinces: [
        "Ohio",
        "New York",
        "California"
      ]},
      {country:"Canada",
      provinces: [
        "Ontario",
        "Northern Territory",
        "Sascetchewan"
      ]},
    ]
    

    The other option would be to make your output look like the javascript I posted.

    var countries = [
    <c:forEach items="${countryList}" var="country" varStatus="status">  
        {country: '${country.name}',
        provinces : [ 
            <c:forEach items="${country.provinces}" var="province" varStatus="provinceStatus">  
               '${province.name}'
               <c:if test="${!provinceStatus.last}">    
                 ,    
               </c:if>   
            </c:forEach>  
        ]}
        <c:if test="${!status.last}">    
          ,    
        </c:if>  
        </c:forEach>  
    ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.