简体   繁体   中英

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM