2014-11-04 112 views
1

里面一个JSP页面的文件系统路径,我是确定文件系统上的页当前路径,例如:确定当前JSP

WEB-INF/jsp/currentPage.jsp 

我希望能够给JSP:包括到另一个基于当前文件系统路径的页面,而不是用户用来访问该页面的URL。

我希望能够将相同的代码复制/粘贴到许多JSP,并让每个页面都知道它的文件系统路径。

+0

这似乎是非常不好的做法。不要超出应用范围。 – 2014-11-04 16:14:14

回答

0

答案是可用的here。基本上,您可以在this.getClass()上使用正则表达式来确定完整路径。

在JSP:

<jsp:include page='<%= SkinController.getSkinPath(this.getClass()) %>' /> 

而且在SkinController.java

public static String getSkinPath(Class<?> cls){ 
    Pattern pattern = Pattern.compile("org.*INF.?.(jsp|tiles).(.*?)_jsp"); 

    String name = cls.getName(); 

    Matcher matcher = pattern.matcher(name); 

    if(matcher.matches()){ 
     String type = matcher.group(1); 
     String path = matcher.group(2).replace(".", "/"); 

     //fullPath is the value we're looking for int he original question 
     String fullPath = type.concat("/").concat(path).concat(".jsp"); 

     return getSkinPath(fullPath); 
    }else{ 
     return ""; 
    } 

} 
0

为了引用其他jsp页面(以及JSP的其他实用程序方法),请使用JSTL库。非常便利。您可以查看this page以了解如何使用它。

0

使用要求geturi方法:

String uri = request.getRequestURI(); 
String pageName = uri.substring(uri.lastIndexOf("/")+1); 
+0

这将返回到[servlet的根目录]的路径(http://stackoverflow.com/questions/12160639/what-does-mean-in-the-method-servletcontext-getrealpath),我想要的路径当前的JSP页面。 我希望能够将相同的代码复制/粘贴到许多JSP,并让每个页面都知道它的文件系统路径。 – spaetzel 2014-11-04 16:18:46

+0

得到uri并解析它以知道你的jsp页面。 – SMA 2014-11-04 16:20:49

+0

由于JSP包含在其他JSP中,因此不会执行此操作。我需要实际的文件系统路径,而不是将它从请求URI中提取出来。 – spaetzel 2014-11-04 16:31:32