2013-12-11 44 views
3

我有我的控制器類似Spring MVC的POST請求

@RequestMapping("/property") 
@ResponseBody 
public String property(@RequestBody UserDto userDto) { 

    System.out.println(userDto.getUsername()); 
    System.out.println(userDto.getPassword()); 

    return "Hello"; 
} 

但它給了我一個錯誤,當我用後

<form method="post" action="http://localhost:8080/home/property"> 

    <input name="username"/> 
    <input name="password"/> 
    <input type="submit"/> 
</form> 
在我的HTML

。我哪裏錯了。

回答

3

當您發佈表單時,應該使用@ModelAttribute註釋。

你的代碼更改爲:

@RequestMapping("/property") 
@ResponseBody 
public String property(@ModelAttribute("userDto") UserDto userDto) { 
    System.out.println(userDto.getUsername()); 
    System.out.println(userDto.getPassword()); 
    return "Hello"; 
} 

而且你的HTML/JSP可以是:

<form method="post" name="userDto" action="http://localhost:8080/home/property"> 
    <input name="username"/> 
    <input name="password"/> 
    <input type="submit"/> 
</form> 
2

請求的身體是你逝去的東西就像一個JSON或XML對象(或生數據,如byte [])發送到HTTP POST。在發佈表單數據時,會爲您處理和分析這些數據。最簡單的方法是使用MVC表單:帶有命令對象的表單代碼,然後您將只接收一個命令對象,其中包含映射到該對象的所有表單條目。

0

一種方法是什麼傑文建議, 也可以修改你的春天來接受它像,

UserDto userDto; 
@RequestMapping("/property") 
@ResponseBody 
public String property(@RequestParam("username") userDto.username, @RequestParam("password") userDto.password) { 

    System.out.println(userDto.getUsername()); 
    System.out.println(userDto.getPassword()); 

    return "Hello"; 
} 

ofcourse如果你已經暴露在課堂上,這不是一個優雅的實踐屬性。

0

如果你得到http錯誤500?然後嘗試使用

@RequestMapping(value = "/property", method = RequestMethod.POST) 

如果還有其他錯誤,請指定。

1

請求映射的默認方法是GET。必須使用RequestMapping指定url方法。

@RequestMapping(value="/property",method=RequestMethod.POST)