Spring控制器重定向到另一个页面 [英] Spring Controller redirect to another page

查看:355
本文介绍了Spring控制器重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。
这是jspx文件的内容:

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

}
}

< input type =hiddenvalue =$ {pageContext.request.contextPath} / foo / foo2 / foodatname =urltxtid =urltxt >

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

因此,如果我按下提交按钮, postSMTH 函数被调用,并且ajax对象被粘贴到Controller,如下所示:

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

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

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


@RequestMapping(value =/ foo2 / foodat,method = RequestMethod.POST)
public String doTHAT(HttpServletRequest request){
//检查授权

fooMap = request.getParameterMap();

//在数据库中做一些事情,取决于paramMap

returnredirect:/ foo / foo1;
}
}

是的,重定向在末端不工作。它只停留在页面foo2。



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



如果有人会有一些提示,会很好。感谢

解决方案

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

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


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

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 postSMTH function is called and the ajax object is paste to the Controller which look like this:

@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.

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

Would be nice if someone would have some hint. Thanks

解决方案

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:

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

这篇关于Spring控制器重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆