Java 带有“包含”页面的 jsp 页面中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/943770/
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
Variables in jsp pages with "included" pages
提问by Omar Kooheji
What are the scoping rules for variables in a jsp page with pages added to them using tags?
使用标签向其中添加页面的 jsp 页面中变量的范围规则是什么?
My understanding is that an included page is essentially copied verbatim into the page, which would lead me to assume that if I've declared a variable in a Parent JSP that it would be available in the child ones.
我的理解是,包含的页面本质上是逐字复制到页面中的,这会让我假设如果我在父 JSP 中声明了一个变量,它就可以在子 JSP 中使用。
However Eclipse complains about this (understandably because I could feasibly include the pages in any page or use them as stand alone. And when I try to start the tomcat server it fails to start.
然而 Eclipse 对此抱怨(可以理解,因为我可以将页面包含在任何页面中或将它们用作独立页面。当我尝试启动 tomcat 服务器时,它无法启动。
I basically want to get a couple of variables from the session in the parent page and use them in the child pages. This doesn't work.
我基本上想从父页面的会话中获取几个变量并在子页面中使用它们。这不起作用。
So I've struck ont he idea of getting them from the session in each of the child pages, however I was wondering if I could give them all the same variable names, or if I'd have to pick different variable names for them in each page so they didn't clash.
所以我想到了从每个子页面的会话中获取它们的想法,但是我想知道是否可以给它们所有相同的变量名称,或者我是否必须为它们选择不同的变量名称每一页,所以他们没有冲突。
Also what about imports if I import log4net in the parent jss do I also have to import it in the child ones?
另外,如果我在父jss中导入log4net,我是否也必须在子jss中导入它?
采纳答案by Kris
In JSP there are two ways of including other jsp pages.
在 JSP 中,有两种方法可以包含其他 jsp 页面。
<%@include file="include.jsp"%>
and
和
<jsp:include page="include.jsp" />
If you use the former, then any variable declared on the parent JSP will be in scope in the include.jsp (of course Eclipse will not see this as you surmised) as it is effectively copied in by the compiler.
如果您使用前者,那么在父 JSP 上声明的任何变量都将在 include.jsp 的范围内(当然 Eclipse 不会像您推测的那样看到这一点),因为编译器有效地复制了它。
If you use the second approach, the inclusion is done at runtime and the include page has its own scope.
如果您使用第二种方法,则包含是在运行时完成的,并且包含页面有自己的作用域。
Ditto for imports. Although it is safe to redundantly import them in the include page.
进口同上。尽管在包含页面中冗余导入它们是安全的。
If I'm using the former I prefer to suffix them with .jspf to signify a JSP fragment. I can than turn off some of Eclipses warning in the fragment files. But in general I try to avoid using that method and prefer the second approach.
如果我使用前者,我更喜欢在它们后缀 .jspf 来表示 JSP 片段。我可以关闭片段文件中的一些 Eclipse 警告。但总的来说,我尽量避免使用这种方法,而更喜欢第二种方法。
More information can be found in the docs here: Include directiveand JSP include.
回答by mR_fr0g
From an object-orientated point of view, i would recommend not relying on the scope of the variable in parent.jsp being included in the child.jsp. This is because when i include a fragment in a jsp i tend to want to reuse that fragment in many different places. For example if i have a child.jsp i may want to use it in parent1.jsp as well as parent2.jsp. In the case it is better not to variable inheritence.
从面向对象的角度来看,我建议不要依赖 parent.jsp 中包含在 child.jsp 中的变量的范围。这是因为当我在 jsp 中包含一个片段时,我倾向于在许多不同的地方重用该片段。例如,如果我有一个 child.jsp,我可能想在 parent1.jsp 和 parent2.jsp 中使用它。在这种情况下,最好不要进行变量继承。
回答by Dagvadorj
Use the following, if you want to use variable within the path of the page to be included:
如果要在要包含的页面路径中使用变量,请使用以下命令:
<% pageContext.include("/cities/" + (String) request.getAttribute("country_code") + ".jsp"); %>
回答by Marouane Gazanayi
When you create a variable, you should set the scope to session, otherwise, the included page will not see it. Example :
创建变量时,应将作用域设置为会话,否则包含的页面将看不到它。例子 :
<logic:iterate id="supportTmp" name="publicites" indexId="indexLots" scope="session">
<c:set var="support" value="${supportTmp}" scope="session"/>
<c:choose>
<c:when test="${publiciteMoniteur == true}">
<jsp:include page="/jsp/bodies/suiviEnvoiPubliciteMoniteurLigne.jsp" />
</c:when>
<c:otherwise>
<jsp:include page="/jsp/bodies/suiviEnvoiPubliciteDefautLigne.jsp" />
</c:otherwise>
</c:choose>
</logic:iterate>