您好,我想知道如何创建在另一个下拉列表中选择一个值后得到优化的动态下拉列表。
例如,如果我有两个下拉列表 Customer Name
和 Customer 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/