Hi guys and sorry for stupid question (can't handle it for an hour yet):
is there an equivalent for
<jstl:url value="${myURL}" />
but for use in controllers? I need an application part too (e. g. URLs like /application/myURL).
Thanks!
Hi guys and sorry for stupid question (can't handle it for an hour yet):
is there an equivalent for
<jstl:url value="${myURL}" />
but for use in controllers? I need an application part too (e. g. URLs like /application/myURL).
Thanks!
I use implements ServletContextAware
http://static.springsource.org/sprin...textAware.html
ServletContextAware uses "injection by interface", if you implement this interface, you bean would be injected with the servlet context.
Once you get the servlet context (you need at least servlet 2.5), you can call "getContextPath".
http://grepcode.com/file/repo1.maven...xtPath% 28%29
so:
1. add a new member variable
2. implement an interfaceCode:private ServletContext servletContext;
3. implement the setter methodCode:public class YourController implements ServletContextAware { }
4. callto get the context path.Code:servletContext.getContextPath()
Last edited by titiwangsa; Sep 15th, 2012 at 08:06 AM. Reason: provide more info
titiwangsa, thanks alot for your reply.
With your solution I've found even simpler method (strange, but it was obvious) - just use request.getContextPath(). Something like this works perfect (I do not know, however, what is the minimum required version of spring for it):
So, to have full equivalent for <jstl:url value="${myURL}" /> we can use this code:Code:@RequestMapping(value = "/test") public String testProcess(Model model, HttpServletRequest request) { String appPath = request.getContextPath(); Logger.getLogger(this.getClass()).info("Application path is: " + appPath); return "viewName"; }
Best regardsCode:@RequestMapping(value = "/test") public String testProcess(Model model, HttpServletRequest request, HttpServletResponse response) { String myURL = "/nextTest"; String nextTestFullURL = response.encodeURL(request.getContextPath() + myURL); ... pass to model, if needed ... return "viewName"; }
Lsync