2014-01-22 32 views
0

我想要做的似乎很簡單,但每個配置我用給我下面的錯誤時:如何避免「無論BindingResult也不是爲bean名稱平原目標對象‘命令’可以作爲請求屬性」搜索

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute 

我想傳遞一個字符串給控制器搜索和Offices一個List了在新的頁面中發現的顯示屬性。我無法查看我的瀏覽器中的officeSearch頁面,我收到上述錯誤。

officeSearch.jsp(應該只是一個簡單的形式傳遞一個被搜索的字符串):

<div class="header-divider2"> 
    <h4>Search</h4> 
</div> 
<div id="content" class="center"> 
    <div class="well center square borderless"> 
     <h1>Location Search</h1> 
     <div class="visible-md visible-lg well well-search"> 
      <form:form method="POST" action="officeSearchResults"> 
      <div class="form-group"> 
      <form:input path="searchCriteria" type="text" id="searchCriteria" placeholder="Find a location..." /> 
      </div> 
      <input type="submit" class="btn btn-default" value="Search" /> 
     </form:form> 
     </div><!-- visible-md --> 
    </div><!-- well center square --> 
</div><!-- center --> 

下面是應顯示在搜索頁面的控制方法:

@RequestMapping(value = "/officeSearch", method=RequestMethod.GET) 
public String showOfficesSearch() { 
    return "officeSearch"; 
} 

這裏提交搜索後應顯示的jsp(明顯存在殘留):

<div class="header-divider2"> 
    <h4>Search Results</h4> 
</div> 
<div id="content" class="center"> 
    <div class="well center square borderless"> 
     <h1>Location Search RESULTS</h1> 
    </div><!-- well center square --> 
</div><!-- center --> 

這裏是一個的意思顯示前一頁的控制器方法:

@RequestMapping(value = "/officeSearchResults", method = RequestMethod.POST) 
public ModelAndView search(@ModelAttribute("officeSearchResults") String searchCriteria) { 
    List<Office> offices = officeServiceImpl.search(searchCriteria); 

    return new ModelAndView("officeSearchResults", "command", offices); 
} 

在得到這方面的工作,請告知。謝謝。

回答

1

您的表單當前沒有綁定到模型屬性,因爲您尚未添加任何模型屬性。默認情況下,即。當你不指定modelAttributecommandName屬性(使用一個或另一個)

<form:form method="POST" action="officeSearchResults"> 

它將尋找名爲command結合,即一個模型屬性。通過path來解決input元素

所以只需添加一個名爲command一個模型屬性(或其他任何東西,並將它配置)

@RequestMapping(value = "/officeSearch", method=RequestMethod.GET) 
public String showOfficesSearch(Model model) { 
    model.addAttribute("command", new WhateverObjectYouWantToBindTo()); 
    return "officeSearch"; 
} 

或者,你可以建立你<form><input>元素的自己,而不是使用Spring的form標籤支持。

相關問題