Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>rather than putting a fixed value in your view it would be best to get the context path for the request and then adding that to the path in your view.</p> <p>Add the following imports in to your Home controller;</p> <pre><code>import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.RequestContextHolder; </code></pre> <p>Then in the RequestMapping method get the current request object and create a UrlPathHelper instance and get the base path for the requests context;</p> <pre><code>HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); UrlPathHelper helper = new UrlPathHelper(); String baseURL = helper.getContextPath(request); </code></pre> <p>So, when run from vFabric locally, baseURL will be "/myapp" and when run from a Cloud Foundry instance it will be ""</p> <p>All that is left is to add this to the model and use it in the view;</p> <pre><code>model.addAttribute("relPath", baseURL); </code></pre> <p>I tested this with the Spring MVC template project in STS and it worked just fine, my HomeController looked like this;</p> <pre><code>package com.vmware.mvctest; import java.text.DateFormat; import java.util.Date; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.util.UrlPathHelper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.sun.tools.internal.ws.processor.model.Request; /** * Handles requests for the application home page. */ @Controller public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value = "/home", method = RequestMethod.GET) public String home(Locale locale, Model model) { logger.info("Welcome home! the client locale is "+ locale.toString()); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); UrlPathHelper helper = new UrlPathHelper(); Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); String baseURL = helper.getContextPath(request); model.addAttribute("serverTime", formattedDate ); model.addAttribute("relPath", baseURL); return "home"; } } </code></pre> <p>and my view looked like this;</p> <pre><code>&lt;%@ page session="false" %&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Home&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt; Hello world! (${relPath}) &lt;/h1&gt; &lt;a href="${relPath}/home"&gt;home&lt;/a&gt; &lt;P&gt; The time on the server is ${serverTime}. &lt;/P&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload