天天看点

springmvc的重定向和转发

重定向和转发

默认的是forward方式,

显式指定重定向:redirect

@RequestMapping("/testRedirect")
    public  String testRedirect(Model model){
        model.addAttribute("msg","redirectTo...");

//        不能直接重定向到web-inf文件下面的资源,这里面的资源对客户端不可见,必须由服务端访问
//        所以,在controller 另外定义一个方法doForward,转发访问redirect.jsp
        return "redirect:redirect";

    }
           
package com.wang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;

@Controller
public class GoMethodController {

    @RequestMapping("/testRedirect")
    public  String testRedirect(Model model){
        model.addAttribute("msg","redirectTo...");

//        不能直接重定向到web-inf文件下面的资源,这里面的资源对客户端不可见,必须由服务端访问
//        所以,在controller 另外定义一个方法doForward,转发访问redirect.jsp
        return "redirect:redirect";

    }


//    直接转发到页面
    @RequestMapping("/testForward")
    public  String testForward(Model model){
        model.addAttribute("msg","Forward to ...");
        return "redirect";
    }

//    重定向过来,之后转发
    @RequestMapping("/redirect")
    public  String doForward(Model model){
        model.addAttribute("msg","redirectTo Forward to ...");

        return "redirect";
    }

}