JSF如何从备用Bean重定向到在备用Bean中动态构建的URL? [英] JSF How to redirect from backing bean to a URL dynamically constructed in the backing bean?

查看:79
本文介绍了JSF如何从备用Bean重定向到在备用Bean中动态构建的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个例子应该可以说明这个问题.我有10个文档显示在页面上,其中3个文档包含在zip包中的其他信息.在初始页面加载期间,我仅知道哪些文档具有此附加信息,而我 不知道这些zip文件的URL.因此,我将显示指向包含其他zip文件的3个文档的链接(获取Zip包").当用户单击获取Zip程序包"时,它将在后备Bean中调用一个方法,该方法会转到数据库以找出zip程序包的URL.完成此操作后,我想将zip软件包提供给浏览器,然后浏览器会弹出另存为..."对话框,用户可以保存zip软件包.

An example should hopefully demonstrate the issue. I have 10 documents that are displayed on the page, 3 of them have additional information contained inside zip packages. During the initial page load, I only know which documents have this additional information, I do not know the URL to these zip files. So then, I display a link ('Get Zip Package') to the 3 documents which contain additional zip files. When user clicks on 'Get Zip Package', it calls a method in the backing bean which goes to the database to figure out the URL of the zip package. Once this is done, I would like to serve the zip package to the browser which would then pop up as Save As... dialog and user can save the zip package.

我尝试了两种方法,但都没有用.

I have tried two approaches but neither of them work.

方法1

<p:commandLink actionListener="#{myBackingBean.zipPackage(aDocument)}"
               value="Get Zip Package"
               ajax="false"
               rendered="#{aDocument.packageAvailable}"/>

public String zipPackage(DocItem item){
  //logic here to figure out the URL for this item's zip package
  return packageLink;
}

方法2

<h:outputLink onclick="getPackageLink([{name:'product', value: '#{aResult.product}'}, {name:'version',value:'#{aResult.version}'}])"
   <h:outputText value="Get Documentation Package"/>
</h:outputLink>
<p:remoteCommand name="getPackageLink" actionListener="#{kbBackingBean.zipPackage()}"/>

public String zipPackage() {
  Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
  String product = (String) map.get("product");
  String version = (String) map.get("version");
  //logic here to figure out the URL for this item's zip package
  return packageLink;
}

在页面加载和显示10个文档时,获取Zip包"链接(对于3个文档)没有指向任何内容,实际上具有与页面相同的URL.当我单击它时,它向服务器发送一个GET请求,并调用后备bean函数.使用方法1时,浏览器似乎确实在等待backing bean方法完成之后才开始呈现响应.使用方法2时,无需等待,浏览器立即重新加载页面.我怀疑由于获取Zip包"链接指向页面本身,因此浏览器的GET请求将首先得到处理,而后备bean方法的响应只会丢失.

As the page loads and the 10 documents are displayed, the 'Get Zip Package' link (for the 3 documents) points to nothing, essentially has the same URL as the page. When I click on it, it sends a GET request to the server and calls the backing bean function as well. With approach 1, the browser does appear to wait for the backing bean method to finish before it begins rendering a response. With approach 2, there is no wait and the browser immediately reloads the page. I suspect that since the 'Get Zip Package' link points to the page itself, browser's GET request gets processed first and the backing bean method's response simply gets lost.

我怀疑第三种方法(我还没有尝试过)会很好用,那就是调用一个Servlet,然后将它提供给zip包.但是,看到我有一个指向zip包的直接URL,我希望有一种方法可以将其提供给浏览器,而无需涉及Servlet.

A third approach (I have not tried yet) which I suspect will work just fine is to invoke a Servlet which would then serve up the zip package. But seeing how I have a direct URL to the zip package, I was hoping there would be a way to serve that to the browser without getting a Servlet involved.

最欢迎有任何建议或方法允许我在不调用Servlet的情况下使用URL.当然,如果Servlet是唯一的正确"方式,我肯定会这样做.

Any suggestion(s) or approach(es) which would allow me to use the URL without invoking a Servlet are most welcome. Of course, if Servlet is the only 'proper' way of doing this, I will certainly do that.

谢谢.

推荐答案

您可以尝试使用这种逻辑:

You can try with this kind of logic :

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
context.redirect(context.getRequestContextPath() + "download-page.jsf?product=" + product + "&version=" + version);

这篇关于JSF如何从备用Bean重定向到在备用Bean中动态构建的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆