jsp的范围:useBean
home.jsp
<jsp:useBean id="username" class="java.lang.String" scope="application"/>
<%
username="Jitendra";
%>
<jsp:include page="include.jsp"/>
include.jsp
<%=username%>
这给出了一个错误,指出“username”在 include.jsp 中未定义,即使 Bean 的范围是 application...
home.jsp
<jsp:useBean id="username" class="java.lang.String" scope="application"/>
<%
username="Jitendra";
%>
<jsp:include page="include.jsp"/>
include.jsp
<%=username%>
This gives an error saying “username” is undefined in include.jsp, even though the scope of Bean is application…
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
至于您的问题,您使用旧式脚本在本地声明的任何内容都不与
jsp:useBean
链接。此外,声明本地 scriptlet 变量在包含的页面中不可见,您需要至少将它们明确地放入请求范围中。因为使用 scriptlet 是一种不好的做法。我建议完全忘记它。在您的具体情况下,只需创建一个真实 java bean 来保存数据。也就是说,具有(隐式)默认构造函数和由公共 getter/setter 公开的私有属性的类。下面是一个基本示例:
然后您可以使用 servlet 类来预处理请求。为此,您可以使用 servlet 的
doGet()
方法。将此 Servlet 映射到
web.xml
中的url-pattern
(例如/show
)。然后,该 servlet 应该可以通过 http://example.com/context/show 访问,并且它的 doGet() 将立即执行。然后更改/创建 JSP 文件
show.jsp
并将其放置在/WEB-INF
中以防止直接访问(以便客户端无法通过http 访问它: //example.com/context/show.jsp
但被“强制”调用 servlet)并使用以下行:${user}
引用与以下对象关联的对象任何请求/会话/应用程序属性键user
。这在幕后进行jspContext.findAttribute(“用户”)
。由于返回的User
实例符合 javabean 规范,因此${user.name}
将调用上的
实例和 EL 将显示其结果。getName()
方法User哦,我应该补充一点,您不需要为此需要
jsp:useBean
,因为 servlet 已经创建并将所需的 bean 放入范围中。也就是说,我建议从一本像样的 JSP/Servlet 教程/书籍开始。示例:
希望这有帮助。
As to your problem, anything which you declare locally using the old fashioned scriptlets is not linked with a
jsp:useBean
. Also, declaring a local scriptlet variable is not visible in the included pages, you need to explicitly put them in at least the request scope. As using scriptlets is a bad practice. I recommend to forget about it at all.In your specific case, just create a real java bean to hold the data. That is, a class with an (implicit) default constructor and private properties which are exposed by public getters/setters. Here's a basic example:
Then you can use a servlet class to preprocess requests. You can use servlet's
doGet()
method for this.Map this servlet in
web.xml
on anurl-pattern
of for example/show
. This servlet should then be accessible byhttp://example.com/context/show
and itsdoGet()
would be executed immediately.Then change/create the JSP file
show.jsp
which you place in/WEB-INF
to prevent from direct access (so that clients cannot access it byhttp://example.com/context/show.jsp
but are "forced" to call the servlet) with the following line:The
${user}
refers to the object which is associated with any request/session/application attribute keyuser
. This does behind the scenesjspContext.findAttribute("user")
. As the returnedUser
instance conforms the javabean spec, the${user.name}
will invoke thegetName()
method on theUser
instance and the EL will display its outcome.Oh, I should add, you do not need
jsp:useBean
for this as the servlet has already created and put the desired bean in the scope.That said, I recommend to start with a decent JSP/Servlet tutorial/book. Examples:
Hope this helps.
您的代码是“邪恶的”,因为
java.lang.String
并不是真正的 bean。值得注意的是,它没有“set”方法来更改其文本(这是故意的,它应该是不可变的)。访问 bean 的方法是声明一个 bean,然后使用它的属性(即 get() 和 set() 方法后面的名称)来更改它。您不能直接更改实际的 bean 实例,只能更改其值。
Your code is "evil" because a
java.lang.String
is not really a bean. Significantly, it doesn't have a "set" method to change its text (that's intentional, it's meant to be immutable).The way you access beans is to declare one, and then use its properties (i.e. the names behind the get() and set() methods) to change it. You cannot directly change the actual bean instance, only its values.
在 Tomcat 6 上,
home.jsp
被转换为 Servlet 代码:虽然行为相同,但生成的确切代码取决于应用程序服务器实现。
本地
username
变量不会扩展到将从include.jsp
生成的 Servlet 中。您没有将值“Jitendra”设置到应用程序范围中,仅设置局部变量的值。正如其他人所指出的,不可变的 String 并不能成为一个很好的 bean。
On Tomcat 6,
home.jsp
is translated to the Servlet code:Although the behaviour is the same, the exact code generated depends on the application server implementation.
The scope of the local
username
variable does not extend into the Servlet that will be generated frominclude.jsp
. You are not setting the value "Jitendra" into the application scope, only setting the value of the local variable.As others have pointed out, an immutable String does not make a very good bean.