Get path from a JSP with Apache Tiles 3 and Spring MVC 3.2.3

1.4k Views Asked by At

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.

1

There are 1 best solutions below

1
On

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>