您好我想知道如何创建动态下拉列表,在另一个下拉列表中选择一个值后得到改进。
例如,如果我有两个下拉列表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>
在控制器中,列表由以下内容填充:
model.addAttribute("customerNameList",
customerService.listAllCustomerNames());
model.addAttribute("customerCountryList",
customerService.listAllCustomerCountries());
感谢您的帮助!
/D
更新
好的,所以我现在使用 JavaScript 在CustomerName
选择 a 时提交页面,以便为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>
这是控制器的一部分:
@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 页面中的提交按钮时保存交付?