← 返回首页
SpringMVC教程(八)
发表时间:2020-06-01 16:57:13
redirect和forward

Controller在返回视图时,可以使用请求重定向或者请求转发,跳转到静态视图或者其它动作。

项目结构图如下:

接上节案例实现。

1.使用redirect. 在HelloController中测试请求重定向到用户的登录动作。

package com.controller;

import com.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloController {

    @RequestMapping(value = "testredirect")
    public String redirecttest(@ModelAttribute("loginUser") Users user) {
        System.out.println(user);
        return "redirect:/users/login";
    }
}

使用PostMan测试\testredirect,

可见使用请求重定向,ModelAttribute中的属性不会保留。

2.使用forward

在HelloController中测试请求转发向到用户的登录动作。

package com.controller;

import com.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloController {

    @RequestMapping(value = "testforward")
    public String forwardtest(@ModelAttribute("loginUser") Users user) {
        System.out.println(user);
        return "forward:/users/login";
    }


}

使用PostMan测试\testforward,

可见使用请求转发,ModelAttribute中的属性会保留。

3.使用redirect访问静态资源

redirect可以访问非/WEB-INF/安全目录下的静态资源。例如:

package com.controller;

import com.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloController {

    @RequestMapping(value = "testredirect2")
    public String redirecttest2() {

        return "redirect:/hello.html";
        //return "redirect:/WEB-INF/jsp/hello.jsp"; //失败,不能访问安全目录下的静态资源。
    }
}

4.使用forward访问静态资源

forward可以访问任意目录下的静态资源。例如:

package com.controller;

import com.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class HelloController {

    @RequestMapping(value = "testforward2")
    public String forwardtest2() {

        return "forward:/WEB-INF/jsp/hello.jsp";
    }

}

小结: Controller可以使用 redirect请求重定向和forward请求转发到其它静态资源和动作,但是在前后分离开发的大背景下,这个功能很少使用,仅仅做大概了解即可。