• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Use of redirect: in Spring MVC

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working with the following controller from the Spring In Practice book:



The project is using an InternalResourceViewResolver. As you can see, the postComment() method is mapped to a URL of the form: "http://localhost:8080/sip/articles/{article-name}/commnets", where my web application context path is "/sip". When I hit this method (which saves a comment to an article), as you see above, the method does a redirect to view named by the string {pageNumber + "#comment-" + comment.getId()}, where pageNumber and comment.getId() are integers. This should form a string of the form: "1#commnet-5" for pageNumber = 1 and comment.getId() = 5. The return statement will then be: return "redirect:1#comment-5";. When I hit this method in the application with these values, it redirects to the URL "http://localhost:8080/sip/{article-name}/1?null". My understanding of redirect: was that it takes the view that follows ':' as the redirect URL, so it seems like it should have redirected to "http://localhost:8080/sip/1#comment-5". I don't see where the redirected URL is getting the {article-name} from and why it is dropping the "#comment-5". Also, as part of testing I changed the statement to: [return "redirect:";] and the URL I got was "http://localhost:8080/sip/#postComment". Could someone enlighten me as to what is going on. I am obviously not fully understanding how redirect: works in Spring. Thank you in advance.
 
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"I shall pass through this world but once. Any good, therefore, that I can show to any human being,
let me do it now. Let me not defer nor neglect it, for I shall not pass this way again."





GET-POST-REDIRECT pattern from http://en.wikipedia.org/wiki/Post/Redirect/Get

According to my understanding: See my down below Servlet3UploadController as a simple illustration concept-wise.
(Use case: The user get a upload form, post it, and recieve a status: success or failure)

For my example: I use TCP/IP monitor to see the traffic exchange between client and server

1. client From browser: http://localhost:18180/mvc/newupload (where mvc is my servlet context)
GET /mvc/newupload HTTP/1.1

server
HTTP/1.1 200 OK

2.
(client see the form and upload a file)

POST /mvc/newupload/form HTTP/1.1

3.
server
HTTP/1.1 302 Found
Location: http://localhost:18180/mvc/newupload;jsessionid=1AB62F1331AB48940C08CD248B75D486

(REDIRECT)

4. client see the form


 
Mike Tabak
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I understand this. Actually my questions comes down to the following. Say I have a controller with the following method:


This will try to redirect to a view "/ctxroot/rd/rdtest" where ctxroot is the context root. So in the above since we don't have a leading slash in the redirect (redirect:/rdtest), the redirect is with respect to /ctxroot/rd. If we did include the slash we would have redirected to "/ctxroot/rdtest". So when the slash is missing, is this what is referred to as redirecting to the parent URL?
 
H Paul
Ranch Hand
Posts: 491
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Trust but verify.



A. I assume there is not request mapping at the class level




From Spring Framework Reference Documentation 4.0.4.RELEASE
Section:
16.5. Resolving views
RedirectView

"redirect:{url}" is a RedirectView. When Spring see The redirect: prefix, it will take it a REDIRECT with {url} is the redirect URL.

For your case:
1.
return "redirect:/rdtest";
will be mapped as:
http://server:port/ctxroot/rdtest

(relative to the current Servlet context ctxroot)

2. BUT

return "redirect:rdtest";

will be mapped as: http://server:port/ctxroot/rd/test/rdtest

This is the FUN part with a debugger. (Trust but verify.)

B. Just use a debugger and set a break at below method and put a trace for log.



Put a trace for log:




Edited: Use TCP/IP mon to watch the exchange between client and server where you will see the absolute redirect URL: See header Location: {http url}
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic