springMvc请求的跳转和传值的方法

本文深入讲解了使用Servlet、Model对象及ModelAndView进行forward跳转的三种方式,包括在同一类及不同类方法间的跳转,以及redirect跳转到页面和控制器的方法。通过实例展示了如何在Spring MVC中有效利用这些技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

forword跳转页面的三种方式:

1.使用serlvet

?
1
2
3
4
5
6
7
8
9
10
11
12
/**
    * 使用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对象

?
1
2
3
4
5
6
7
8
9
10
11
12
/**
    * 使用forward跳转,传递基本类型参数到页面
    *   注意:
    *     1.使用springmvc 封装好的Model对象(底层就是request作用域)
    */
   @RequestMapping ( "/test1" )
   public String test1(Model model){
     String name = "张小四" ;
     model.addAttribute( "name" , name);
     return "back/attr" ;
     
   }

3.使用ModelAndView

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
    * 使用modelAndView
    *   注意事项
    *     modelAndView对象中的数据只能被ModelAndView对象的视图获取
    */
   @RequestMapping ( "/test2" )
   public ModelAndView test2(ModelAndView modelAndView){
     String name = "张小五" ;
     modelAndView.setViewName( "back/attr" );
     modelAndView.addObject( "name" , name);
     return modelAndView;
      
   }

当然也可以通过new 一个ModelAndView对象来实现

?
1
2
3
4
5
@RequestMapping ( "/test3" )
   public ModelAndView test3(){
     String name = "张小六" ;
     return new ModelAndView( "back/attr" , "name" , name);
   }

forword跳转到Controller中的方法:

跳转到相同类中的方法

?
1
2
3
4
5
6
7
8
9
/**
    * 使用forword跳转到相同类中的某一方法
    * 注意:
    *     1.不需要加上类上的@RequestMapping的值
    */
   @RequestMapping ( "/test00" )
   public String test00(){
     return "forward:test1" ;
   }

跳转到不同类中的方法:

?
1
2
3
4
5
6
7
8
9
/**
    * 使用forword跳转到不同类中的某一方法
    * 注意:
    *     1.需要加上类上的@RequestMapping的值:比如 :/hello
    */
   @RequestMapping ( "/test01" )
   public String test01(){
     return "forward:/hello/test" ;
   }

redirect跳转到页面:

 使用servlet

?
1
2
3
4
5
6
7
8
9
10
11
/**
    * 使用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

?
1
2
3
4
5
6
7
8
/**
    * 使用redirect跳转 向页面传递数据
    *     1..使用ModelAndView对象 modelAndView对象会把model中的数据以?形式拼接到地址栏后 可以使用${param.key}接受
    */
   @RequestMapping ( "/test5" )
   public ModelAndView test5(){
     return new ModelAndView( "redirect:/back/attr.jsp" , "name" , "小张张" );
   }

redirect跳转到Controller中的方法:

跳转到同类和不同类的方法都需要加上类上的@RequestMapping,就不粘出测试代码了

转载于:https://www.cnblogs.com/efforts-will-be-lucky/p/7404960.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值