我正在处理一个 jsf 应用程序,它在某个时间需要在新选项卡中打开一个外部页面,让第一页保持事件状态。我需要找到一种让应用程序执行的方法,只需单击一下按钮:
我试过使用
<outputLink />
但它没有 Action 属性。我试过使用
<commandLink />
但它无法重定向到外面。我也试过
<commandLink />
与 target="_blank"
以及在支持 bean 中编码的重定向:<h:commandLink onclick="submit();" target="_blank" action="#{myBean.execute}" disabled="#{myBean.linkDisabled}" value="external link" />
并且,在支持 bean 中:
public String execute() {
linkDisabled = true;
try{
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(Constants.EXTERNAL_URL);
} catch(Exception e){
throw new FacesException("Redirection failed");
}
return THIS_PAGE;
}
一个页面在新标签页中打开,但它是当前页面而不是带有 URL
Constants.EXTERNAL_URL
的页面并且该按钮仍处于启用状态。不显示错误消息。有什么建议吗?提前致谢,安德里亚
最佳答案
你的问题令人困惑。您正在混合“链接”和“按钮”。您基本上需要通过 JavaScript 禁用客户端中的元素,因为响应不会返回到当前浏览器窗口/选项卡,而是返回到不同的浏览器窗口/选项卡。在 JavaScript 中,禁用按钮很容易,但禁用链接却很容易。为简单起见,我假设您的意思是并且可以使用 <h:commandButton>
并且这是当前表单中唯一的按钮。
要解决您的问题,您需要删除 onclick="submit()"
(因为它干扰了默认行为)并且您需要删除导航结果(这应该无关紧要,但您在某些 JSF 实现中永远不知道)并且您需要在客户端通过 JS 禁用该按钮。
你所有的代码都应该是这样的:
<h:form target="_blank">
<h:commandButton
value="external"
action="#{myBean.execute}"
onclick="setTimeout('document.getElementById(\'' + this.id + '\').disabled=true;', 50);" />
</h:form>
和
public void execute() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect(Constants.EXTERNAL_URL);
}
setTimeout()
是必要的,因为在发送表单数据之前按钮被禁用,这将导致按钮自己的 name=value 对不会作为请求参数发送,因此 JSF 不会调用任何操作。
关于jsf - 在新选项卡中重定向到外部 URL 并同时在支持 bean 中执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10188912/