Struts 中 JSP 中的活动表单是否有通用引用?

发布于 2024-08-21 05:11:39 字数 535 浏览 2 评论 0原文

我正在使用 struts 1.3.10

在 JSP 中,我可以从与操作关联的表单中引用一个对象。所以我可以:

<bean:define id="someObject" name="myCurrentActionForm" property="someObject"/>

在 struts-config.xml 中定义我的表单。

在我上面的例子中,“someObject”实际上是在 myCurrentActionForm 继承的基类中定义的。

但我需要以通用方式引用“someObject”。

我尝试过:

<bean:define id="someObject" name="baseForm" property="someObject"/>

使用对基本表单类的引用,但收到错误消息,无法找到该属性。

我需要在公共 JSP 片段中定义此 bean。有没有办法以通用方式引用与操作关联的表单?

TIA

I'm using struts 1.3.10

In JSP I can reference an object from the form that's associated with the action. So I can have:

<bean:define id="someObject" name="myCurrentActionForm" property="someObject"/>

Where I have my form defined in struts-config.xml.

In my case above "someObject" is actually defined in the base class that myCurrentActionForm inherits from.

But I need to reference the "someObject" in a generic way.

I tried:

<bean:define id="someObject" name="baseForm" property="someObject"/>

using the reference to the base form class but I get an error that the property could not be found.

I need to define this bean in a common JSP snippet. Is there a way to reference the form associated with the action in a generic way?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

对你再特殊 2024-08-28 05:11:39

我是这样做的。

<jsp:useBean id="baseForm" class="web.common.BaseForm" scope="application"/>

这将在我可以使用的应用程序范围内创建一个实例。

Here's how I did it.

<jsp:useBean id="baseForm" class="web.common.BaseForm" scope="application"/>

This creates a single instance at the application scope that I can use.

谜泪 2024-08-28 05:11:39

如果您使用:

<jsp:useBean id="baseForm" class="web.common.BaseForm" scope="application"/>

并且稍后使用:

<bean:define id="someObject" name="baseForm" property="someObject"/>

您应该不会有问题。如果该对象不存在, 将在应用程序范围内创建该对象,并且 检索它应该没有问题,因为它确实存在a pageContext.findAttribute(...) 搜索所有范围。

如果您收到“无法在任何范围内找到 bean:“baseForm””,则意味着当您调用 时该表单不存在(也许您没有调用 首先是?)。

另一件事是,应用程序范围不仅意味着所有 JSP 文件都可以访问该 bean,而且所有客户端都将共享同一个对象。如果该 bean 具有在用户之间共享的状态,那么您最终可能会遇到问题。一个用户可以访问另一用户的机密数据。

编辑:要正确理解这一点...您只想在 JSP 文件中以通用方式引用当前操作表单(无论是什么),而不依赖于您在 struts 中指定的名称-config(myCurrentActionForm、someOtherCurrentActionForm 等)并仅使用像 baseForm 这样的限定符?

如果是,那么您可以为自己创建一个所有其他操作类都将扩展的父 Action 类。在此父类的 execute(mapping, form, request, response) 方法中,您只需在所需范围内放置对 form 的引用(您可以提取确切的范围)来自 mapping 对象,或者只是使用 baseForm 名称将其存储在您想要的任何范围中 - 我会远离应用程序范围)。

然后,您可以使用模板方法模式来调用子类操作execute 方法,或者您可以借助子类 execute 方法调用父 execute 方法来完成其操作。

一旦进入 JSP,您就可以使用 baseForm 访问表单。标签不会关心范围是什么,因为它们会搜索直到找到对象。如果您坚持使用 web.common.BaseForm 中的属性,他们甚至不会关心它是一个子类实例。

If you use:

<jsp:useBean id="baseForm" class="web.common.BaseForm" scope="application"/>

and later on you use:

<bean:define id="someObject" name="baseForm" property="someObject"/>

you shouldn't have a problem. <jsp:useBean> will create the object in the application scope if it does not exist and <bean:define> should have no problem in retrieving it since it does a pageContext.findAttribute(...) searching for all scopes.

If you get "Cannot find bean: "baseForm" in any scope" that means that the form ain't there when you call <bean:define> (maybe you didn't call <jsp:useBean> first?).

One other thing, application scope does not only mean that all JSP files will have access to the bean, but also all clients will share the same object. You can end up having problems if that bean has state that you share between users. One user could have access to confidential data of another.

EDIT: To understand this right... you just want to reference the current action form (whatever that will be) in a generic way in your JSP files, without depending on the name that you specified in struts-config (myCurrentActionForm, someOtherCurrentActionForm etc) and just using a qualifier like baseForm?

If yes, than you could create yourself a parent Action class that all your other action classes will extend. In the execute(mapping, form, request, response) method of this parent class you just place a reference to the form in the desired scope (you can extract the exact scope from the mapping object or just store it in whatever scope you want, using the baseForm name – I would stay away from the application scope).

You could then use the Template Method Pattern to call the subclass actions execute methods, or you could resort to the subclass execute method calling the parent execute method to do its thing.

Once in the JSP you would then be able to access the form using baseForm. The tags won’t care what the scope is because they will search until they find the object. If you stick to properties from the web.common.BaseForm they won’t even care that it is a subclass instance.

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