我建立与对一个解决方案:remoteCommand和这样的回答:In javascript, how can I uniquely identify one browser window from another which are under the same cookiedbased sessionId
我加入这个JS给我的模板,在window.name每个browswer标签创建一个唯一的ID的存储。然后调用一个p:remoteCommand检查GUID:
$(window).load(function() {
// ----------------------
var GUID = function() {
// ------------------
var S4 = function() {
return (Math.floor(Math.random() * 0x10000 /* 65536 */
).toString(16));
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
};
if (!window.name.match(/^GUID-/)) {
window.name = "GUID-" + GUID();
}
if ($('#guid_form\\:server_guid').text().length == 0 ||
$('#guid_form\\:server_guid').text() != window.name) {
checkGuid([{name:'guid', value:window.name}]);
}
})
增加了Primefaces remoteCommand到我的模板是由上面的脚本调用。
<h:form id="guid_form">
<h:outputText value="#{checkTabAction.guid}" id="server_guid"/>
<p:remoteCommand name="checkGuid" actionListener="#{checkTabAction.checkGuid}" process="@this" partialSubmit="true" />
</h:form>
并且增加了通过比较validateds当前浏览器标签/窗口的检查行动的GUID:
@ConversationScoped
@Named(value = "checkTabAction")
public class CheckTabAction implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Logger log;
private String guid = null;
public void checkGuid() {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String guid = params.get("guid").toString();
if (this.guid == null) {
this.guid = guid;
}
if (!StringUtils.equals(this.guid, guid)) {
log.info("New tab detected!");
throw new NonexistentConversationException("New tab detected!");
}
}
public String getGuid() {
return guid;
}
}
如果可以的话,升级到JSF-2.2:现在 – kolossus 2014-12-02 16:33:18
处理多个窗口,所有我后已经在JSF 2.2中读到过这样的消息:如果用户在新选项卡中打开链接/按钮,则无法工作,因为两个选项卡中的窗口ID都是相同的。无论如何,JSF 2.2的更新目前是不可能的。 – user1127860 2014-12-04 11:57:46
JSF 2.2只提供一个ClientWindow,但它不管理它的范围。使用DeltaSpike而不是Seam3。 DeltaSpike是这条道路上的未来,而且非常棒! – 2014-12-05 22:50:43