2014-10-06 30 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`。