Wicket:重定向到共享资源
我有一个检票表格,提交后应该为用户提供一个可供下载的文件。该文件由外部 servlet(基于表单)生成,该 servlet 将生成的内容写入响应的输出流。
通常,这可以在表单提交时使用 getRequestCycle().setRequestTarget(new RedirectRequestTarget(redirectUrl);
来完成,其中 redirectUrl
将是带有参数的外部 servlet 的 url。
但是,< code>redirectUrl 显示在浏览器的地址栏中,这不好,因为用户不应该看到参数值,
为了克服这个问题,我实现了一个自定义的 DynamicWebResource
。 ,它以 byte[]
形式获取 servlet 输出,并通过通常的 wicket 共享资源机制绑定到虚拟 url,
在表单提交时我执行以下操作:
RequestParameters rps = new RequestParameters();
rps.setResourceKey(new ResourceReference("myResource").getSharedResourceKey());
getRequestCycle().setRequestTarget(new SharedResourceRequestTarget(rps));
依赖 wicket 的内部处理方式。
然后 调试我可以清楚地看到我的共享资源被正确调用,外部 servlet 的响应被正确生成、获取,然后使用所有标头和内容写入当前响应,但之后没有任何反应,没有错误,只是
当我调用时 什么也没有发生。我的共享资源直接指定 url wicket 已将其绑定到(如 http://localhost:8080/webAppRoot/wicket/myResource?param1=value) - 一切正常,文件格式正确且可下载。
我还需要做些什么吗?这很可能是愚蠢的事情,但我已经用头撞墙一天了......
i've got a wicket form, which when submitted should give users a file to download. the file is generated by an external servlet (based on the form), which writes generated content to response's output stream.
normally this can be done using getRequestCycle().setRequestTarget(new RedirectRequestTarget(redirectUrl);
on form submit, where redirectUrl
will be the external servlet's url with parameters.
however, redirectUrl
shows up in browser's address bar, which is not good as users see parameter values, which they shouldn't.
in order to overcome that, i've implemented a custom DynamicWebResource
, which fetches the servlet output as byte[]
and is bound to a virtual url through usual wicket shared resource mechanism.
on form submit i do the following:
RequestParameters rps = new RequestParameters();
rps.setResourceKey(new ResourceReference("myResource").getSharedResourceKey());
getRequestCycle().setRequestTarget(new SharedResourceRequestTarget(rps));
and then rely on wicket's internal ways of handling.
when debugging i can clearly see that my shared resource is correctly invoked, the external servlet's response is correctly generated, fetched and then written to current response with all the headers and stuff, but nothing happens after that, no errors, just nothing.
when i call my shared resource directly specifying the url wicket has bound it to (like http://localhost:8080/webAppRoot/wicket/myResource?param1=value
) - everything works, file is well-formed and downloadable.
is there something additional i have to do? it's most probably something stupid, but i've been banging my head agains the wall for a day already...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。浏览器不处理响应的原因是表单是使用 AJAX 提交的。将提交类型更改为普通解决了该问题。
problem solved. the reason for the response not to be processed by the browser was the fact that the form was submitted using AJAX. changing submit type to plain solved the issue.