Announcement Announcement Module
Collapse
No announcement yet.
Access request context path from velocity/freemarker Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access request context path from velocity/freemarker

    I am trying to figure out how to make the context path of the current request available to the model for Velocity/Freemarker using SpringMVC. I need this to make absolute URLs work when needed. Is there a way to expose this easily to all my views?

    Thanks,
    Patrick

  • #2
    put the RequestContext into the model by setting the bean property 'requestContextAttribute' on your view (or view resolver for Velocity/FreeMarkerViewResolver). For example, in views.properties or equiv.
    Code:
    foo.requestContextAttribute=rc
    then in your template,
    Code:
    ${rc.contextPath}
    Regards,

    Comment


    • #3
      Thanks. I had seen that parameter before and had wondered if that was what I needed to set. Thanks for the clarification. I just tried it and like magic it worked.

      Patrick

      Comment


      • #4
        Append the jstl/core taglib in your freemarker file and set url as
        <@c.url value="<pathof css or js file>"/> @c.url automatic include contexpath.

        example:
        Code:
        <#assign c=JspTaglibs["http://java.sun.com/jsp/jstl/core"]/>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html>
        	<head>
        		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>	
        		<link type="text/css" rel="stylesheet" href="<@c.url value='/css/estilo.css'/>"/> 
        		<script type="text/javascript" src="<@c.url value='/js/jquery.js'/>"></script>
        </head>
        <body>
        .....
        </body>
        </html>

        Comment

        Working...
        X