【发布时间】:2023-03-27 07:32:01
【问题描述】:
我想通过休眠来保存 Village 对象,其中需要保存已经持久化的 District id。我在下拉列表中填充了区对象。我在 spring 2 编写了类似的工作,但在 spring 3 它不起作用。
在这里,如果我在 POST 中记录 village.getDistrict(),id 设置完美,因为我设置为下拉列表,但 District 对象的其他值为 null。
@SessionAttributes({"village"})
@Controller
public class VillageController{
@Autowired(required=true)
private AddressService addressService;
@RequestMapping(value="/cp/village.html", method = RequestMethod.GET)
public String setForm(ModelMap model) {
Village village = new Village();
village.setDistrict(new District());
model.addAttribute("village", village);
return "/cp/village";
}
@ModelAttribute("districtList")
public List<District> populateDistrictList() {
return addressService.getDistrictList();
}
@RequestMapping(value="/cp/village.html", method = RequestMethod.POST)
public String getForm(@ModelAttribute("village") Village village,
BindingResult result,
SessionStatus status) {
log.debug("============================="+village.getDistrict());
addressService.saveVillage(village);
status.setComplete();
return "redirect:/cp/village.html123";
}
}
在 JSP 中:
<form:form commandName ="village" action="village.html" >
<div>
<label><fmt:message key="location.district"/></label>
<form:select path="district.id">
<form:options items="${districtList}" itemValue="id" itemLabel="districtName"/>
</form:select>
</div>
<div>
<label><fmt:message key="location.village"/></label>
<form:input path = "villageName" />
</div>
<div>
<label><fmt:message key="prompt.remarks"/></label>
<form:textarea path = "remarks" rows="2" cols="50"/>
</div>
<div class = "button-area">
<input type = "submit" value="Save" class="submit-button" />
<input type = "button" value="Cancel" onclick="window.location='commonComplaintList.html'" class="submit-button" />
</div>
<br/>
</form:form>
【问题讨论】: