Spring MVC controller怎么处理301跳转

发布时间:2021-12-20 10:37:12 作者:iii
来源:亿速云 阅读:309

本篇内容主要讲解“Spring MVC controller怎么处理301跳转”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring MVC controller怎么处理301跳转”吧!

一、 SpringMVC301,302跳转

spring mvc项目是spring的一个子项目用于处理视图层的请求。
302跳转很简单:

@RequestMapping(value = "blog.html")
public String rindex() {
 return "redirect:/blog";
}

按照不重复造轮子的理念,其实301跳转也很简单:

@RequestMapping(value = "blog.html")
public RedirectView rindex(HttpServletRequest request) {
	RedirectView redirectView = new RedirectView("/blog");
	redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
	return redirectView;	
}

spring mvc 提供了一个专门用于处理各种重定向的view视图。
跳转结果如下图:

Spring MVC controller怎么处理301跳转

其它各种实现如:

response.setStatus(301);
response.setHeader("Location", "https://www.xxxx");

二、 Redorect跳转传参

 Redorect传参3种方式

第一种:手动拼接url

new ModelAndView("redirect:/toList?param1="+value1+"&param2="+value2); 
这样有个弊端,就是传中文可能会有乱码问题,需要自己处理。 


第二种:RedirectAttributes attributes

这里用它的addAttribute方法,这个实际上重定向过去以后你看url,是它自动给你拼了你的url。 
使用方法: 
 public String test(RedirectAttributes attributes) 

    attributes.addAttribute("test", "hello"); 
    return "redirect:/test/test2"; 

第三种:ModelAndView在model加上属性

ModelAndView  model = new ModelAndView("redirect:/main/index");   
model.addObject("userName", userName);  //把userName参数带入到controller的RedirectAttributes
return model;  

到此,相信大家对“Spring MVC controller怎么处理301跳转”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. spring MVC配置详解
  2. 初学者对Spring MVC的认识

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring mvc controller

上一篇:大数据中常用开发工具的高级使用技巧有哪些

下一篇:DataSphere Studio指的是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》