Struts 中 JSP 中的活动表单是否有通用引用?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是这样做的。
这将在我可以使用的应用程序范围内创建一个实例。
Here's how I did it.
This creates a single instance at the application scope that I can use.
如果您使用:
并且稍后使用:
您应该不会有问题。如果该对象不存在,
将在应用程序范围内创建该对象,并且
检索它应该没有问题,因为它确实存在apageContext.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:
and later on you use:
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 apageContext.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 theexecute(mapping, form, request, response)
method of this parent class you just place a reference to theform
in the desired scope (you can extract the exact scope from themapping
object or just store it in whatever scope you want, using thebaseForm
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 subclassexecute
method calling the parentexecute
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 theweb.common.BaseForm
they won’t even care that it is a subclass instance.