【问题标题】:Forwarding or Redirecting to a @ResponseBody method with GET/POST使用 GET/POST 转发或重定向到 @ResponseBody 方法
【发布时间】:2015-05-07 18:30:30
【问题描述】:

刚刚在 spring mvc 中编写了一些用于从一个动作重定向和转发到另一个动作的代码。还使用 GET 和 POST 进行了一些排列。

我的问题是,当我们从一个使用@RequestMapping 的操作重定向/转发到另一个具有@RequestMapping 和@ResponseBody 的操作时,事情是如何工作的。

以下是控制器和jsp的代码sn-p与ajax脚本。评论提到了对响应错误不起作用的方法。

@Controller
@RequestMapping(value = "/myMvc")
public class MyMvcController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(Model map, HttpServletRequest req) {
        return "home";
    }

    @RequestMapping(value = "/action1", method = RequestMethod.POST)
    public @ResponseBody String action1(Model map, HttpServletRequest req) {

        return "Sending response body as POST";
    }

    @RequestMapping(value = "/action2", method = RequestMethod.GET)
    public @ResponseBody String action2(Model map, HttpServletRequest req) {

        return "sending response body as GET";
    }

    /*****action3 to action6 Redirect******/

    //POST to GET
    @RequestMapping(value = "/action3", method = RequestMethod.POST)
    public String action3(Model map, HttpServletRequest req) {

        return "redirect:/myMvc/action2";
    }

    //POST to POST
    @RequestMapping(value = "/action4", method = RequestMethod.POST)
    public String action4(Model map, HttpServletRequest req) {

        //will not execute 
        //Request method 'GET' not supported
        return "redirect:/myMvc/action1";
    }

    //GET to POST
    @RequestMapping(value = "/action5", method = RequestMethod.GET)
    public String action5(Model map, HttpServletRequest req) {

        //will not execute 
        //Request method 'GET' not supported
        return "redirect:/myMvc/action1";
    }

    //GET to GET
    @RequestMapping(value = "/action6", method = RequestMethod.GET)
    public String action6(Model map, HttpServletRequest req) {

        return "redirect:/myMvc/action2";
    }

    /*****action7 to action10 Forward******/

    //POST to GET
    @RequestMapping(value = "/action7", method = RequestMethod.POST)
    public String action7(Model map, HttpServletRequest req) {

        //will not execute 
        //Request method 'POST' not supported
        return "forward:/myMvc/action2";
    }

    //POST to POST
    @RequestMapping(value = "/action8", method = RequestMethod.POST)
    public String action8(Model map, HttpServletRequest req) {

        return "forward:/myMvc/action1";
    }

    //GET to POST
    @RequestMapping(value = "/action9", method = RequestMethod.GET)
    public String action9(Model map, HttpServletRequest req) {

        //will not execute 
        //Request method 'GET' not supported
        return "forward:/myMvc/action1";
    }

    //GET to GET
    @RequestMapping(value = "/action10", method = RequestMethod.GET)
    public String action10(Model map, HttpServletRequest req) {

        return "forward:/myMvc/action2";
    }
}

home.jsp 文件

<button onclick="hitAction3()">hit action3</button><br><br>
    <button onclick="hitAction4()">hit action4</button><br><br>
    <button onclick="hitAction5()">hit action5</button><br><br>
    <button onclick="hitAction6()">hit action6</button><br><br>
    <button onclick="hitAction7()">hit action7</button><br><br>
    <button onclick="hitAction8()">hit action8</button><br><br>
    <button onclick="hitAction9()">hit action9</button><br><br>
    <button onclick="hitAction10()">hit action10</button><br><br>

<script>
    function hitAction3() {
        $.ajax({
                method:'POST',
                url: "${pageContext.request.contextPath}/myMvc/action3",
        }).done(function(data) {alert(data);});
    }

    function hitAction4() {
        $.ajax({
                method:'POST',
                url: "${pageContext.request.contextPath}/myMvc/action4",
        }).done(function(data) {alert(data);});
    }

    function hitAction5() {
        $.ajax({
                method:'GET',
                url: "${pageContext.request.contextPath}/myMvc/action5",
            }).done(function(data) {alert(data);});
    }

    function hitAction6() {
        $.ajax({
                method:'GET',
                url: "${pageContext.request.contextPath}/myMvc/action6",
            }).done(function(data) {alert(data);});
    }

    function hitAction7() {
        $.ajax({
                method:'POST',
                url: "${pageContext.request.contextPath}/myMvc/action7",
        }).done(function(data) {alert(data);});
    }

    function hitAction8() {
        $.ajax({
                method:'POST',
                url: "${pageContext.request.contextPath}/myMvc/action8",
        }).done(function(data) {alert(data);});
    }

    function hitAction9() {
        $.ajax({
                method:'GET',
                url: "${pageContext.request.contextPath}/myMvc/action9",
            }).done(function(data) {alert(data);});
    }

    function hitAction10() {
        $.ajax({
                method:'GET',
                url: "${pageContext.request.contextPath}/myMvc/action10",
            }).done(function(data) {alert(data);});
    }
</script>

我已经知道的是:

在重定向中:创建一个新的请求对象。

In Forward : 相同的请求对象被转发到下一个动作。

如果我错过了,请分享一些关于此的规则或信息。

【问题讨论】:

    标签: spring jsp http spring-mvc servlets


    【解决方案1】:

    重定向将使用Location 标头向客户端发送 HTTP 302 响应,因此客户端无论如何都会执行 GET

    如果您希望您的“POST 到 POST”案例起作用,请使用前向前缀。

    查看this SO question for more details on forward vs. redirect

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2014-06-20
      相关资源
      最近更新 更多