2014-03-31 58 views
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; 
} 
+0

prepareEditFormModelAndView做什麼? –

+0

當你在郵局工作時,你也發送它的ID嗎? – geoand

+0

@geoand,不,我沒有通過身份證。我通過了辦公室的模型屬性。 –

回答

2

我在添加以前的評論在這裏,爲了更好的澄清。根據OP,以下問題修復了以下問題:

當ID不在表單中時,當模型返回時,沒有ID設置爲使持久性提供者認爲它是新實體的實體。 因此,最明顯的解決方案是在保存操作中發佈實體的ID(可能使用隱藏字段)。

另一種解決方案是嘗試基於某個業務密鑰 加載數據庫中的實體以查看實體是否爲新實體。

相關問題