栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从URL检索片段(哈希),然后将值注入Bean

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从URL检索片段(哈希),然后将值注入Bean

您可以借助此方法

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

onhashchange
event 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.hash
at
intervals using
setInterval()

instead. The above pre example should at least give a good kickoff. It works
at at least FF3.6 and IE8.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/386947.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号