刚刚编写了一些代码,用于在spring mvc中将一个动作重定向并转发到另一个动作。还使用GET和POST完成了一些排列。
我的问题是当我们从一个带有@RequestMapping的动作重定向/前进到另一个带有@RequestMapping和@ResponseBody的动作时,事情是如何工作的。
以下是带有ajax脚本的控制器和jsp的代码片段。对于无法处理响应错误的方法,提到了评论。
@Controller
@RequestMapping(value = "/myMvc")
public class MyMvcController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(Model map, HttpServletRequest req) {
return "home";
}
@RequestMapping(value = "/action1", method = RequestMethod.POST)
public @ResponseBody String action1(Model map, HttpServletRequest req) {
return "Sending response body as POST";
}
@RequestMapping(value = "/action2", method = RequestMethod.GET)
public @ResponseBody String action2(Model map, HttpServletRequest req) {
return "sending response body as GET";
}
/*****action3 to action6 Redirect******/
//POST to GET
@RequestMapping(value = "/action3", method = RequestMethod.POST)
public String action3(Model map, HttpServletRequest req) {
return "redirect:/myMvc/action2";
}
//POST to POST
@RequestMapping(value = "/action4", method = RequestMethod.POST)
public String action4(Model map, HttpServletRequest req) {
//will not execute
//Request method 'GET' not supported
return "redirect:/myMvc/action1";
}
//GET to POST
@RequestMapping(value = "/action5", method = RequestMethod.GET)
public String action5(Model map, HttpServletRequest req) {
//will not execute
//Request method 'GET' not supported
return "redirect:/myMvc/action1";
}
//GET to GET
@RequestMapping(value = "/action6", method = RequestMethod.GET)
public String action6(Model map, HttpServletRequest req) {
return "redirect:/myMvc/action2";
}
/*****action7 to action10 Forward******/
//POST to GET
@RequestMapping(value = "/action7", method = RequestMethod.POST)
public String action7(Model map, HttpServletRequest req) {
//will not execute
//Request method 'POST' not supported
return "forward:/myMvc/action2";
}
//POST to POST
@RequestMapping(value = "/action8", method = RequestMethod.POST)
public String action8(Model map, HttpServletRequest req) {
return "forward:/myMvc/action1";
}
//GET to POST
@RequestMapping(value = "/action9", method = RequestMethod.GET)
public String action9(Model map, HttpServletRequest req) {
//will not execute
//Request method 'GET' not supported
return "forward:/myMvc/action1";
}
//GET to GET
@RequestMapping(value = "/action10", method = RequestMethod.GET)
public String action10(Model map, HttpServletRequest req) {
return "forward:/myMvc/action2";
}
}
home.jsp文件
<button onclick="hitAction3()">hit action3</button><br><br>
<button onclick="hitAction4()">hit action4</button><br><br>
<button onclick="hitAction5()">hit action5</button><br><br>
<button onclick="hitAction6()">hit action6</button><br><br>
<button onclick="hitAction7()">hit action7</button><br><br>
<button onclick="hitAction8()">hit action8</button><br><br>
<button onclick="hitAction9()">hit action9</button><br><br>
<button onclick="hitAction10()">hit action10</button><br><br>
<script>
function hitAction3() {
$.ajax({
method:'POST',
url: "${pageContext.request.contextPath}/myMvc/action3",
}).done(function(data) {alert(data);});
}
function hitAction4() {
$.ajax({
method:'POST',
url: "${pageContext.request.contextPath}/myMvc/action4",
}).done(function(data) {alert(data);});
}
function hitAction5() {
$.ajax({
method:'GET',
url: "${pageContext.request.contextPath}/myMvc/action5",
}).done(function(data) {alert(data);});
}
function hitAction6() {
$.ajax({
method:'GET',
url: "${pageContext.request.contextPath}/myMvc/action6",
}).done(function(data) {alert(data);});
}
function hitAction7() {
$.ajax({
method:'POST',
url: "${pageContext.request.contextPath}/myMvc/action7",
}).done(function(data) {alert(data);});
}
function hitAction8() {
$.ajax({
method:'POST',
url: "${pageContext.request.contextPath}/myMvc/action8",
}).done(function(data) {alert(data);});
}
function hitAction9() {
$.ajax({
method:'GET',
url: "${pageContext.request.contextPath}/myMvc/action9",
}).done(function(data) {alert(data);});
}
function hitAction10() {
$.ajax({
method:'GET',
url: "${pageContext.request.contextPath}/myMvc/action10",
}).done(function(data) {alert(data);});
}
</script>
我所知道的是:
在重定向中:创建一个新的请求对象。
在转发中:相同的请求对象转发到下一个操作。
如果我失踪,请分享一些有关此事的规则或信息。
答案 0 :(得分:2)
重定向会向客户端发送带有Location
标头的HTTP 302响应,因此客户端无论如何都会执行GET 。
如果您希望“POST POST”案例有效,请使用正向前缀。
查看this SO question for more details on forward vs. redirect。