javascript
requestparam的作用_Spring MVC:请解释@RequestParam和@ModelAttribute之间的区别
文檔
@ModelAttribute上的方法參數指示參數應該從模型中檢索。如果模型中不存在,則應首先實例化參數,然后將其添加到模型中。一旦出現在模型中,參數的字段應該從具有匹配名稱的所有請求參數填充。 WebDataBinder類將請求參數名稱(包括查詢字符串參數和表單字段)與名稱建模屬性字段相匹配。
@RequestParam將請求參數綁定到控制器中的方法參數。
免責聲明/澄清
我知道@ModelAttribute和@RequestParam是不一樣的東西,并不是相互排斥的,不執行相同的作用,并能同時使用,如this question - 的確,@RequestParam可以用于填充@ModelAttribute的字段。我的問題更多地針對他們內部工作之間的差異。
問:
是什么@ModelAttribute之間的差異,@RequestParam(在方法的參數,而不是方法使用)?具體做法是:
來源:待辦事項@RequestParam和@ModelAttribute具有 信息/人口,在URL即請求參數相同的源,其可以被供給作為一種形式/模型,這是POST版的要素是什么?
用法:用@RequestParam檢索到的變量是否被丟棄(除非傳入模型中),而使用@ModelAttribute檢索的變量是否被自動送入要返回的模型?
或者在非?;镜木幋a示例中,這兩個示例之間的真實工作區別是什么?
實施例1:@RequestParam:
// foo and bar are thrown away, and are just used (e.g.) to control flow?
@RequestMapping(method = RequestMethod.POST)
public String testFooBar(@RequestParam("foo") String foo,
@RequestParam("bar") String bar, ModelMap model) {
try {
doStuff(foo, bar);
}
// other code
}
實施例2:@ModelAttribute:
// FOOBAR CLASS
// Fields could of course be explicitly populated from parameters by @RequestParam
public class FooBar{
private String foo;
private String bar;
// plus set() and get() methods
}
// CONTROLLER
// Foo and Bar become part of the model to be returned for the next view?
@RequestMapping(method = RequestMethod.POST)
public String setupForm(@ModelAttribute("fooBar") FooBar foobar) {
String foo = fooBar.getFoo();
String bar = fooBar.getBar();
try {
doStuff(foo, bar);
}
// other code
}
我的當前理解:
@ModelAttribute和@RequestParam博個詢問請求參數的信息,但他們不同的方式使用這些信息:
@RequestParam只是填充獨立變量(當然這可能是一個@ModelAttribute類字段)。這些變量在控制器完成時將被丟棄,除非它們已被饋入模型中。
@ModelAttribute填充一個類的字段,然后填充模型的屬性將被傳遞回視圖
這是正確的嗎?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的requestparam的作用_Spring MVC:请解释@RequestParam和@ModelAttribute之间的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java观察者_Java中的观察者模式
- 下一篇: [云炬创业学笔记]第二章决定成为创业者测