mardi 24 février 2015

Spring MVC Controller: what are difference between "return forward", "return redirect" and "return jsp file"

I don't understand what I should use. I have two pages - intro.jsp(1) and booksList.jsp(2). For each page I created one Controller Class. The first page has button which opens second page:



<form method="GET" action="/request-list">
<input type="submit"/>
</form>


The first question is: I am not sure about correctness this button. It works well, but I have question mark after press this button.


The second question is: When I press that button, method with next annotation is called (Controller for the second page):



@RequestMapping(value = "/books")
@Controller
public class BooksListController {

@RequestMapping
public String booksList() {
return "jsp/books/booksList";
}
}


What should I return by this method? In other words how can I jump from first page to second one?



  1. return "redirect:/books"; returns http://localhost:8080/books?

  2. return "jsp/books/booksList"; returns http://localhost:8080/request-list?

  3. return "forward:/books"; returns http://localhost:8080/request-list?


I see that result is the same: all these Strings gave me the same page (page 2 was opened). In which cases I should use "redirect", "forward", "page.jsp"?


Also I've read Post/Redirect/Get article. Do I have to use "redirect" after POST method handling??


Aucun commentaire:

Enregistrer un commentaire