java - 选择后的 Spring MVC 细化下拉列表

标签 java javascript html spring hibernate

您好,我想知道如何创建在另一个下拉列表中选择一个值后得到优化的动态下拉列表。

例如,如果我有两个下拉列表 Customer NameCustomer Country我选择了某个 Customer Name ,我只想看到相应的客户国家。

使用查询:

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

我可以获得相应的国家,但如何传递输入值 customerName当它在自己的下拉列表中被选中时?

这是我用于 customerName 的代码下拉列表:

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

在 Controller 中,列表由以下内容填充:

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

感谢您的帮助!

/D

更新

好的,所以我现在已经使用 JavaScript 在 CustomerName 时提交了页面。选择以便为 CustomerCountry 加载经过优化的列表列表下拉框。

这是 jsp 的一部分,包括脚本:

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

这是 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";

    }

我遇到的问题是 post 方法在第一个 if 循环后不会停止并检查错误,然后在我在 CustomerName 中选择一个值后立即尝试保存交付。下拉框。

有谁知道我如何才能让它只继续检查错误(第二个 if 循环)并在我点击 jsp 页面中的提交按钮时保存交付?

最佳答案

您必须向选择框添加一个 JavaScript 事件监听器,它将执行以下操作之一:

  • 提交表单(在修改其 action 属性后),以便 Controller 重新显示同一页面但包含不同的国家/地区
  • 使用客户名称向 Controller 发送 AJAX 请求,并使用响应中包含的一组国家/地区动态填充选择框。例如,响应可以是国家/地区的 JSON 数组,或包含选择框新选项的 HTML 片段。

关于java - 选择后的 Spring MVC 细化下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11098445/

相关文章:

java - XML-RPC PHP Java

java - GridBagLayout 的问题

javascript - 遍历父容器的子容器

javascript - Flexnav 菜单按钮仅使用其内容的宽度

javascript - 用ajax替换div内容

java - 为什么 IntelliJ 建议用 Lambda 表达式替换匿名 Runnable?

javascript - z-index 的问题

javascript - 图像未显示在移相器 3 中

css - 使用css reset时如何在段落之间添加空格?

java - 方面永远不会被调用