[英]Struts 1 - Calling more actions via one post without altering struts-config
I'm working on a legacy app (struts 1, own framework, java 6, on WebSphere 7) where I need to change the behavior of an operation. 我正在开发一个遗留应用程序(在WebSphere 7上为struts 1,自己的框架,java 6),在这里我需要更改操作的行为。
Let's call them (1) CopyItem , (2) AlterItem , (3) MarkAsCopied . 我们称它们为(1) CopyItem ,(2) AlterItem ,(3) MarkAsCopied 。
A button called one service which needs to be replaced with three other services. 一个称为一项服务的按钮,需要用其他三个服务替换。
Depending on the result of the first one, invoke the second one and so on. 根据第一个结果,调用第二个,依此类推。 I want to navigate in the end where the first one would take me (so it would look like the original behavior from user point of view).
我想在第一个带我的地方导航(所以从用户的角度看,它看起来像是原始行为)。
Initially I thought I wrap every parameter I need into a form, POST it, and then on the Java side, I would call action execute for each service. 最初,我认为我将所需的每个参数包装到表单中,将其发布,然后在Java方面,我将为每个服务调用动作执行。 Technically from CopyItemAction.execute() of CopyItem I would call AlterItem and MarkAsCopied executes as well.
从技术上CopyItemAction.execute()CopyItem的我会打电话AlterItem和MarkAsCopied执行为好。
I feel that this is pretty far from a clean solution. 我觉得这离解决方案还差得很远。
Do you have a better idea, how to do this? 您是否有更好的主意,该怎么做?
In the end I did it via synchronized Ajax. 最后,我通过同步的Ajax做到了。 Which is definitely bad practice, so I do not recommend if you have the access on the backend code.
这绝对是不好的做法,因此,我不建议您访问后端代码。 However in my case now it turned out to be a working not-that-ugly solution.
但是,以我为例,现在看来这不是一个可行的解决方案。 Based on Dave Newton's ( https://stackoverflow.com/users/438992/dave-newton ) proposal.
基于Dave Newton( https://stackoverflow.com/users/438992/dave-newton )的建议。
Keep that in mind the struts actions are still called according to the configuration defined in struts-config.xml. 请记住,仍然会根据struts-config.xml中定义的配置调用struts动作。
I created the js function below and invoked the services with the specified forms one by one. 我在下面创建了js函数,并以指定的形式一一调用了服务。 This way a form (POST) submit calls the struts action, but doesn't navigate away.
这样,表单(POST)提交将调用struts操作,但不会离开。 After they run successfully I redirected the page.
他们成功运行后,我将页面重定向。
function submitForm(form) {
if (form !== null && form !== undefined) {
let formData = new FormData(form);
let xhr = new XMLHttpRequest();
xhr.open('POST', form.getAttribute('action'), false);
var successResponse = false;
xhr.onreadystatechange = function() {
if(xhr.readyState == XMLHttpRequest.DONE) {
successResponse = xhr.status == 200;
}
}
xhr.send(formData);
return successResponse;
}
}
In conclusion: this is not a recommended solution, but if you're in such a situation like me, then it works. 结论:这不是推荐的解决方案,但是如果您遇到像我这样的情况,那么它会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.