内部对象ID保存在Spring 3中 [英] Inner object id save in Spring 3
问题描述
我想通过休眠方式保存乡村对象,需要保存已保存的区号.我在下拉列表中填充了区域对象.我在春季2编写了类似的代码,但在春季3上不起作用.
I want to save Village object through hibernate where already persisted District id need to be saved. I populated district object in dropdown. I coded similar work in spring 2, but in spring 3 it doesn't work.
在这里,如果我在POST中记录village.getDistrict(),则将id设置为我设置为下拉列表时的完美值,但是District对象的其他值为null.
here, if I log village.getDistrict() in POST, id is set perfectly as I set to dropdown but other value of district object is 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>
推荐答案
您没有在District对象中设置任何值,因此它为空.
You do not set any value in the District object, so it is empty.
如果您期望该区域具有来自List<District> populateDistrictList()
的对象的值,那么我不得不说:它不能立即使用.
If you expected that the district has the values of objects from List<District> populateDistrictList()
, then I have to say: it does not work out of the box.
One way to do it, would be implementing a Converter, that converts a string (the id) to the District object, by loading it from the database.
这篇关于内部对象ID保存在Spring 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!