1

I'd like to get the current path of the page I'm on from inside the JSP file. The JSP file in this case being a Tile as part of the apache tiles framework.

The URL I'm hitting is http://localhost:8080/dashboard/projects where 'dashboard' is the servlet and 'projects' maps to a spring controller like so:

@RequestMapping( { "/projects" } )
public String showAllProjectsPage( Map< String, Object > model ) {
    return "projects";
}

Where "projects" refers to a tile defined in my views.xml like so:

<tiles-definitions>
  <definition name="template" template="/WEB-INF/views/template.jsp">
    <put-attribute name="header" value="/WEB-INF/views/shared/header.jsp" />
    <put-attribute name="sidebar" value="/WEB-INF/views/shared/sidebar.jsp" />
    <put-attribute name="footer" value="/WEB-INF/views/shared/footer.jsp" />
    <put-attribute name="head" value="/WEB-INF/views/shared/head.jsp" />
  </definition>
  <definition name="home" extends="template">
    <put-attribute name="content" value="/WEB-INF/views/home/home.jsp" />
  </definition>
  <definition name="developer" extends="template">
    <put-attribute name="content" value="/WEB-INF/views/developers/developer.jsp"></put-attribute>
  </definition>
  <definition name="developers" extends="template">
    <put-attribute name="content" value="/WEB-INF/views/developers/developers.jsp"></put-attribute>
  </definition>
  <definition name="project" extends="template">
    <put-attribute name="content" value="/WEB-INF/views/projects/project.jsp"></put-attribute>
  </definition>
  <definition name="projects" extends="template">
    <put-attribute name="content" value="/WEB-INF/views/projects/projects.jsp"></put-attribute>
  </definition>
</tiles-definitions>

I've tried the following methods and they aren't working for me:

<% out.println(request.getPathInfo(  )); %>

Output: "null"

<% out.println(request.getPathTranslated(  )); %>

Output: "null"

<% out.println(request.getRequestURI(  )); %>

Output: "/dashboard/WEB-INF/views/template.jsp"

<% out.println(request.getRequestURL(  )); %>

Output: "http://localhost:8080/dashboard/WEB-INF/views/template.jsp"

<% out.println(request.getServletPath(  )); %>

Output: "/WEB-INF/views/template.jsp"

The part of the path I'm interested in is 'projects', and I thought that the method request.getPathTranslated() would do that for me, but no dice. I can only get back 'null' or else the path the the tiles template.

cmikeb1
  • 1,054
  • 9
  • 19

1 Answers1

2

I'm not sure what getPathTranslated() is supposed to do, but if it doesn't work can you add the path to the model as a workaround?

@RequestMapping("/projects")
public String showAllProjectsPage( Map< String, Object > model ) {
    model.put("tileDef", "projects");
    return "projects";
}

Then in your JSP access it like so:

<span id="tileDef">${tileDef}</span>
Dave L.
  • 9,595
  • 7
  • 43
  • 69
  • This is what I ended up doing, although I'd still like someone to chime in with some insight as to why getPathTranslated() doesn't work. The javadoc says: "Returns any extra path information after the servlet name but before the query string" which is exactly what I'm looking for. – cmikeb1 Sep 16 '13 at 22:55