如何以编程方式设置下一个要执行的Action的FormBean?
我希望将一个操作转发到下一个配置的操作,但在此之前创建一个新的 ActionForm 实例并将其与转发的操作关联:
伪代码如下:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
ActionForward forwardPage = mapping.findForward("success");
// Some Code....
....
....
// finally
MyActionForm form = new MyActionForm();
form.setSomeProp(...);
form.setAnotherProp(...);
forwardPage.doSomeMagicAndAssociateWith(form);
return forwardPage;
}
抱歉,如果这是一个愚蠢的问题,我对这一切都是新手,并且已经无法找到答案...
谢谢
编辑1:
只是为了澄清:ActionForwarded 到“成功”在我现在必须添加的新功能之前就已经存在。
我只是希望在某些流程中,在初始流程(此处称为“成功”)之前进入并进行一些计算,然后在成功的情况下[:-)]将流程转发到“成功”操作。
希望我的观点现在更清楚了。
I wish to forward an action to the next configured action but prior to that to create a new ActionForm instance and have it associated with the forwarded action:
Psuedo code follows :
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
ActionForward forwardPage = mapping.findForward("success");
// Some Code....
....
....
// finally
MyActionForm form = new MyActionForm();
form.setSomeProp(...);
form.setAnotherProp(...);
forwardPage.doSomeMagicAndAssociateWith(form);
return forwardPage;
}
Sorry if its a dumb question, I'm new to all this and have not been able to find an answer...
Thanks
EDIT 1:
Just to clarify: ActionForwarded to "success" had existed prior to the new feature that I must now add.
I just wish to, in certain flows, come in before the initial flow (here called "success") and do some computations after which in case of success [ :-) ] to forward the flow to the "success" action.
Hope my point is clearer now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设 struts-config 中有一个简单的配置,如下所示:
要转发到第二个操作,您只需使用
return mapping.findForward("success");
并且流程将转发到第二个操作。假设您对每个 ActionForm 有如下一些配置:那么您想要的就是在转发之前获取
form2
并用数据填充它。您当然可以这样做,因为每个表单(无论是
form1
或form2
)都被绑定到具有指定名称的指定范围,但这会干扰 Struts 的正常流程这是一种不好的做法(在介入并抢尽风头之前,您必须真正了解 Struts 的工作原理)。这就是为什么我不会告诉你如何去做。我建议您尽可能将这两个操作解耦。每个操作都会做自己的事情,任何操作都不会依赖于另一个操作预先填充的数据。
为此,请将“神奇”代码移动到第二个操作中,并在到达那里后执行操作。如果您来自操作 1,则只需执行操作 2 中的操作,那么您可以向配置添加一个标志:
并在操作 2 中测试该标志 (
request.getParameter("theFlag") != null< /代码> 例如)。这样,您就可以将这两个操作仅通过标志绑定起来。
您可以做的另一件事是使用 DispatchAction 类。这是执行方法的集合,这些方法都可以共享相同的表单,因此您可以从一种方法转发到另一种方法,而无需管理多个表单。
编辑:根据您的评论:“...我的新功能,需要在它之前执行一些逻辑,并且只有在成功的情况下才继续执行“原始”操作。”您的意思是您只是需要这样的东西:
如果是这样,为什么您需要创建一个新的 ActionForm 实例并将其与转发的操作相关联?
Assuming a simple configuration in struts-config like the following:
To forward to your second action, you just use
return mapping.findForward("success");
and the flow will be forwarded to the second action. Assuming you have some configs like the following for each ActionForm:then what you want is to get your hand on
form2
and fill it with data before doing the forward.You can off course do that since each form (be it
form1
orform2
) are bounded to the specified scope with the specified name, but that will be interfering with Struts normal flow and it is a bad practice (you must really know how Struts works before stepping in and stealing the show). That's is why I won't tell you how to do it.What I recommend you to do instead is to decouple as much as you can the two actions. Each action will do its thing, no action will depend on data being pre-filled by another action.
To do that, move your "magic" code in your second action and do the operations once you are there. If you only need to do the operations in action 2 if you come from action 1 then you can add a flag to the configuration:
and test for that in action 2 (
request.getParameter("theFlag") != null
for example). This way, you have the two actions tied only by the flag.One other thing you can do is use DispatchAction classes. This is a collection of execute methods that could all share the same form so you forward from one method to the other without having multiple forms to manage.
EDIT: Based on your comment: "...my new feature, needs to execute some logic prior to it and only if successful go on to execute the "original' action." do you mean you just need something like this:
If that is it, why do you need to create a new ActionForm instance and have it associated with the forwarded action?
在操作中,您可以更改传入参数的 ActionForm 的字段值,但不能创建新的 ActionForm 并将其与下一个 JSP 页面关联。
如果您想在调用转发页面之间重置表单中的所有数据,您可以执行类似“form.reset();”的操作或“form.clear();”,在 Form 类中创建此方法。
如果您需要成功页面中表单中没有的字段,您可以使用“request”对象来存储这些字段。
In the action, you can change field values of the ActionForm passed in parameter, but you can't create a new ActionForm and associate it with the next JSP page.
If you want to reset all data in the form beetween calling the forwarded page, you can do something like "form.reset();" or "form.clear();", creating this method in your Form class.
If you need fields in the success page that are not in the form, you can use the "request" object to store theses fields.
我自己已经成功了。也许我的解决方案将解释我不确定我在问题中能够做什么...
假设这是我的 struts-config.xml 文件:
FirstForm、FirstAction、SecondForm 和 SecondAction
之前就已存在我的功能和我不希望干预他们的代码...NewAction
的execute()
执行以下操作:I have managed on my own. Maybe my solution will explain what I am not sure I had been able to in my question...
Say this is my struts-config.xml file:
FirstForm, FirstAction, SecondForm and SecondAction
had existed previous to my feature and I had NO wish to intervene within their code...The
NewAction
'sexecute()
does the following: