spring mvc:@RequestParam与@ModelAttribute异同

关于spring mvc中的两个注解:@RequestParam、@ModelAttribute区别,原先并没有特别注意,直到最近找别人开发的一个小模块的bug时,才有意识的比较了两者的区别。

1、@RequestParam,@RequestParam("xx") 表示在前端传递过来的参数中必须有个参数名称为“xx”(可以使用require=false避免必须)

2、@ModelAttribute,@ModelAttribute("xx") 表示将前端传递过来的参数按照名称注入到对应的对象中,“xx”只是表示放到ModelMap中的key值

时间: 2024-08-10 19:11:25

spring mvc:@RequestParam与@ModelAttribute异同的相关文章

Spring MVC @RequestParam

案例来说明 1 @RequestMapping("user/add") 2 public String add(@RequestParam("name") String name, 3 @RequestParam("age") int age){ 4 System.out.println(name+","+age); 5 return "hello"; 6 } 测试1 当我们请求路径为:http://loc

[Spring MVC] - @ModelAttribute使用

在Spring MVC里,@ModelAttribute通常使用在Controller方法的参数注解中,用于解释model entity,但同时,也可以放在方法注解里. 如果把@ModelAttribute放在方法的注解上时,代表的是:该Controller的所有方法在调用前,先执行此@ModelAttribute方法. 比如我们有一个Controller:TestController @Controller @RequestMapping(value="test") public c

【Spring MVC】 - @ModelAttribute使用

@ModelAttribute一个具有如下三个作用: ①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑 定流程,而且自动暴露为模型数据用于视图页面展示时使用: ②暴露表单引用对象为模型数据:放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用 对象,如注册时需要选择的所在城市等,而且在执行功能处理方法(@RequestMapping 注解的方法)之前,自动添加 到模型对象中,用于视图页面展示时使用: ③暴露@Request

Spring MVC @ModelAttribute详解

被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立成一个控制器也是如此. 1.注释void返回值的方法 1 @Controller 2 public class HelloModelController { 3 4 @ModelAttribute 5 public void populateModel(@RequestParam String ab

Spring MVC之@ModelAttribute注解的使用

@ModelAttribute注解的作用 Spring MVC提供了几种将数据添加到模型的方式:使用ModelMap/Model类型的方法参数.方法体内创建ModelAndView实例,如下代码片段所示: @RequestMapping(value = "/login.htm", method = RequestMethod.GET) public String doLogin(ModelMap modelMap) { BaseMember mockMember = new BaseM

四、Spring MVC的RequestParam注解

前面的章节,有提到可以通过@PathVariable注解来映射restful风格的url中的值到方法中去,本章就看看如果不使用restful风格的url来怎么进行参数的传递. RequestParam就是来实现参数传递的,能够把用户的输入绑定到后台的方法上面.它有三个主要的属性: value:定义参数的名称 required:定义参数是否是必须的,默认是true defaultValue:定义参数的默认值 下面结合具体的示例代码来看一下如何使用: 1.下面的这段代码定义了三个参数,一个name,

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable--转载

原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotation.RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be appl

Spring MVC 后台获取前台form参数值(requestparam)

Spring MVC 获取前台参数值得java代码: @RequestMapping(value = "/loginCheck") public String loginCheck( @RequestParam("username") String username, @RequestParam("password") String password) { UserMess user = new UserMess(); user.setUsern

spring mvc表单form值自动传到javabean-注解@ModelAttribute

直接通过Form Bean进行表单可以简化表单提交的处理,特别是对于复杂表单,过于简单的表单就不建议了,因为毕竟需要额外创建一个Form Bean. 前段时间项目中有一个比较复杂的表单,有多层次而且涉及数组,如果不采用Form Bean的处理方式会比较麻烦,但多层而且是数组,之前一直没有找到如何通过Form Bean方式进行处理,但相信Spring MVC应该可以做到,因为Spring的强大之处就是IOC嘛,所以在网上查阅资料,并不断尝试后终于解决,现在不管是多么复杂的表单均可以通过Form B