Spring mvc 参数传递



一,常用注解

@ModelAttribute

一个类中所有方法执行前都会执行此注解下的方法


1.@RequestMapping

  @RequestMapping(value = "/add", method = RequestMethod.GET)

 

2.@SessionAttributes(names={"userstatess","userpassword"})

 // 1.用在类头;

// 2.当检查到model中保存有对应的属性值会自动保存一份到session对象中

 

3. @PathVariable    

  URL地址动态传参


// http://ip:port/springmvc/user/delete/43
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String deleteUserRest(@PathVariable("id") int userId, Model model) {
		System.out.println("删除项ID :" + userId);
		model.addAttribute("msg", "删除成功");
		return "user/message";
	}

4. @ModelAttribute("user")


2.请求重定向

 对于请求重定向可以分为:1.带参数  2.不带参数

 

 带参数;

(1)手动拼接法

 @RequestMapping("/one")
  public String doOne(RedirectAttributes redirectAttr) {
     return "redirect:/two?message=hellobody";
  }
  @RequestMapping("/two")
  public String doTwo(String message) {
     System.out.println("message:" + message);
     return "success";
  }
http://localhost:8080/SpringMVC-1-1/two?message=hellobody
注:参数值为中文需手动编解码

(2)自动拼接法

@RequestMapping("/one")
  public String doOne(RedirectAttributes redirectAttr) {
     redirectAttr.addAttribute("message", "hellobody");
     return "redirect:/two";
  }
  public String doOne(Model model) {
     model.addAttribute("message", "hellobody");
     return "redirect:/two";
  }


  @RequestMapping("/two")
  public String doTwo(String message) {
     System.out.println("message:" + message);
     return "success";
  }
http://localhost:8080/SpringMVC-1-1/two?message=hellobody
使用RedirectAttributes和Model的addAttribute方法系统默认采用参数传值

(3)参数保存session中隐藏传递


@RequestMapping("/one")
  public String doOne(RedirectAttributes redirectAttr) {
     redirectAttr.addFlashAttribute("message", "hellobody");
     return "redirect:/two";
  }

  @RequestMapping("/two")
  public String doTwo(@ModelAttribute("message") String message) {
     System.out.println("message:" + message);
     return "success";
  }

http://localhost:8080/SpringMVC-1-1/two


 

 注意:1.使用RedirectAttributesaddAttribute方法传递参数会跟随在URL后面,如上代码即为http://localhost:8080/SpringMVC-1-1/two?message=hellobody

           2.使用addFlashAttribute不会跟随在URL后面,会把该参数值暂时保存于session,待重定向url获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。你会发现redirect后的jsp页面中message只会出现一次,刷新后message再也不会出现了,这验证了上面说的,message被访问后就会从session中移除。对于重复提交可以使用此来完成。

            3.带参数重定向,用到RedirectAttributes ;springmvc配置文件需要添加<mvc:annotation-driven />

3. Controller层代码中把addFlashAttribute中的数据提取出来 

    方法一:利用httpServletRequest 

 public String test2(HttpServletRequest request) { 
     Map<String,?>map = RequestContextUtils.getInputFlashMap(request); 
     System.out.println(map.get("message").toString()); 
      return "/test/hello"; 
 } 

   方法二:利用Spring提供的标签@ModelAttribute 

public String test2(@ModelAttribute("message") String str) { 
   System.out.println(str); 
   return "/test/hello"; 
} 


  以上两种方法是在后台Controller层获取值的方法,如果是在前台页面的话,就会比较简单,直接利用el表达式就可以取到数据 














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值