【问题标题】:Spring Controller redirect to another pageSpring Controller 重定向到另一个页面
【发布时间】:2012-06-05 14:19:18
【问题描述】:

嘿,我遇到了以下问题。 这是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>

所以如果我按下提交按钮,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";
}
}

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

我是 Spring 新手,也许是某个地方的小错误。我自己一个人搞不定。

如果有人能给点提示就好了。谢谢

【问题讨论】:

  • 可能发生了一些异常并且重定向未被调用。您是否添加了 log/System.out 并看到最后一条语句被调用?

标签: java ajax spring redirect controller


【解决方案1】:

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

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

【讨论】:

  • 在这种情况下,您应该接受答案或给予一定的信任。谢谢
【解决方案2】:

它没有重定向,因为您是通过 ajax 提交的。您需要自己处理重定向。也许这个非常流行的问题会有所帮助。 How to manage a redirect request after a jQuery Ajax call

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2016-06-10
    相关资源
    最近更新 更多