Java 如何从jsp中的jsp重定向到SpringMVC控制器的另一个请求映射
情况: 我在jsp中有一个jsp。我使用.html()将另一个jsp加载到外部jsp的div中。我想将我的url从控制器重定向到一个全新的url映射 样本控制器:Java 如何从jsp中的jsp重定向到SpringMVC控制器的另一个请求映射,java,spring-mvc,jsp,redirect,Java,Spring Mvc,Jsp,Redirect,情况: 我在jsp中有一个jsp。我使用.html()将另一个jsp加载到外部jsp的div中。我想将我的url从控制器重定向到一个全新的url映射 样本控制器: @RequestMapping(value = { "/main/submit" }, method = RequestMethod.POST) public String main(ModelMap model) { System.out.println("In 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中的Jsp:
$.ajax({
type : "POST",
url : "/main/submit",
success : function(msg) {
console.log('redirect');
},
error : function() {
alert("Error.");
}
});
现在,问题是外部jsp保持不变,/anotherJSP url只在内部jsp中加载。我想离开这两个JSP,转到新的请求映射URL。还有什么我能做的吗?提前多谢 您不能重定向
帖子
当您返回redirect:/anotherJSP
时,服务器将重定向指令发送回web浏览器,然后浏览器为给定URL发送一个新的GET
请求
GET
请求将针对给定的URL,并带有任何查询参数。这意味着和POST
有效负载(数据)将丢失
将
@RequestMapping(value={/anotherJSP“},method=RequestMethod.POST)
更改为@GetMapping(/anotherJSP”)
(假设Spring 4.3或更高版本)。由于ajax调用是异步的,返回“重定向:/anotherJSP”的效果代码>不会影响浏览器窗口,相反,您应该在ajax调用中使用window.location.href,如下所示:
$.ajax({
type : "POST",
url : "/main/submit",
success : function(msg) {
console.log('redirect');
window.location.href = /anotherJSP;
},
error : function() {
alert("Error.");
}
});
据我所知,您希望它在有人转到/main/submit时导航到/anotherJSP?你的应用程序需要通过JS来完成吗?是的,这只是一个例子,这就是为什么它没有任何其他逻辑发生。实际上,我将数据从内部jsp发送到/main/submit/然后当提交成功时,它将重新加载整个jsp。很抱歉,我忘记编辑它了,requestmethod设置为GET not POST for另一个jsp。