Spring MVC下拉列表

Spring MVC Dropdown List

本文关键字:下拉列表 MVC Spring      更新时间:2023-09-26

我想要在多个下拉列表中,但从数据库中查询。例如,当我选择国家/地区"USA"时,我想要一个下拉列表城市,而当我选择一个城市"New York"时,我们想要的下拉列表是地区。有没有用ajax或者用springMVC框架的jquery来做到这一点?我很感激你的回答。非常感谢。

这类事情没有"开箱即用"的解决方案。您可以使用SpringMVC的元素来实现这一点,但它将主要是您自己的自定义代码。

Controller:
@RequestMapping("/cities/{country}.json")
public @ResponseBody getCities(@PathVariable String country) {
    --return a List<City> or List<String or whatever-- 
}
Javascript
$.get('/context/controller/cities/USA.json', function(response) {
    for(var i = 0, length = response.length; i < length; i++) {
        --do something with the city--
    }
});

编辑:就确保城市值不同而言,我会在服务器端上这样做

干杯,标记

您可以将mvc控制器中的方法映射到特定的url。例如:

@RequestMapping(value = "/some/url/here", method = RequestMethod.GET)
public [your response type here] getCitiesList{
}

当您从下拉列表中选择某个内容时,将所选内容附加到url中作为请求参数,然后可以在mvc应用程序中使用该参数来查询数据库中要在下一个下拉列表中填充的城市。

对每个列表重复此过程。

对一个模糊问题的回答有点模糊,但我希望它能有所帮助:)