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 8105861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:16:36+00:00 2026-06-06T00:16:36+00:00

Hi I am wondering how to create dynamic drop down lists that get refined

  • 0

Hi I am wondering how to create dynamic drop down lists that get refined after the selection of a value in another drop down list.

For example, if I have two drop down lists Customer Name and Customer Country and I select a certain Customer Name, I only want to see the corresponding Customer Countries.

Using a query:

public List<Customer> getAllCustomerCountries(customerName) {
    return this.sessionFactory
            .getCurrentSession()
            .createQuery(
                    "select distinct customerCountry from Customer where 
                     customerName=:name").setParameter("name", customerName)
                    .list();
}

I can get the corresponding Countries but how do I pass on the input value customerName when it is selected in its own drop down list?

here is the code I’m using for the customerName drop down list:

            <tr>
                <td>Customer Name</td>
                <td><form:select path="customerName">
                    <form:option value="" label="--- Select ---" />
                    <form:options items="${customerNameList}" />
                    </form:select>
                </td>
                <td><form:errors path="customerName" cssClass="error" /></td>
            </tr>

In the controller the lists are populated by:

    model.addAttribute("customerNameList",
            customerService.listAllCustomerNames());
    model.addAttribute("customerCountryList",
            customerService.listAllCustomerCountries());

Thank you for your help!

/D

Update

Ok, so I have now used JavaScript to submit the page when a CustomerName is chosen so that a refined list list is loaded for the CustomerCountry drop down box.

Here is part of the jsp including the script:

        <script>
            function repopulate(){  
                document.getElementById("hasId").value ="true";
                alert("You selected : " + document.getElementById("hasId").value);
                document.deliveryForm.submit();

            }
        </script>

<!-- ... -->        

                <tr>
                    <td><form:hidden id="hasId" path="hasCustomerName" value="false"/></td>
                </tr>

            <tr>
                <td>Customer Name</td>
                <td><form:select path="customerName" onChange="repopulate()">
                    <form:option value="" label="--- Select ---" />
                    <form:options items="${customerNameList}" />
                    </form:select>
                </td>
                <td><form:errors path="customerName" cssClass="error" /></td>
            </tr>

And here is part of the controller:

@RequestMapping(value = "/add", method = RequestMethod.GET)
    public String getDelivery(ModelMap model) {

        DeliveryDto deliveryDto = new DeliveryDto();

        model.addAttribute("deliveryDtoAttribute", deliveryDto)

        model.addAttribute("customerNameList",
                customerService.listAllCustomerNames());
        model.addAttribute("customerCountryList", null);


        return "new-delivery";
    }



    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String postDelivery(
            @ModelAttribute("deliveryDtoAttribute") @Valid DeliveryDto deliveryDto,
            BindingResult result, ModelMap model) {

        if (deliveryDto.getHasCustomerName() == "true"){

            model.addAttribute("deliveryDtoAttribute", deliveryDto); 

            model.addAttribute("customerNameList",
                    customerService.listAllCustomerNames());
            model.addAttribute("customerCountryList",
                    customerService.listAllCustomerCountries(deliveryDto.getCustomerName()));

            return "new-delivery";
        }




        if (result.hasErrors()) {
            model.addAttribute("deliveryDtoAttribute", deliveryDto); 
            model.addAttribute("customerTargetIdList",
                    customerService.listAllCustomerTargetIds());
            model.addAttribute("customerNameList",
                    customerService.listAllCustomerNames());
            model.addAttribute("customerCountryList",
                    customerService.listAllCustomerCountries(deliveryDto.getCustomerName()));

        }


        Delivery delivery = new Delivery();

        /* A bunch of setters and to set the values in the delivery object that will be saved */

        deliveryService.createDelivery(delivery);


        return "redirect:/home";

    }

The problem I’m having is that the post method doesn’t stop after the first if-loop and checks for errors and then tries to save the delivery as soon as i select a value in the CustomerName drop down box.

Does anyone know how I can make it so that it only continues on to check for errors (2nd if-loop) and save the delivery when I hit the submit button in the jsp page?

  • 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-06-06T00:16:38+00:00Added an answer on June 6, 2026 at 12:16 am

    You must add a JavaScript event listener to the select box, which will do one of the following:

    • submit the form (after modifying its action attribute), in order for the controller to redisplay the same page but with a different set of countries
    • send an AJAX request to the controller with the customer name, and dynamically populate the select box with the set of countries contained in the response. The response could be a JSON array of countries, or an HTML snippet containing the new options of the select box, for example.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there are some example coding to create dynamic tables with
I am wondering what the best way to create two columns of dynamic lists
I was wondering how to: create a droplet that saves to a relative filepath.
Hi there i am wondering if i create a webapp that uses Spring AMQP.
Just wondering if it is possible to dynamically create a list of strings in
Wondering if it's possible to create a dynamic linq query using linq to objects.
I want to create a dynamic DropDownList in ASP.NET, but after postback the DropDownList
When a user registers I am wondering how to create them a dynamic directory
I was wondering how to create a strong key for AES-256 from the contents
I am wondering how to create my own SOA style messaging system, similar to

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.