有一个执行是有条件的主体,但在这种情况下它每次都会被执行,为什么?

发布于 2025-01-05 08:31:17 字数 1382 浏览 1 评论 0原文

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2025-01-12 08:31:17

根据规则,只有当容器创建一个new Bean时,useBean标签的主体才应该被执行,因为useBean标签,如果Bean 不存在。

但就我而言,我已经在 classes 文件夹中的 foo 文件夹中创建了 Person.class bean web-inf。因此规则规定,如果 Bean 已经存在,则不应执行 setProperty 标记,但它每次都会被执行。

您将“类”与“实例”混淆了”。 Person.class 不算作 bean 实例。这是bean类的蓝图。

“如果 Bean 不存在”部分涉及放置在指定范围内的 bean 类的实例。您已指定将其放置在请求范围中。因此,每个 HTTP 请求都将重新创建 bean 实例,因为该实例将在请求结束时被丢弃。如果您通过 scope="session" 将其放置在会话范围内,那么它将在 HTTP 会话开始时创建,并在会话结束时被丢弃。一个会话可以跨越多个请求。

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.

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 by scope="session" then it will be created on start of HTTP session and be trashed on end of session. A session can span multiple requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文