2014-10-06 25 views
0

我在Stack Overflow上搜索了一遍,但找不到解決方案。我有一個GET請求Spring MVC:如何在彈簧驗證錯誤中保留模型屬性

@RequestMapping(method = RequestMethod.GET, value = "/showdeletesearchqueryform") 
    public String showDeleteSearchQuery(final Model model) { 
    if (LOG.isDebugEnabled()) { 
     LOG.debug("Fetching all the search query results."); 
    } 
    ImmutableList<ArtQueryResults> results = this.searchQueriesService 
     .getSearchQueries(APPNAME); 

    // Adding model attribute # 1 
    model.addAttribute("searchResults", results); 
    if (LOG.isDebugEnabled()) { 
     LOG.debug("\"searchResults\" model attribute has been intialized from " 
      + results); 
    } 
    ArtDeleteQueryRequest request = new ArtDeleteQueryRequest(); 
    request.setAppName(APPNAME); 
    if (LOG.isDebugEnabled()) { 
     LOG.debug("Model attribute initialized = " + request); 
    } 
    // Adding model attribute # 2 
    model.addAttribute("deletedAttributes", request); 
    return "deletesearchqueries"; 
    } 

我的JSP

<div class="column-group"> 
      <form:form method="POST" action="${pageContext.request.contextPath}/arttestresults/showdeletesearchqueryform" modelAttribute="deletedAttributes"> 
      <form:errors path="*" cssClass="alert alert-danger column lg-units-5 units-2" element="div"/> 
      <form:hidden path="appName" id="appNameId" htmlEscape="true"/> 
      <div class = "units-1 column lg-units-12"> 
     <!-- Hidden Key for app name. --> 


     <form:select path="idsToBeDeleted" id="IdsToBeDeletedSelectId"> 
      <c:forEach items="${searchResults}" var="searchResult" varStatus="loop"> 
     <form:option label="${searchResult.searchQuery}" value="${searchResult.id}" /> 
     </c:forEach> 
     </form:select> 
     </div> 
     <div class="units-1 column lg-units-12"> 
      <%-- This is a hack that make sure that form is submitted on a click. Not sure why form is not being submitted. --%> 
      <button class="button" type="submit" onclick="javascript:$('form').submit();">Delete Selected Queries</button> 
     </div> 
      </form:form> 

我控制器POST功能

@RequestMapping(method = RequestMethod.POST, value = "/showdeletesearchqueryform") 
    public String deleteSearchQueries(
     Model model, 
     @ModelAttribute(value = "deletedAttributes") @Valid final ArtDeleteQueryRequest request, 
     final BindingResult result) { 
    if (result.hasErrors()) { 
     LOG.warn("There are " + result.getErrorCount() + " validation errors."); 
     return "deletesearchqueries"; 
    } else { 
     if (LOG.isDebugEnabled()) { 
     LOG.debug("The ids to be deleted are " + request.getIdsToBeDeleted()); 
     } 
     this.searchQueriesService.deleteSearchQueriesById(
      ImmutableList.copyOf(request.getIdsToBeDeleted())); 
     return "redirect:/arttestresults/showdeletesearchqueryform"; 
    } 
    } 

,增加了多模型控制器的功能屬性,如果驗證失敗,模型屬性searchResults在返回錯誤情況下的視圖時沒有被拾取?有沒有辦法保留其他定義的模型屬性?

+0

爲什麼要這樣呢?您爲獲取請求創建的模型僅適用於獲取請求。如果您需要它們可用於每個請求,請使用'@ SessionAttributes'或使用'@ ModelAttribute'註釋的方法。 – 2014-10-06 05:53:31

+0

你沒有使用bean驗證? – 2014-10-06 09:29:16

+0

Amrola - 我正在使用bean驗證。 – Kartik 2014-10-06 16:24:41

回答

0

GET和職位不同的要求。你在帖子請求中得到的只是表單中的內容,因此只有"deletedAttributes"模型屬性,並且只有JSP中的<input>字段。

您需要像在get方法中那樣明確地再次輸入searchResults模型屬性。

正如M. Deinum建議的那樣,如果控制器中的所有方法都使用一個或多個屬性,則可以使用註釋方法自動將它們(它們)放入模型中。

您還可以在Spring的Petclinic example中使用SessionAttributes model attributes, that is attributes that are stored in session and not in request. But it is hard to have them properly cleaned from session if user do not post the form but go into another part of the application. You have an example of usage of SessionAttributes`。