在我的控制器中,我写了如下填写国家/地区列表和州名单:
@ModelAttribute("countries")
public List<Country> initializeCountries() {
List <Country> countries = countryService.findAllCountrys();
return countries;
}
@ModelAttribute(&#34;状态&#34) public List initializeStates(){
List <State> states = stateService.findAllStates();
return states;
}
在我的jsp中,我已经成功填充了国家和州:
<form:select path="country" id="country">
<form:option value="">Select Country</form:option>
<form:options items="${countries}" itemValue="countryCode" itemLabel
= "countryName" />
</form:select>
<form:errors path="country" cssClass="error"/>
<form:select path="state" id="state">
<form:option value="">Select Country</form:option>
<form:options items="${states}" itemValue="stateCode"
itemLabel="stateName" />
</form:select>
<form:errors path="state" cssClass="error"/>
但我需要根据国家的选择来填充州。
答案 0 :(得分:0)
您可以使用Ajax和JavaScript / JQuery。举一个例子,这可能对你有帮助
$("#country").change(function() {
var countryId = $(this).val();
$.ajax({
async: true,
type: 'GET',
url: '/fetchStates',
data: {
countryId : countryId,
},
error: function() {
alert("Error");
},
success: function(stateDetails) {
// call a function to populate state details
}
});
});