带有“包含"的jsp页面中的变量页 [英] Variables in jsp pages with "included" pages

查看:15
本文介绍了带有“包含"的jsp页面中的变量页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用标签添加页面的jsp页面中变量的作用域规则是什么?

What are the scoping rules for variables in a jsp page with pages added to them using tags?

我的理解是,包含的页面本质上是逐字复制到页面中的,这使我假设如果我在父 JSP 中声明了一个变量,它就可以在子 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.

然而 Eclipse 对此抱怨(可以理解,因为我可以将页面包含在任何页面中或将它们用作独立页面.当我尝试启动 tomcat 服务器时,它无法启动.

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.

我基本上想从父页面的会话中获取几个变量并在子页面中使用它们.这不起作用.

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.

另外,如果我在父jss中导入log4net,我是否也必须在子jss中导入它?

Also what about imports if I import log4net in the parent jss do I also have to import it in the child ones?

推荐答案

在 JSP 中,有两种方法可以包含其他 jsp 页面.

In JSP there are two ways of including other jsp pages.

<%@include file="include.jsp"%>

<jsp:include page="include.jsp" />

如果您使用前者,那么在父 JSP 上声明的任何变量都将在 include.jsp 的范围内(当然 Eclipse 不会像您推测的那样看到这一点),因为编译器有效地复制了它.

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.

如果您使用第二种方法,则包含在运行时完成,并且包含页面有自己的作用域.

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.

如果我使用前者,我更喜欢给它们加上 .jspf 后缀来表示 JSP 片段.我可以关闭片段文件中的一些 Eclipse 警告.但总的来说,我尽量避免使用这种方法,而更喜欢第二种方法.

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.

可以在此处的文档中找到更多信息:包含指令JSP 包含.

More information can be found in the docs here: Include directive and JSP include.

这篇关于带有“包含"的jsp页面中的变量页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆