springMvc请求的跳转和传值的方法
作者:liuconglin 时间:2023-02-06 17:29:56
forword跳转页面的三种方式:
1.使用serlvet
/**
* 使用forward跳转,传递基本类型参数到页面
* 注意:
* 1.使用servlet原生API Request作用域
*
*/
@RequestMapping("/test")
public String test(HttpServletRequest request,HttpServletResponse response){
String name = "张小三";
request.setAttribute("name",name);
return "/back/attr";
}
2.使用Model对象
/**
* 使用forward跳转,传递基本类型参数到页面
* 注意:
* 1.使用springmvc 封装好的Model对象(底层就是request作用域)
*/
@RequestMapping("/test1")
public String test1(Model model){
String name = "张小四";
model.addAttribute("name", name);
return "back/attr";
}
3.使用ModelAndView
/**
* 使用modelAndView
* 注意事项
* modelAndView对象中的数据只能被ModelAndView对象的视图获取
*/
@RequestMapping("/test2")
public ModelAndView test2(ModelAndView modelAndView){
String name = "张小五";
modelAndView.setViewName("back/attr");
modelAndView.addObject("name", name);
return modelAndView;
}
当然也可以通过new 一个ModelAndView对象来实现
@RequestMapping("/test3")
public ModelAndView test3(){
String name = "张小六";
return new ModelAndView("back/attr", "name", name);
}
forword跳转到Controller中的方法:
跳转到相同类中的方法
/**
* 使用forword跳转到相同类中的某一方法
* 注意:
* 1.不需要加上类上的@RequestMapping的值
*/
@RequestMapping("/test00")
public String test00(){
return "forward:test1";
}
跳转到不同类中的方法:
/**
* 使用forword跳转到不同类中的某一方法
* 注意:
* 1.需要加上类上的@RequestMapping的值:比如 :/hello
*/
@RequestMapping("/test01")
public String test01(){
return "forward:/hello/test";
}
redirect跳转到页面:
使用servlet
/**
* 使用redirect跳转 向页面传递数据
* 1.使用Servlet原生API Session ServletContext
*/
@RequestMapping("/test4")
public String test4(HttpServletRequest request,HttpSession session){
String name = "张晓霞";
session.setAttribute("name", name);
return "redirect:/back/attr.jsp";
}
使用ModelAndView
/**
* 使用redirect跳转 向页面传递数据
* 1..使用ModelAndView对象 modelAndView对象会把model中的数据以?形式拼接到地址栏后 可以使用${param.key}接受
*/
@RequestMapping("/test5")
public ModelAndView test5(){
return new ModelAndView("redirect:/back/attr.jsp","name","小张张");
}
redirect跳转到Controller中的方法:
跳转到同类和不同类的方法都需要加上类上的@RequestMapping,就不粘出测试代码了
来源:http://www.cnblogs.com/liuconglin/p/5769893.html
标签:spring,mvc,传值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
你所不知道的Spring的@Autowired实现细节分析
2023-11-25 05:40:47
![](https://img.aspxhome.com/file/2023/0/60120_0s.jpg)
Ubuntu16.04下配置VScode的C/C++开发环境
2023-10-22 19:21:17
![](https://img.aspxhome.com/file/2023/6/123856_0s.jpg)
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
2021-12-29 08:15:07
![](https://img.aspxhome.com/file/2023/5/65725_0s.jpg)
SpringBoot整合Shiro框架,实现用户权限管理
2021-10-27 00:03:02
Android贝塞尔曲线初步学习第二课 仿QQ未读消息气泡拖拽黏连效果
2023-08-18 10:18:20
![](https://img.aspxhome.com/file/2023/4/120174_0s.gif)
Android launcher中模拟按home键的实现
2023-03-25 02:33:48
Android中去掉标题栏的几种方法(三种)
2023-04-01 10:57:37
![](https://img.aspxhome.com/file/2023/5/102575_0s.png)
Java的锁机制:synchronized和CAS详解
2023-03-18 16:04:00
![](https://img.aspxhome.com/file/2023/9/101929_0s.png)
Java SpringBoot实现带界面的代码生成器详解
2023-09-28 11:54:14
![](https://img.aspxhome.com/file/2023/0/63000_0s.png)
Android应用开发中模拟按下HOME键的效果(实现代码)
2023-03-09 00:13:14
springboot读取application.yaml文件数据的方法
2023-09-06 05:29:24
![](https://img.aspxhome.com/file/2023/6/63086_0s.jpg)
深入了解java NIO之Selector(选择器)
2022-02-01 16:39:17
Java 的可变参数方法详述
2022-06-03 19:12:34
![](https://img.aspxhome.com/file/2023/8/110908_0s.png)
java实现将域名解析成ip示例
2021-11-20 10:33:33
Spring实战之使用ClassPathResource加载xml资源示例
2023-11-28 23:00:30
Android虚拟机Dalvik和ART科普
2022-12-31 06:02:28
![](https://img.aspxhome.com/file/2023/9/138799_0s.png)
Android中NestedScrolling滑动机制详解
2022-09-16 22:32:13
![](https://img.aspxhome.com/file/2023/9/106059_0s.png)
Spring JPA联表查询之OneToOne源码详解
2022-08-06 13:57:55
![](https://img.aspxhome.com/file/2023/3/67603_0s.jpg)
Java日期操作类常见用法示例
2021-06-17 01:12:59
c#的dllimport使用方法详解
2023-04-20 04:01:49