新建Controller:ForwardController

 1 package com.yas.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 @RequestMapping("/jump")
 8 public class ForwardController {
 9 
10     //forward:转发,页面跳转但地址栏不会改变
11 
12     @RequestMapping("/test1")
13     public String test(){
14         //return "hello";
15 
16         //另一种方式,使用转发关键字:forward
17         return "forward:/WEB-INF/jsp/hello.jsp";
18     }
19 
20     //转发到另一个action
21     @RequestMapping("/test2")
22     public String test2(){
23         //使用相对路径
24         return "forward:test1";
25 
26         //使用绝对路径
27         //return "forward:/jump/test1";
28     }
29 
30     //redirect:重定向,地址栏也会改变,体现为http的302跳转
31 
32     @RequestMapping("/test3")
33     public String test3(){
34         return "redirect:/index.jsp";
35     }
36 
37     @RequestMapping("/test4")
38     public String test4(){
39         //相对路径
40         return "redirect:test3";
41 
42         //绝对路径
43         //return "redirect:/jump/test3";
44     }
45 
46     /*
47     注意:
48     1、查询操作,采用forward
49     2、增删改操作,采用redirect,防止用户刷新页面反复操作数据库
50      */
51 }

 

posted on 2021-10-26 14:25  Sempron2800+  阅读(27)  评论(0编辑  收藏  举报