2012-10-31 41 views
0

我在教自己Spring Form標記,並且遇到了可能是我無法解決的簡單錯誤。我收到以下錯誤,當我在瀏覽器中啓動這個程序:彈簧窗體標記錯誤(綁定結果)

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

我試過最,在谷歌搜索無果來到了修正。有人能發現我出錯的地方嗎?以下是相關的組件。非常感謝。

控制器:

@Controller 
@RequestMapping("/registration") 
public class LoginController { 

    @RequestMapping(method = RequestMethod.GET) 
    public String setupForm(ModelMap model) { 
     Registration registration = new Registration(); 
     model.addAttribute("registration", registration); 
     return "index"; 

    } 


    @RequestMapping(method = RequestMethod.POST) 
    public String onSubmit(@ModelAttribute("registration") Registration registration, Map model) { 
     return "success"; 
    } 
} 

JSP(/index.jsp):

 <form:form commandName="index"> 
     <table border="0" cellspacing="12"> 
      <tr> 
       <td> 
        <form:input path="email"/> 
       </td> 
      </tr> 

      <tr> 
       <td> 
        <form:password path="password"/> 
       </td> 
      </tr> 

      <tr> 
       <td> 
        <input type="submit" value="Submit"/> 
       </td> 
      </tr> 
     </table> 
    </form:form> 

Command對象(Registration.java):

public class Registration { 
    private String password; 
    private String email; 
    // getters,setters 

回答

0

在您的索引頁正確以下並嘗試

<form:form commandName="index"><form:form commandName="registration">

你也能做到這樣,如果上述方法無效

<form:form modelAttribute="registration" commandName="registration">

感謝

+0

謝謝Heggi !!等待期結束後,我會接受你的回答! – EdgeCase

0

這被看作是錯誤的,因爲當你提交你的表格具有與@ModelAttribute註釋關聯的綁定結果。

試着改變你的代碼如下:

@RequestMapping(method = RequestMethod.POST) 
    public String onSubmit(@ModelAttribute("registration") Registration registration, BindingResult result, Map model){ 
      return "success"; 
     } 

還要注意的是綁定結果對象應立即模型屬性後跟隨。

如果您使用兩個@ModelAttributes,那麼每個人都應該有它自己的綁定結果對象。

請參閱春源指南,瞭解所有相關的文檔

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html

1

面對同樣的問題,幾天就回來,我從命中和跟蹤瞭解什麼是,索引頁是靜態頁面,沒有處理情況在呈現之前。如果我想要在索引頁面中使用表單模型綁定,我應該有一個控制器的處理方法,它將創建一個註冊對象並將其放置在ModelAndView中,然後再創建index.jsp。

在控制器中添加一個方法,如下面試試

@RequestMapping(method = RequestMethod.GET, value="/") 
public ModelAndView initiate(){ 
ModelAndView objModView = new ModelAndView("/index.jsp"); 
objModView.addObject("registration",new Registration()); 
return objModView; 
}