2014-01-29 19 views
0

我開始學習春天MVC3,和我想展示一個簡單的「搜索表單」在這裏我的JSP文件無論是BindingResult還是bean名稱'command'的普通目標對象。春天例外

<form:form method="post" action="addGeo.html" > 
    <table> 
     <tr> 
      <td>   
       <input type="submit" value="<spring:message code="label.addzone"/>"/> 
      </td> 
     </tr> 
    </table>  
</form:form> 

<form:form method="post" action="maingeo.html" > 
    <table> 
     <tr> 
      <td> 
       <form:label path="codeZone"><spring:message code="label.area"/></form:label> 
      </td> 
      <td>  
       <form:input path="codeZone" /> 
      </td>   
      <td> 
       <form:label path="type"><spring:message code="label.type"/></form:label> 
      </td> 
      <td> 
       <form:input path="type" /> 
      </td>   
      <td> 
       <form:label path="codePostal"><spring:message code="label.departement"/></form:label> 
      </td> 
      <td> 
       <form:input path="codePostal" /> 
      </td> 
      <td>   
       <input type="submit" value="<spring:message code="label.searchArea"/>"/> 
      </td> 
     </tr> 
    </table> 
</form:form> 

,在這裏我的控制器:

@Controller 
public class RefGeoController { 

@Autowired 
private RefGeoService refgeoService;   

    @RequestMapping("/maingeo") 
    public String goSearchArea() { 
     return "maingeo"; 
    } 
} 

,當我去這個URL頁面,我有此異常:java.lang.IllegalStateException:無論BindingResult也不是爲bean名稱「命令」可以作爲請求屬性平原目標對象。我認爲我忘記了我的表單或可能在我的控制器中,但我不知道在哪裏。另外當我不想發送特定的模型到我的jsp視圖時,我應該把什麼放在我的方法參數?

回答

1

你需要寫一個類,包含您的形式領域 - 這種類被稱爲命令對象/類。

然後在你的控制器的方法,那就是負責提供表單頁面,你需要創建這個命令對象的實例,把它的型號,然後讓視圖渲染。即在模型中使用的命令對象的名稱,必須符合「命令」屬性<form:form command="myCommand">標籤的名稱。如果您沒有此屬性,則默認名稱爲command

@Controller public class RefGeoController { 

    @Autowired private RefGeoService refgeoService;   

    @RequestMapping("/maingeo") 
    public ModelAndView goSearchArea() { 
     return new ModelAndView("maingeo", "searchCommand", new SearchCommand()); 
    } 

    //only to prevent your next question: How to recive the committed form 
    @RequestMapping("/maingeo.html", method = RequestMethod.POST) 
    public ModelAndView handleSearch(SearchCommand searchCommand) { 
     ... implement the search stuff 
    } 
} 


<form:form method="post" action="maingeo.html" command="searchCommand"> 
+0

感謝快速回復,我有這個類和所有attribut比賽fiels形式,用於在模型中的命令對象的名字放在命令名也,但總會有這樣execption!似乎集中這一行:<形式:標籤路徑= 「codeZone」><彈簧:消息代碼= 「label.area」/>和主要原因:既不-bindingresult ... – user3248251

+0

@ user3248251:添加的System.out。 println到'goSearchArea()'方法,並檢查它是否真的被調用。 – Ralph

+0

是的,我的方法被調用。 – user3248251

相關問題