有一个执行是有条件的主体,但在这种情况下它每次都会被执行,为什么?
这是我的 jsp,其中我编写了 useBean 标准操作,并且还包含 jsp:setProperty 主体! 根据规则,如果 Bean 不存在,则只有当 Container 由于 useBean 标签而创建新 Bean 时,才应执行 useBean 标签的主体。
但就我而言,我已经在 web-inf 的classes 文件夹中的foo 文件夹中创建了Person.class bean。 所以规则说如果 Bean 已经存在,那么 setProperty 标签不应该被执行,但它每次都会被执行。
如果我从 foo 文件夹中删除 Bean,那么我会收到 NoClassDefFound 异常。
如果有人知道的话,请提供正确的推理或给我任何逻辑。
action.jsp
<%@ page import="foo.Person"%>
<html>
<head>
<title>Action JSP</title>
</head>
<body>
<h1 align="center">Standard Actions are used Here.</h1>
<br>
<jsp:useBean id="person" class="foo.Person" scope="request" >
<jsp:setProperty name="person" property="name" value="Ankur Garg" />
</jsp:useBean>
Name is: <jsp:getProperty name="person" property="name" />
<br>
Name set by Standard action:
<%
foo.Person p = (foo.Person) pageContext.getAttribute("person",PageContext.REQUEST_SCOPE);
%>
<%=
p.getName()
%>
<br>
Residence not set:
<%=
p.getResidence()
%>
Password passes by user is:
<%
String param = request.getParameter("Password");
out.print("<br>"+param);
%>
<br>
<a href="CallActionJsp">Call Again </a>
</body>
</html>
谢谢&问候 安库加尔格
This is my jsp in which i have written useBean standard action and that contains a body of jsp:setProperty too!!
According to the rules the body of useBean tag should be executed only when Container is making a new Bean because of useBean tag, if Bean does not exist.
But in my case, i have already made Person.class bean in foo folder in classes folder of web-inf.
So rule says that if Bean already exist then setProperty tag should not be executed but it is getting executed everytime.
And if i removes the Bean from the foo folder then i gets a Exception of NoClassDefFound
Please provide the proper reasoning or give me any logic for this, if some one knows about it.
action.jsp
<%@ page import="foo.Person"%>
<html>
<head>
<title>Action JSP</title>
</head>
<body>
<h1 align="center">Standard Actions are used Here.</h1>
<br>
<jsp:useBean id="person" class="foo.Person" scope="request" >
<jsp:setProperty name="person" property="name" value="Ankur Garg" />
</jsp:useBean>
Name is: <jsp:getProperty name="person" property="name" />
<br>
Name set by Standard action:
<%
foo.Person p = (foo.Person) pageContext.getAttribute("person",PageContext.REQUEST_SCOPE);
%>
<%=
p.getName()
%>
<br>
Residence not set:
<%=
p.getResidence()
%>
Password passes by user is:
<%
String param = request.getParameter("Password");
out.print("<br>"+param);
%>
<br>
<a href="CallActionJsp">Call Again </a>
</body>
</html>
Thanks & Regards
Ankur Garg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将“类”与“实例”混淆了”。
Person.class
不算作 bean 实例。这是bean类的蓝图。“如果
Bean
不存在”部分涉及放置在指定范围内的 bean 类的实例。您已指定将其放置在请求范围中。因此,每个 HTTP 请求都将重新创建 bean 实例,因为该实例将在请求结束时被丢弃。如果您通过scope="session"
将其放置在会话范围内,那么它将在 HTTP 会话开始时创建,并在会话结束时被丢弃。一个会话可以跨越多个请求。You're confusing "classes" with "instances". The
Person.class
does not count as a bean instance. It's the blueprint of the bean class.The part "if
Bean
does not exist" concerns the instance of the bean class which is been placed in the specified scope. You've specified it to be placed in the request scope. So every single HTTP request will recreate the bean instance simply because the instance will be trashed by end of request. If you place it in the session scope byscope="session"
then it will be created on start of HTTP session and be trashed on end of session. A session can span multiple requests.