2009-11-03 50 views
4

UPDATE 1/31/10:由於此線程繼續獲得大量視圖...我很好奇它最近是否對任何人有所幫助?隨時留下意見/反饋,謝謝。Spring SimpleFormController - 在成功視圖中包含搜索表單


我有一個Spring表單,我希望重用搜索頁面以在搜索表單中包含結果。目前,我這樣做時,我得到加載成功視圖以下錯誤:

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

這裏是我的bean的配置:

<bean name="/search.html" class="myapp.web.AccountSearchController"> 
     <property name="sessionForm" value="true"/> 
    <property name="commandName" value="searchAccounts"/> 
    <property name="commandClass" value="myapp.service.AccountSearch"/> 
    <property name="validator"> 
     <bean class="myapp.service.AccountSearchValidator"/> 
    </property> 
    <property name="formView" value="accountSearch"/> 
    <property name="successView" value="accountSearchResults"/> 
</bean> 

下面是JSP的包含搜索表單的片段:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 

<form:form method="post" commandName="searchAccounts"> 

<table valign="top" cellspacing="0" cellpadding="0" width="500" border="0"> 

    <tr> 
     <td valign="top"> 

     <div class="border-title">Account Search</div> 

     <div id="navhome"> 
     <div class="border"> 
     <div id="sidebarhome"> 


     <table id="form"> 
      <tr> 
       <td colspan="2">Search by Account ID or Domain Name. If 
       values are provided for both, only accounts matching both values 
       will be returned.</td> 
      </tr> 

      <tr> 
       <td colspan="2">&nbsp;</td> 
      </tr> 

      <tr> 
       <td align="right" valign="top"><form:label path="accountId">Account ID</form:label>:</td> 
       <td><form:input path="accountId" size="30"/></td> 
      </tr> 
      <c:set var="accountIdErrors"><form:errors path="accountId"/></c:set> 
      <c:if test="${not empty accountIdErrors}"> 
      <tr> 
       <td>&nbsp;</td> 
       <td>${accountIdErrors}</td> 
      </tr> 
      </c:if> 
      <tr> 
       <td align="right" valign="top"><form:label path="domainName">Domain Name</form:label>:</td> 
       <td><form:input path="domainName" size="30"/></td> 
      </tr> 
      <c:set var="domainNameErrors"><form:errors path="domainName"/></c:set> 
      <c:if test="${not empty domainNameErrors}"> 
      <tr> 
       <td>&nbsp;</td> 
       <td>${domainNameErrors}</td> 
      </tr> 
      </c:if> 
      <tr> 
       <td colspan="2">&nbsp;</td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
       <td><input type="submit" name="submit" value="Search"> 
       </td> 
      </tr> 
     </table> 

     </div> 
     </div> 
     </div> 

     </td> 
    </tr> 
</table> 
</form:form> 

而且......這裏是我的表單控制器類(小於進口):

public class AccountSearchController extends SimpleFormController { 

    protected final Log logger = LogFactory.getLog(getClass()); 

    public ModelAndView onSubmit(Object command, BindException errors) throws ServletException { 
     String accountId = ((AccountSearch) command).getAccountId(); 
     String domainName = ((AccountSearch) command).getDomainName(); 

     logger.info("User provided search criteria...\n\tDomain Name: " + domainName + "\n\tAccountId: " + accountId); 

     //TODO do search 

     logger.info("returning from AccountSearch form view to " + getSuccessView()); 

     return new ModelAndView(getSuccessView()); 
    } 

    protected Object formBackingObject(HttpServletRequest request) throws ServletException { 
     AccountSearch accountSearch = new AccountSearch(); 
     return accountSearch; 
    } 
} 

在此先感謝您的幫助!

-AJ

UPDATE:

我這個移植到下面每一個答案註解控制器。這是新的/工作代碼:

@Controller 
@RequestMapping("/search.html") 
public class AccountSearchController { 

    // note: this method does not have to be called setupForm 
    @RequestMapping(method = RequestMethod.GET) 
    public String setupForm(Model model) { 
     AccountSearchCriteria accountSearchCriteria = new AccountSearchCriteria(); 
     model.addAttribute("accountSearchCriteria", accountSearchCriteria); 
     model.addAttribute("title", "Account Search"); 
     return "accountSearch"; 
    } 

    // note: this method does not have to be called onSubmit 
    @RequestMapping(method = RequestMethod.POST) 
    public String onSubmit(@ModelAttribute("accountSearchCriteria") AccountSearchCriteria accountSearchCriteria, BindingResult result, SessionStatus status, Model model) { 
     new AccountSearchValidator().validate(accountSearchCriteria, result); 
     if (result.hasErrors()) { 
      return "accountSearch"; 

     } else { 
      ArrayList<AccountSearchCriteria> accountSearchResults = new ArrayList<AccountSearchCriteria>(); 

      AccountSearchCriteria rec = new AccountSearchCriteria(); 
      rec.setDomainName("ajcoon.com"); 
      accountSearchResults.add(rec); 

      AccountSearchCriteria rec2 = new AccountSearchCriteria(); 
      rec2.setDomainName("ajcoon2.com"); 
      accountSearchResults.add(rec2); 

      //TODO do search 
      //ArrayList<HashMap<String,String>> accountSearchResults = new AccountSearchService().search(accountId,domainName); 

      if(accountSearchResults.size() < 1){ 
       result.rejectValue("domainName", "error.accountSearch.noMatchesFound", "No matching records were found."); 
       return "accountSearch"; 

      } else if(accountSearchResults.size() > 1){ 
       model.addAttribute("accountSearchResults", accountSearchResults); 
       return "accountSearch"; 

      } else { 
       status.setComplete(); 
       return "redirect:viewAccount?accountId="; 
       //return "redirect:viewAccount?accountId=" + accountSearchResults.get(0).getAccountId(); 
      } 
     } 
    } 
} 
+0

嗨,我試圖有一個搜索表單,結果顯示在我的JSP中使用標籤在同一個搜索表單頁上。關於使用這些標籤,你有沒有改變你的JSP?我可能會誤讀你的標題,因爲注意到你的successView與你的formView不一樣。我目前正在使用SimpleFormController並考慮使用帶註釋的方法。在網上找到任何例子都是一個挑戰。不知道它會如何禁止我,因爲我也使用referenceData方法。任何幫助,將不勝感激。謝謝! – Richard 2013-01-22 21:38:22

回答

1

嘗試使用(拋出異常,而不是......)

protected Object formBackingObject(HttpServletRequest request) 
          throws Exception { 
     AccountSearch accountSearch = new AccountSearch(); 
     System.out.println("inside formBackingObject"); 
     return accountSearch; 
} 

看起來不執行你的formBackingObject方法。通過上述更改重新運行代碼並查看日誌控制檯以查看該方法是否已執行。

-

您應該使用註釋而不是擴展控制器。 Spring 3.0將會棄用控制器層次結構。

+0

謝謝surajz ...我繼續並將其移植到註釋控制器並設法解決問題。我將發佈新的控制器進行編輯。 +1的建議! – 2009-11-03 05:03:27