2014-11-08 88 views
0

我想在我的類中獲取url參數。我的基於Spring MVC的應用程序。使用spring獲取URL參數mvc

在呼籲網址:http://localhost:8080/mywebapp/dir/register.do?id=26提示錯誤400

@RequestMapping(value = "/register") 
public ModelAndView finalPage(@PathVariable("id") Long id) throws NumberFormatException, Exception { 
    // code... 
} 

任何人都可以解決我的問題。

+0

您認爲'@ PathVariable'如何工作,爲什麼? – 2014-11-08 17:57:48

+0

使用'@ QueryParam'而不是'@ PathVariable' – Rohan 2014-11-08 18:00:35

+0

@SotiriosDelimanolis我不確定,有沒有其他方法? – Bharat 2014-11-08 18:01:20

回答

2

你說

我想在我的課

獲取URL參數,但使用的是@PathVariable

表明,方法參數應該綁定到一個URI模板變量

您的映射中沒有名爲id的uri模板變量。

你真正需要的是一個@RequestParamdocumentation解釋瞭如何使用它。

+0

謝謝@SotiriosDelimanolis,我使用了「@RequestParam」,它的工作非常完美。但如果我沒有在URL中傳遞id,該怎麼辦?在某些情況下,我沒有通過url傳遞參數 – Bharat 2014-11-08 18:12:26

+1

@Bharat如果你看看我鏈接的javadoc,你可以設置'required'爲'false'。 – 2014-11-08 18:18:17