Spring MVC中的重定向和转发!!!

参考入门案例工程:Spring MVC入门案例!!!-CSDN博客

首先在index.jsp文件中写两个超链接进行页面跳转:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    $Title$



重定向
请求转发

 1.重定向:在AccountController中定义两个方法,进行重定向。

@Controller
@RequestMapping("/account")
public class AccountController {

    @GetMapping("/findAccount3")
    public String findAccount3() {
        return "redirect:/account/findAccount4";
    }

    @GetMapping("/findAccount4")
    public String findAccount4(Model model) {
        model.addAttribute("msg", "重定向");
        return "success";
    }
}

2.转发:在AccountController中定义两个方法,进行转发。

@Controller
@RequestMapping("/account")
public class AccountController {

    @GetMapping("/findAccount5")
    public String findAccount5() {
        return "forward:/account/findAccount6";
    }

    @GetMapping("/findAccount6")
    public String findAccount6(Model model) {
        model.addAttribute("msg", "请求转发");
        return "success";
    }
}

你可能感兴趣的:(spring,mvc,java)