2013-08-20 103 views
1

我想从JSP文件中获取当前页面的路径。在这种情况下,JSP文件是一个Tile,作为apache tile框架的一部分。使用Apache Tiles 3和Spring MVC从JSP获取路径3.2.3

我打的网址是http://localhost:8080/dashboard/projects其中“仪表盘”是servlet和“项目”映射到弹簧控制,像这样:

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

这里的“项目”指的是在我的观点中定义的瓷砖.XML像这样:

<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> 

我尝试以下方法和他们不是为我工作:

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

输出: “null

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

输出: “null

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

输出: “/dashboard/WEB-INF/views/template.jsp

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

输出: “http://localhost:8080/dashboard/WEB-INF/views/template.jsp

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

输出:“/WEB-INF/views/template.jsp

我感兴趣的是“项目”,我认为方法request.getPathTranslated()会为我做的路径的一部分,但没有骰子。我只能返回'空'或其他瓷砖模板的路径。

回答

2

我不确定getPathTranslated()应该做什么,但如果它不起作用,可以将路径添加到model作为解决方法?

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

然后在你的JSP访问它像这样:

<span id="tileDef">${tileDef}</span> 
+0

这是我落得这样做,但我还是喜欢一个人到了一些关于为何getPathTranslated()没有按帮腔没有工作。 javadoc说:“返回任何额外的路径信息在servlet名称后面,但在查询字符串”之前,这正是我正在寻找的。 – CMikeB1

相关问题