java Spring Controller 重定向到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10899072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:01:37  来源:igfitidea点击:

Spring Controller redirect to another page

javaajaxspringredirectcontroller

提问by user1386375

Hey I got the following problem. This is the content of the jspx file:

嘿,我遇到了以下问题。这是jspx文件的内容:

function postSMTH() {
    $.ajax({
        type: "POST",
        url: document.getElementById("urltxt").value,
        data: parameters,

        });
}

<input type="hidden" value="${pageContext.request.contextPath}/foo/foo2/foodat" name="urltxt" id="urltxt"/>

<div class="foodat"><a href="javascript:postSMTH();"><spring:message code="foo_foo2_foodat_text" text="FOODAT"/></a></div>

So if I push the submit button, the postSMTHfunction is called and the ajax object is paste to the Controller which look like this:

因此,如果我按下提交按钮,postSMTH则会调用该函数并将 ajax 对象粘贴到控制器,如下所示:

@Controller
@RequestMapping(value="/foo")
public class FooController {

..............

@RequestMapping(value="/foo2", method=RequestMethod.POST)
public String homePOST(HttpServletRequest request) {
    ........
}    
@RequestMapping(value="/foo2", method=RequestMethod.GET)
public String homeGET(HttpServletRequest request) {
    ........
} 


@RequestMapping(value="/foo2/foodat", method=RequestMethod.POST)
public String doTHAT(HttpServletRequest request) {
    //  check authorization

        Map fooMap = request.getParameterMap();

        // do something in the Database, depending on the paramMap

    return "redirect:/foo/foo1";
}
}

Everything is working fine regarding the Database, but the Problem is, that the redirect at the end DOESN'T work. It just stays at the page foo2.

关于数据库一切正常,但问题是,最后的重定向不起作用。它只停留在 foo2 页面。

I'm new to Spring, maybe its a little mistake somewhere. I just cant make it out by myself.

我是 Spring 的新手,也许在某个地方有点错误。只是我自己弄不出来。

Would be nice if someone would have some hint. Thanks

如果有人有一些提示,那就太好了。谢谢

回答by Eugene Kuleshov

You are making asynchronous form submission using jquery ajax call. So, after your request is completed you need to change the document location using javascript. E.g., something like this:

您正在使用 jquery ajax 调用进行异步表单提交。因此,在您的请求完成后,您需要使用 javascript 更改文档位置。例如,这样的事情:

$.ajax({
    type: "POST",
    url: document.getElementById("urltxt").value,
    data: parameters,
    complete: function() {
        window.location.replace(...);
      }
  });

回答by digitaljoel

It's not redirecting because you are submitting via ajax. You'll need to handle the redirect yourself. Perhaps this very popular question will help. How to manage a redirect request after a jQuery Ajax call

这不是重定向,因为您是通过 ajax 提交的。您需要自己处理重定向。也许这个非常受欢迎的问题会有所帮助。如何在 jQuery Ajax 调用后管理重定向请求