2
我有一個MVC應用程序創建新的辦公室,而不是在使用編輯表單時更新它們。請幫我理解爲什麼會發生這種情況。Spring MVC,嘗試編輯但創建新對象
用於填充搜索結果的搜索方法:
@RequestMapping(value = "/searchResults", method = RequestMethod.POST)
public ModelAndView search(@RequestParam String searchCriteria, HttpServletRequest request) {
List<Office> offices = officeServiceImpl.search(searchCriteria);
return new ModelAndView("searchResults", "offices", offices);
}
這裏是鏈接編輯表單看起來像搜索結果頁上:
<a href="${pageContext.request.contextPath}/app/office/${office.getId()}/edit" class="btn btn-success">Edit Office</a>
這裏是控制器的編輯GET方法用現有的辦公室填寫表格:
@RequestMapping(value = "/{officeId}/edit", method = RequestMethod.GET)
@Transactional(noRollbackFor=NoResultException.class)
public ModelAndView initUpdateOfficeForm(
@PathVariable("officeId") Long officeId, Model model) {
Office office = officeServiceImpl.find(officeId);
//prepareEditFormModelAndView(office) just converts some objects to strings for typeahead form population
return prepareEditFormModelAndView(office);
}
這裏是編輯POST me的ThOD:
@RequestMapping(value = "/{officeId}/edit", method = RequestMethod.POST)
public ModelAndView processUpdateOfficeForm(@ModelAttribute("office") @Valid Office office,
BindingResult result, SessionStatus status) {
if (! "united states of america".equals(office.getFolderStrings().toLowerCase())) {
//This portion of code converts the typeahead strings to objects
result = tryCountries(office, result);
result = tryDepartments(office, result);
result = tryEmployees(office, result);
}
if (result.hasErrors()) {
return prepareEditFormModelAndView(office);
} else {
officeServiceImpl.save(office);
status.setComplete();
return new ModelAndView("editResult", "office", office);
}
}
officeServiceImpl調用officeRepositoryImpl方法保存它看起來像:
@Override
public Office save(Office office) {
em.merge(office);
em.flush();
return office;
}
感謝
編輯:添加prepareEditFormModelAndView(辦公室),此方法試圖從關聯對象建立字符串:
@Transactional(noRollbackFor={NoResultException.class, IndexOutOfBoundsException.class})
private ModelAndView prepareEditFormModelAndView(Office office) {
String departmentStrings = "";
String employeeStrings = "";
List<OOM> officeOOMs = new ArrayList<OOM>();
StringBuilder sb = new StringBuilder();
try {
officeOOMs = oomServiceImpl.getOOMsForCurrentOffice(office.getId());
} catch (NoResultException e) {
officeOOMs = null;
}
for (OOM o : officeOOMs) {
try {
Employee tempEmployee = employeeServiceImpl.find(o
.getEmployeeId());
sb.append(tempEmployee.getDisplayName() + ", ");
} catch (NoResultException e) {
sb.append("Not found in system");
}
}
employeeStrings = sb.toString();
if ((! "".equals(office.getDepartmentStringsOnForm())) && office.getDepartmentStringsOnForm() != null) {
departmentStrings = office.getDepartmentStringsOnForm();
}
String folderStrings = "";
try {
folderStrings = kmlFolderServiceImpl.getInternationalOfficeString(office.getId());
LOGGER.info("Folder Strings: " + folderStrings);
} catch (NoResultException e) {
folderStrings = "";
LOGGER.info("Folder Strings: " + "no result");
}
boolean isInternational = office.isInternational();
ModelAndView result = new ModelAndView("editOfficeForm", "office", office);
result.addObject("departmentStrings", departmentStrings);
result.addObject("isInternational", isInternational);
result.addObject("folderStrings", folderStrings);
result.addObject("employeeStrings", employeeStrings);
return result;
}
prepareEditFormModelAndView做什麼? –
當你在郵局工作時,你也發送它的ID嗎? – geoand
@geoand,不,我沒有通過身份證。我通過了辦公室的模型屬性。 –