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

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

问题描述

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

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

我的理解是,包含的页面基本上是复制的verbatum页面,这将使我假设如果我在父jsp中声明了一个变量,它将在子节点中可用。

My understanding is that an included page is essentially copied verbatum 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 whe I try to start the tomcat server it fails to start.

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

I basically want to get a couple of variables fromt he 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.

也是wh如果我在父jss中导入log4net,那么我还必须在子导入中导入它吗?

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.

同样适用于进口。尽管在include页面中冗余导入它们是安全的。

Ditto for imports. Although it is safe to redundantly import them in the include page.

如果我使用前者,我更喜欢用.jspf后缀它们来表示JSP片段。我可以在片段文件中关闭一些日食警告。但总的来说,我试图避免使用该方法,而更喜欢第二种方法。

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页面中带有“included”的变量网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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