我建立與對一個解決方案: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