java 在 JSP 中获取当前文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/863248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current filename in JSP
提问by elzapp
Is there a way to get which JSP is currently rendered, with JSTL or Struts (or without)? like _ _ file _ _ in Python and PHP?
有没有办法使用 JSTL 或 Struts(或不使用)获取当前呈现的 JSP?像 Python 和 PHP 中的 _ 文件 _ _?
回答by Aaron Digulla
Well ... yes ... in a way
嗯......是的......在某种程度上
String __jspName = this.getClass().getSimpleName().replaceAll("_", ".");
I'm using a JSP called pre.jspfor that which I include at the top of each JSP in my webapp:
我正在使用一个调用的 JSP pre.jsp,它包含在我的 web 应用程序中每个 JSP 的顶部:
<%@page import="org.apache.log4j.Logger"%>
<%
String __jspName = this.getClass().getSimpleName().replaceAll("_", ".");
Logger log = Logger.getLogger(this.getClass().getName());
log.info("BEGIN JSP "+__jspName);
%>
<!-- BEGIN <%=__jspName %> -->
Plus I put this at the end of each JSP:
另外我把它放在每个 JSP 的末尾:
<!-- END <%=__jspName %> --><% log.info("END JSP "+__jspName); %>
That gives me a consistend log. To make sure each JSP is "correct", I have a check in my build script which just looks for the two strings "/pre.jsp"and ``END <%=__jspName`.
这给了我一个一致的日志。为了确保每个 JSP 都是“正确的”,我检查了我的构建脚本,"/pre.jsp"它只查找两个字符串和 ``END <%=__jspName`。
Note: There are many characters which are allowed in file names but not in Java class names. If you use them, your class names might look weird. If that's the case, I suggest to create a static helper function which converts class names to File names and call that, i.e.
注意:有很多字符可以在文件名中出现,但在 Java 类名中是不允许的。如果你使用它们,你的类名可能看起来很奇怪。如果是这种情况,我建议创建一个静态辅助函数,将类名转换为文件名并调用它,即
String __jspName = MyJspUtils.getFileName(this.getClass());
Each JSP compiler has it's own rules; here is one example: http://itdoc.hitachi.co.jp/manuals/3020/30203Y0510e/EY050044.HTM
每个 JSP 编译器都有自己的规则;这是一个例子:http: //itdoc.hitachi.co.jp/manuals/3020/30203Y0510e/EY050044.HTM
Kudos go to Marcus Junius Brutusfor pointing that out.
荣誉去马库斯Junius罗马家指出了这一点。
回答by Dima R.
The more convenient way is to use: <%= request.getRequestURI() %>
更方便的方法是使用:<%= request.getRequestURI() %>
<%= request.getRequestURI() %> For example, in all of my jsp files, I always do put this line:
<%= request.getRequestURI() %> 例如,在我所有的 jsp 文件中,我总是放这一行:
Rendering JSP File: '<%= request.getRequestURI() %>'
呈现 JSP 文件:'<%= request.getRequestURI() %>'
This puts a comented html line in to the rendered html. This way one cannot see it in the browser, but for debugging purposes, I can always see it inf I do "View source".
这会将注释的 html 行放入呈现的 html 中。这样人们就无法在浏览器中看到它,但出于调试目的,如果我执行“查看源代码”,我总是可以看到它。
回答by gwallet
I succeeded using JSTL as following :
我成功地使用 JSTL 如下:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<!-- <c:out value="${pageScope['javax.servlet.jsp.jspPage']}"></c:out> -->
...
And now, you should see as an HTML comment the name of the servlet produced by the container to render your JSP file, which name is very close to the JSP source file.
现在,您应该将容器生成的用于呈现 JSP 文件的 servlet 名称视为 HTML 注释,该名称与 JSP 源文件非常接近。
回答by Daniel De León
This is a simple copy-paste solution:
这是一个简单的复制粘贴解决方案:
<%=this.getClass().getSimpleName().replaceFirst("_jsp","")%>

