Spring mvc - 使用@ModelAttribute的POST请求会导致不必要的SQL查询

时间:2018-05-14 18:39:16

标签: spring spring-mvc modelattribute

我有一个包含三个选择输入的表单,这些输入是从视图中的db添加的。一切都很好,用户可以选择每个选择。问题是,当我提交表单时,@ModelAttribute生成与GET请求相同的查询(当这三个选择被添加到视图中时),同样,POST后的select字段为空。 / p>

@GetMapping("/road-assistance")
public String viewPage(Model model, Locale locale) {

    model.addAttribute("roadAssistanceDto", roadAssistanceService.createRoadAssistanceDto(locale, null));
    return "admin/roadassistance/view";
}



@PostMapping("/road-assistance")
public String createRoadAssistance(Model model, Locale locale,
                                   final @Valid @ModelAttribute("roadAssistanceDto") RoadAssistanceDto roadAssistanceDto,
                                   BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return "redirect:/";
    }

    return "admin/roadassistance/view";
}

为什么@PostMapping生成相同的查询,同时,我在视图/模型中没有信息?

视图 -

<div class="row card-pagging">
        <div class="col-sm-2 mt-3 offset-sm-2">
            <label data-th-text="#{control.panel.roadassistance.country}" 
 class="control-label"
                   for="country">Country</label>
            <select id="country" data-th-field="*{country}" class="form-control">
                <option data-th-each="countryList : ${roadAssistanceDto.countryList}"
                        data-th-value="${countryList.id}"
                        data-th-text="${countryList.countryI18n[#locale].name}"></option>
            </select>
        </div>
        <div class="col-sm-3 mt-3">
            <label data-th-text="#{control.panel.roadassistance.region}" class="control-label"
                   for="region">Region</label>
            <select data-th-field="*{region}" id="region" class="form-control">
                <option value='' data-th-text="#{control.panel.roadassistance.chooseregion}"></option>
                <option data-th-each="reg : ${roadAssistanceDto.regionList}" data-th-value="${reg.id}"
                        data-th-text="${reg.regionI18n[#locale].name}"></option>
            </select>
        </div>
        <div class="col-sm-3 mt-3">
            <label data-th-text="#{control.panel.roadassistance.city}" class="control-label"
                   for="region">City</label>
            <select data-th-field="*{city}" id="city" class="form-control">
                <option value="" data-th-text="#{control.panel.roadassistance.chooseregion}"></option>
                <option data-th-each="city : ${roadAssistanceDto.cityList}" data-th-value="${city.id}"
                        data-th-text="${city.cityI18n[#locale].name}"></option>
            </select>
        </div>
    </div>

RoadAssistanceDto -

public class RoadAssistanceDto implements Serializable {

private Long id;

private Country country;

private Region region;

private City city;

private List<Country> countryList;

private List<Region> regionList;

private List<City> cityList;

/* GETTERS AND SETTERS /*

}
嗯......我想我注意到了这个问题......当我绑定百里香的田地时,有一些工作......但无论如何,为什么会产生这些问题呢?

0 个答案:

没有答案