0

I'm migrating a Play! 1.2 web application and moving to Spring Boot + Spring MVC. Some views contain URLs to other endpoints. For example, I display the book title on the page and next to it I want to add the URL to go the book's details page (e.g. localhost/books/{id}).

In Play! 1.2 the controllers are static, and there is also a Router which can create the full URL for a method belonging to another controller (Router.getFullUrl("BookController.bookDetails", args)), but how do I achieve this with Spring MVC?

Best regards, Cristian.

fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
Cristian
  • 417
  • 1
  • 9
  • 18
  • http://stackoverflow.com/questions/17218856/redirect-from-one-controller-method-to-another-controller-method - this may help you – Valath Jan 25 '17 at 13:16
  • I saw that post but I don't think that it applies to my scenario. For example, if I have 50 books on that page then I have 50 URLs to the book' detailed descriptions. Also, 90% of the time the user might not even want to see the details of a specific book but go to another tab in the web app. – Cristian Jan 25 '17 at 13:20
  • I believe you dont need any redirects, in anchor tag of each links displaying the corresponding content of the page, insert href as "localhost/books/{id}" .This will trigger your controller directly and render the view mentioned in it. – Valath Jan 25 '17 at 17:38
  • That's what I want to achieve, but I need to generate that URL because I can't write the URL manually everywhere. I need at least the path to the controller, so that I can add "/books/{bookId}" myself. – Cristian Jan 26 '17 at 08:31
  • Why u want to writ thise codes manually? What is your view?for example if its jsp or angular, u can have a single iterator that will iterate through list of records from backend to generate html. If you are using simple html as view think of jquery. – Valath Jan 26 '17 at 09:15
  • 1
    Maybe my point is not very clear. Let's say that I want to insert links to books' detail page in the jsp in the form of "https://admin.myapp.com/books/{bookId}". Because the section "https://admin.myapp.com" changes to "https://admin-test.myapp.com" for the test and "https://admin-acc.myapp.com" for acc, I can't simply write the complete URL in the JSP directly. I need to generate the URL by extracting the common path "https://admin.myapp.com" and then appending the controller and the method. Play! Framework does this for you, it builds a full URL only by giving the controller and the method. – Cristian Jan 26 '17 at 12:43

2 Answers2

0

If you are trying to get the app/deployed name automatically in .jsp files to make the urls, then please make use of context path. An example below :

<c:set var="context" value="${pageContext.request.contextPath}" />
<script src="${context}/themes/js/jquery.js"></script>

From your requirement "admin.myapp.com","admin-test.myapp.com" are server names right? Something like http://admin.myapp.com/book/{bookId},http://admin-test.myapp.com/book/{bookId}. In Spring app, relative path in jsp can be accessed using pageContext.request.contextPath

Valath
  • 880
  • 3
  • 13
  • 35
  • Yes, that is what I want to achieve. What I don't like is the fact that after the contextPath I have to add the controller mapping + method mapping manually, while in Play framework I could get them automatically. – Cristian Jan 26 '17 at 13:39
  • In Spring there is no alternative available for this controller mapping. – Valath Jan 26 '17 at 15:57
0

I also found the UriComponentsBuilder and ServletUriComponentsBuilder. They are similar to the Play! Framework router and provide methods for building URI's, handling parameters and the query etc. We chose to annotate the controllers' methods using constants and then use the same constants with the UriComponentsBuilder to build back the path and create the request query for GET requests.

Cristian
  • 417
  • 1
  • 9
  • 18