2013-01-03 24 views
3

我在一個jsp頁面上有兩個表單。第一種形式不使用modelAttribute,第二種使用modelAttribute。問題是,如果我發佈第一個不使用modelAttribute的表單,它會聲明一個錯誤,我沒有綁定modelAttribute。多個表單在一個jsp中

我在互聯網上搜索尋找解決方案,但我找不到一個有用的。

changeAddress.jsp

<form method="post"> 
    <input type="hidden" name="id" value="0" /> 
    <input type="submit" name="exist" value="send to this address" /> 
</form> 
<form:form method="post" modelAttribute="addressForm"> 
    <form:input path="street" />  
    <input type="submit" name="add" value="send to this address" /> 
</form:form> 

OrderController.java

@RequestMapping(value="changeAddress",method = RequestMethod.GET) 
public ModelAndView showChangAddress(Model model) 
{ 
    model.addAttribute("addressForm", new AddressForm()); 
    return new ModelAndView("body.changeaddress"); 
} 

@RequestMapping(value="changeAddress", params="add", method = RequestMethod.POST) 
public ModelAndView addChangAddress(@ModelAttribute("addressForm") @Valid AddressForm af, BindingResult result, Model model) 
{ 
    System.out.println("a"); 
    return new ModelAndView("body.changeaddress"); 
} 

@RequestMapping(value="changeAddress", params="exist", method = RequestMethod.POST) 
public ModelAndView processChangAddress(@RequestParam(value="id") String id, Model model) 
{ 
    System.out.println("b"); 
    return new ModelAndView("body.changeaddress"); 
} 

大大appriciated的幫助:)

+0

您正在使用哪個版本的Spring? – Lion

+0

3.1.3發佈版本 – Skyverian

+0

您可以嘗試使用document.forms [0] .submit僅提交第一個表單。 – prashanth

回答

3

彈簧形式的taglib documentation有關<form>標籤:

該標記呈現HTML「表單」標記並公開綁定路徑以通過內部標記進行綁定。它將命令對象放在PageContext中,以便命令對象可以被內部標籤訪問。

我認爲你不需要任何東西從你的第一種形式的春天<form>標籤。因此,您可以使用簡單的html表單代替:

<form method="post" action="..."> 
    <input type="hidden" name="id" value="0" /> 
    <input type="submit" name="exist" value="send to this address" /> 
<form> 
+0

我已經這樣做了,但它使我陷入了同樣的錯誤 – Skyverian

相關問題