您可以借助此方法
window.onhashchange来填充隐藏表单的输入字段,该表单在输入字段发生更改时异步提交。
这是Facelets页面的启动示例:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>SO question 3475076</title> <script> window.onload = window.onhashchange = function() { var fragment = document.getElementById("processFragment:fragment"); fragment.value = window.location.hash; fragment.onchange(); } </script> <style>.hide { display: none; }</style> </h:head> <h:body> <h:form id="processFragment" > <h:inputText id="fragment" value="#{bean.fragment}"> <f:ajax event="change" execute="@form" listener="#{bean.processFragment}" render=":showFragment" /> </h:inputText> </h:form> <p>Change the fragment in the URL. Either manually or by those links: <a href="#foo">foo</a>, <a href="#bar">bar</a>, <a href="#baz">baz</a> </p> <p>Fragment is currently: <h:outputText id="showFragment" value="#{bean.fragment}" /></p> </h:body></html>这是合适的bean的外观:
package com.stackoverflow.q3475076;import javax.faces.bean.ManagedBean;import javax.faces.bean.RequestScoped;import javax.faces.event.AjaxBehaviorEvent;@ManagedBean@RequestScopedpublic class Bean { private String fragment; public void processFragment(AjaxBehaviorEvent event) { // Do your thing here. This example is just printing to stdout. System.out.println("Process fragment: " + fragment); } public String getFragment() { return fragment; } public void setFragment(String fragment) { this.fragment = fragment; }}就这样。
Note that the
onhashchangeevent is relatively new and not supported by the
older browsers. In absence of the browser support (undefinied and so on),
you’d like to check
window.location.hashat
intervals using
setInterval()
instead. The above pre example should at least give a good kickoff. It works
at at least FF3.6 and IE8.



