Spring MVC:如何在Spring验证错误中保留模型属性



我在Stack Overflow上四处搜索,但找不到查询的解决方案。我有一个控制器功能,可以在GET请求上添加多个模型属性

  @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没有被拾取?有没有办法也保留其他定义的模型属性?

似乎您需要在spring3.1中添加的flash属性。请看一下示例/解释:

http://viralpatel.net/blogs/spring-mvc-flash-attribute-example/

get和post是不同的请求。您在post请求中得到的只是来自表单的内容,因此只有"deletedAttributes"模型属性和JSP中的<input>字段。

您需要像在get方法中那样,再次明确地放置searchResults模型属性。

正如M.Deinum所建议的,如果一个控制器中的所有方法都将使用一个或多个属性,则可以使用@ModelAttribute注释方法将其自动放入模型中。

您也可以在Spring的Petclinic示例中使用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 ofSessionAttributes`。

相关内容

最新更新