[英]How to redirect to another request mapping of a spring MVC controller from a jsp within a jsp
Situation: I have a jsp within a jsp. 情况:我在一个jsp中有一个jsp。 I load another jsp into a div of the outer jsp using .html().
我使用.html()将另一个jsp加载到外部jsp的div中。 I want to redirect my url into an entirely new url mapping from a controller.
我想将URL从控制器重定向到全新的URL映射。
Sample controller: 样品控制器:
@RequestMapping(value = { "/main/submit" }, method = RequestMethod.POST)
public String main(ModelMap model) {
System.out.println("In controller");
return "redirect:/anotherJSP";
}
@RequestMapping(value = { "/anotherJSP" }, method = RequestMethod.POST)
public String anotherJSP(ModelMap model) {
System.out.println("In another");
return "anotherJSP";
}
Jsp within a jsp: 一个jsp中的jsp:
$.ajax({
type : "POST",
url : "/main/submit",
success : function(msg) {
console.log('redirect');
},
error : function() {
alert("Error.");
}
});
Now, the problem is that the outer jsp stays, and the /anotherJSP url only gets loaded in the innerJSP. 现在,问题是外部jsp保留了,而/ anotherJSP URL仅被加载到innerJSP中。 I wanted to leave the two jsps and go to the new request mapping URL.
我想离开两个jsps并转到新的请求映射URL。 Is there anyway I can do it?
反正我能做到吗? Thanks a lot in advance!
在此先多谢!
You can't redirect a POST
. 您不能重定向
POST
。
When you return redirect:/anotherJSP
, the server sends a redirect instruction back to the web browser, and the browser then sends a new GET
request for the given URL. 当您返回
redirect:/anotherJSP
,服务器将重定向指令发送回Web浏览器,然后浏览器针对给定的URL发送新的GET
请求。
The GET
request will be for the URL given, with any query parameters. GET
请求将针对具有任何查询参数的给定URL。 This means that and POST
payload (data) will be lost. 这意味着和
POST
有效负载(数据)将丢失。
Change @RequestMapping(value = { "/anotherJSP" }, method = RequestMethod.POST)
to @GetMapping("/anotherJSP")
(assuming Spring 4.3 or later). 将
@RequestMapping(value = { "/anotherJSP" }, method = RequestMethod.POST)
更改为@GetMapping("/anotherJSP")
(假设是Spring 4.3或更高版本)。
Since an ajax call is asynchronous the effect of return "redirect:/anotherJSP";
由于ajax调用是异步的,因此
return "redirect:/anotherJSP";
is not affecting the browser window, instead you should use window.location.href in your ajax call like this: 不会影响浏览器窗口,而是应在ajax调用中使用window.location.href,如下所示:
$.ajax({
type : "POST",
url : "/main/submit",
success : function(msg) {
console.log('redirect');
window.location.href = /anotherJSP;
},
error : function() {
alert("Error.");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.