Reputation: 702
Just written some code for redirecting and forwarding from one action to another action in spring mvc.Also done some permutations with GET and POST.
My question is how things work when we redirect/forward from one action with @RequestMapping to another action having @RequestMapping and @ResponseBody.
Below is the code snippet for controller and jsp with ajax script.Comments are mentioned on the methods which are not working with response error.
@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 file
<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>
What I already know is :
In Redirect :A new request object is created.
In Forward :Same request object is forwarded to next action.
Please share some rules or information regarding this if I am missing.
Upvotes: 0
Views: 2560
Reputation: 59201
A redirect will send an HTTP 302 response to the client with a Location
header, so the client will perform a GET in any case.
If you want your "POST to POST" case to work, use a forward prefix.
Check out this SO question for more details on forward vs. redirect.
Upvotes: 2