如何检查jsp文件是否包含在另一个jsp文件中?

发布于 2024-12-09 19:48:37 字数 501 浏览 0 评论 0原文

看看你们都知道如何在 jsp 中包含文件... 我有 2 个文件

one.jsp Two.jsp

给出了下面的代码

Long something = 0;

in Two.jsp 我在 one.jsp 代码中

<%@include file='two.jsp'%>
<%@include file='two.jsp'%>

,我已经两次包含相同的文件,

所以会出现错误,因为将创建一些变量 2 次,对吗?

所以这意味着在第二次包含文件之前我应该​​检查一下,如果 一旦包含,我就不应该再次包含它,如何检查 那 ?

就像在 php 中一样,有类似的函数 包含(“文件.php”); //这将包括文件

和 include_once("文件.php"); //如果之前未包含该文件,则这将包含

该文件,有任何解决方案吗???

see you all know about including file in a jsp...
i have 2 files

one.jsp
two.jsp

in two.jsp i have below given code

Long something = 0;

in one.jsp code is

<%@include file='two.jsp'%>
<%@include file='two.jsp'%>

i have included same file two times,

so there will be error because something variable will be created 2
times, right?

so it means before including file second time i should check that, if
once its included then i should not inculde it again, how to check
that ?

as in php there are functions like
include("file.php");
//that will include file

and
include_once("file.php");
//this will include file if its not included before,

ANY ANY solutions for that????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-12-16 19:48:37

事实上,拥有这两个静态包含将导致同一变量声明两次的错误。这些导入在编译时会将包含页面的代​​码附加到包含页面中,然后一起编译。

我不知道 JSP 中内置有任何 include_once 方法,但您可以通过使用全局 Set (即 HashSet ),声明为顶级页面中的变量,或声明为请求属性(处理结束时应清除该属性),您可以在其中添加已包含的页面的名称。

one.jsp

<% HashSet<String> pagesSet = new HashSet<String>(); %>
...
<%@include file='two.jsp'/>

two.jsp

<% if (!pagesSet.contains("two.jsp"){ %>
   //... Remember to actually ADD two.jsp to pageSet, 
   // because it IS being included NOW.
   pageSet.add("two.jsp");

   // Entire contents of two.jsp
   // ....
<% } %>

请注意,这与 C 中的 #ifndef #define #endif 模式非常相似。

请考虑使用动态包括您将避免出现重复变量的问题。包含的页面将在其自己的范围内执行,要访问服务器端变量,您必须使用 或在 RequestSessionApplication 范围。有关静态和动态包含之间的差异,请参阅此问题的答案: include指令和 jsp:include 属性名称问题

此外,如果您有大量“条件”静态包含,则最终可能会遇到 64K 方法限制

Indeed, having those two static includes will result in an error of having the same variable declared twice. Those imports, at compilation time, will append the code of the included page in the including one, and will then be compiled altogether.

I'm not aware of any include_once approach built-in in JSP, but you could do something similar by having a global Set (i.e. HashSet), declared as a variable in the top-level page, or as a request attribute (that you should be clearing when the processing ends) in which you could be adding the names of the pages already included.

one.jsp

<% HashSet<String> pagesSet = new HashSet<String>(); %>
...
<%@include file='two.jsp'/>

two.jsp

<% if (!pagesSet.contains("two.jsp"){ %>
   //... Remember to actually ADD two.jsp to pageSet, 
   // because it IS being included NOW.
   pageSet.add("two.jsp");

   // Entire contents of two.jsp
   // ....
<% } %>

Note this is quite similar to the #ifndef #define #endif pattern in C.

Take into account that using dynamic includes you would be avoiding the problem of having duplicate variables. The included page would execute on its own scope, and to access server-side variables you'll have to pass them through with <jsp:param> or in one of Request, Session or Application scopes. See this question's answer for the differences between static and dynamic includes: include directive and jsp:include attribute name problem

Additionally, if you have a great number of those "conditional" static includes, you can end up hitting the 64K method limit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文