Java Spring boot:按国家管理地区,按地区管理城市
我的项目包括一个管理公社的模块,或者有必要根据城市绘制地图。为此,我有三个表(国家、州和城市),我想根据国家选择显示地区,根据地区显示城市。现在,我有一个方法,可以根据国家选择返回json格式的地区列表Java Spring boot:按国家管理地区,按地区管理城市,java,json,spring-boot,Java,Json,Spring Boot,我的项目包括一个管理公社的模块,或者有必要根据城市绘制地图。为此,我有三个表(国家、州和城市),我想根据国家选择显示地区,根据地区显示城市。现在,我有一个方法,可以根据国家选择返回json格式的地区列表 @GetMapping("/state") @ResponseBody public List<Regions> getState(final HttpServletRequest request) { String valeur = null ; List<R
@GetMapping("/state")
@ResponseBody
public List<Regions> getState(final HttpServletRequest request) {
String valeur = null ;
List<Regions> listRegion = null;
if(request.getParameter("country") != null) {
valeur = request.getParameter("country");
log.info("valeur"+valeur);
listRegion = regionService.findByPaysPaysid(countryService.findByPaysid(Integer.parseInt(valeur)));
for (Regions regions : listRegion) {
log.info("name : "+regions.getRegionname()+"id"+regions.getRegionid()+"status"+regions.getRegionstatus());
}
}
log.warn("listof region "+listRegion);
return listRegion;
}
@GetMapping(“/state”)
@应答器
公共列表getState(最终HttpServletRequest请求){
字符串valeur=null;
List listRegion=null;
如果(request.getParameter(“country”)!=null){
valeur=request.getParameter(“国家”);
日志信息(“valeur”+valeur);
listRegion=regionService.findByPaysPaysid(countryService.findByPaysid(Integer.parseInt(valeur));
对于(区域:listRegion){
log.info(“名称:”+regions.getRegionname()+“id”+regions.getRegionid()+“状态”+regions.getRegionstatus());
}
}
日志警告(“区域列表”+区域列表);
返回列表区域;
}
还有我的javascript块
<script type="text/javascript">
$(document).ready(function(){
$("#country").on("change", function(){
var pays_id = $("#country").val();
var ddlState = $('#state_id');
ddlState.empty();
ddlState.append($("<option></option>").val('').html('Please wait ...'));
console.log(pays_id);
$.ajax({
url: "http://localhost:8082/district/state",
method: "GET",
dataType: 'json',
data: {country : pays_id},
success: function (d) {
console.log(d);
ddlState.empty();
ddlState.append($("<option></option>").val('').html('Select State'));
$.each(d, function (i, states) {
ddlState.append($("<option></option>").val(states.regionid).html(states.regionname));
});
},
error: function (e) {
console.log(e);
alert('Error!'+e);
}
});
});
});
</script>
$(文档).ready(函数(){
美元(“#国家”)。关于(“变化”,职能(){
var pays_id=$(“#国家”).val();
变量ddlState=$(“#state_id”);
ddlState.empty();
ddlState.append($(“”).val(“”).html('请稍候…);
console.log(pays_id);
$.ajax({
url:“http://localhost:8082/district/state",
方法:“获取”,
数据类型:“json”,
数据:{country:pays_id},
成功:功能(d){
控制台日志(d);
ddlState.empty();
ddlState.append($(“”).val(“”).html('Select State');
$。每个(d,功能(i,状态){
ddlState.append($(“”).val(states.regionid).html(states.regionname));
});
},
错误:函数(e){
控制台日志(e);
警报('Error!'+e);
}
});
});
});
我的表格:
<p class="error-message" th:if="${#fields.hasGlobalErrors()}"
th:each="error : ${#fields.errors('global')}" th:text="${error}">Validation
error</p>
<div class="input-field col s12 m6">
<label for="role" th:text="#{district.country.text}"></label>
<select name="country" id="country">
<option value="" disabled selected th:text="#{country.selectpays.text}"></option>
<option th:each="pays : ${listpays}"
th:value="${pays.paysid}"
th:utext="${pays.paysnom}"
></option>
</select>
</div>
<div class="input-field col s12 m6" >
<select name="state" id="state_id">
<option value="" disabled selected th:text="#{state.selectstate.text}"></option>
</select>
<label for="state" th:text="#{district.state.text}"></label>
</div>
<div class="input-field col s12 m6">
<label for="city" th:text="#{district.city.text}"></label>
<select name="city" id="city">
<option value="" disabled selected th:text="#{city.selectcity.text}"></option>
</select>
</div>
<div class="input-field col s12 m6">
<label for="arrondname" th:text="#{district.arrondname.text}" ></label>
<input type="text" th:field="*{arrondname}" id="arrondname" required/>
<p th:if="${#fields.hasErrors('arrondname')}"
th:errors="*{arrondname}">Validation error</p>
</div>
<div class="input-field col s12">
<button type="submit" th:text="#{district.create.text}" class="btn btn-medium btn-extented pink darken-2" >submit</button>
</div>
</form>
验证
错误
验证错误
提交
请帮助我解决错误您面临的错误是什么?我无法显示与状态对应的select中的状态。这是我的问题