springMVC重定向传参

2 篇文章 0 订阅
1 篇文章 0 订阅

最近做公司项目,遇到一个问题,需要重定向到Controller还要传递参数,在网上找了资料,终于知道了,现在分享一下

<span style="white-space:pre">	</span>@RequestMapping(value="/deleteCategory")
	public String deleteCategory(String serviceClassId, RedirectAttributesModelMap redirectMap) throws Exception{
		
		TppecsUser user = new RequestHelper().getUserInfo();
		
//		redirectMap.put("code", "");
//		redirectMap.put("msg", "shibai");
		redirectMap.addFlashAttribute("code", "213");
		redirectMap.addAttribute("code1", "code1");
		
			
		serviceItemCategoryService.deleteProductCategory(serviceClassId);
		
		return "redirect:/serviceItemCategory/viewCategory.do";
	}
	

这里用的RedirectAttributesModelMap,其实RedirectAttributes 和RedirectAttributesModelMap是一样的

先说明一下ModelMap 、RedirectAttributes  、RedirectAttributesModelMap的用法

使用ModelMap其实重定向是可以传参的,用modelMap.put("code","999")或者modelMap.addAttribute("code","999");

<span style="white-space:pre">	</span>@RequestMapping("/deleteCategory")
	public String deleteCategory(Long spClassId,ModelMap modelMap) throws ApiException{
		
		TppecsUser user = new RequestHelper().getUserInfo();
		
			//客服系统
		try{
		if(spClassId!=null){
			int i = serviceProviderCategoryService.findSpClassId(spClassId);
			if(i<=0){
				serviceProviderCategoryService.delete(spClassId);
			}else{
				modelMap.addAttribute("code","999");
			}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "redirect:/serviceProviderCategory/viewCategory.do"; 
	}

使用ModelMap 后必须在viewCategory.do的Controller的中接收这个参数,才能使用

<span style="white-space:pre">	</span>@RequestMapping(value="/viewCategory")
	public String searchCategory(TppecsSearchSpCategory vo,String code,ModelMap modelMap ,HttpSession session ){	
		try {
			PagingResult<Map<String, Object>> dataPaging = new PagingResult<Map<String, Object>>();
			if(vo.getPageno() == null || vo.getPageno()==0){
				vo.setPageno(1);
			}
			log.debug(vo.getSp_Order()+"spOrder=======");
			log.debug(vo.getClass_Name()+"className=======");
			log.debug(vo.getSp_CalssNum()+"spClassNum=======");
			log.debug(vo.getParent_Id()+"parentId=======");
			dataPaging = serviceProviderCategoryService.querysearchAll(vo);
			modelMap.put("vo", vo);
			modelMap.addAttribute("code",code);
			modelMap.put("dataPaging",dataPaging );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "serviceprovider/category/categoryList";
	}

然后再把这个值put到ModelMap 中,在页面用el表达式可以取到值。

最好的方法还是用 RedirectAttributes  或RedirectAttributesModelMap,用 addFlashAttribute的方法把需要的重定向的值都用 addFlashAttribute方法,

这样不需要再目标Controller加接收参数,直接在重定向后的页面用el表达式就可以取值

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值