且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不使用@RequestParam而不是@ModelAttribute

更新时间:2023-12-04 11:22:40

实际上,至少有四种方式更像(@ RequestParam,@ ModelAttribute,未注释的Pojo参数和直接来自请求对象)

Actually, there are more like at least four ways (@RequestParam, @ModelAttribute, an un-annotated Pojo parameter, and straight from the Request object)

使用单个结构化参数的主要原因是,如果结构化数据具有多个字段.比使用多个@RequestParam参数更方便,并且您可以同时验证所有参数.使用@ModelAttributes,您可以轻松地从会话,数据库或flashAttributes中检索单个对象.

The main reason to use a single structured parameter is if you have structured data with several fields. It's more convenient than using several @RequestParam params, and you can validate all at the same time. With @ModelAttributes you can retrieve a single object from session, database, or flashAttributes easily.

您可以将现有实体或Pojo与@ModelAttribute一起使用,而不必创建自定义的表单支持对象.

You can use existing Entities or Pojos with @ModelAttribute, you don't need to create a custom Form Backing Object necessarily.

但是,如果您只有一个或两个参数,则@RequestParam可以.

But ya, if you just have one or two params, then @RequestParam is fine.