从URL中检索片段(哈希)并将值注入bean中 [英] Retrieve the fragment (hash) from a URL and inject the values into the bean

查看:164
本文介绍了从URL中检索片段(哈希)并将值注入bean中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,将URL的片段(#)中的值注入bean(JSF),就像注入查询参数值一样。我正在使用Ben Alman的Bookmarkable jQuery插件( http://benalman.com/projects/jquery-bbq -plugin / )创建URL片段。我希望prettyFaces的自定义正则表达式模式可以解决我的问题,但直到现在我还没有成功。



http://ocpsoft.com/docs/prettyfaces/snapshot/en-US/html_single/# config.pathparams.regext



我想在此定义我的情况,如果有人有想法,i
会喜欢尝试一下。



我正在使用

RichFaces:3.3.3,

Spring: 3.0.2.RELEASE,

Hibernate:3.5.3-Final,

JSF:2.0.2-FCS,

PrettyFaces:3.0.1



Web应用程序生成以下类型的URL,其中参数
列在哈希(#)之后。我们的想法是拥有一个基于ajax的
Bookmarkable URL。因此,每次单击更改系统的
状态的元素时,都会在重写哈希值后通过ajax和
URL将值发送到服务器。在
哈希之后可以有1到3个参数,参数的数量是可选的。



我的目标是,当用户为URL添加书签时(带有哈希) )并且比
重新访问保存的页面,页面应该将正确的值
注入系统并以前一状态显示页面(如
query-parameter)。



下面,我有一个正则表达式,可以捕获哈希后的所有参数

  // URL:
http:// localhost:8080 / nymphaea / workspace /#node = b48dd073-145c -4eb6-9ae0-e1d8ba90303c& lod = 75e63fcd-f94a-49f5-b0a7-69f34d4e63d7& ln = en

//正则表达式:
\#(\ w * \ = (\w {8} -\w {4} -\w {4} -\w {4} -\w {12}))| \及(\w * \ = (\w {8} -\w {4} -\w {4} -\w {4} -\w {12}))| \及(\w * \ = \w {2})

我知道网站上有一些如何将URL片段发送到那里服务器端逻辑,





无论如何都要将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问题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; }< /风格>
< / h:head>
< h:body>
< h:form id =processFragmentclass =hide>
< h:inputText id =fragmentvalue =#{bean.fragment}>
< f:ajax event =changeexecute =@ formlistener =#{bean.processFragment}render =:showFragment/>
< / h:inputText>
< / h:form>
< p>更改网址中的片段。手动或通过这些链接:
< a href =#foo> foo< / a>,< a href =#bar> bar< / a>,< a href = #baz >巴兹< / A>
< / p>
< p>片段目前是:< h:outputText id =showFragmentvalue =#{bean.fragment}/>< / p>
< / h:body>
< / html>

以下是适当的bean的样子:



< pre class =lang-java prettyprint-override> package com.stackoverflow.q3475076;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.AjaxBehaviorEvent;

@ManagedBean
@RequestScoped
public class Bean {

private String fragment;

public void processFragment(AjaxBehaviorEvent event){
//在这里做你的事。这个例子只是打印到stdout。
System.out.println(进程片段:+片段);
}

public String getFragment(){
return fragment;
}

public void setFragment(String fragment){
this.fragment = fragment;
}

}

这就是全部。



请注意, onhashchange 事件相对较新,旧浏览器不支持。如果没有浏览器支持(未定义等),您需要查看 <$ c每隔一段时间使用 $ c> window.location.hash setInterval() 。上面的代码示例至少应该给出一个很好的启动。它至少适用于FF3.6和IE8。


I am looking for a way to inject values from the fragment (#) of a URL into bean(JSF), in the same way query-parameter values are injected. I am using Ben Alman's Bookmarkable jQuery plugin (http://benalman.com/projects/jquery-bbq-plugin/) to create the URL fragments. I was hoping that Custom regex patterns from prettyFaces could be a way to solve my problem but until now I have been unsuccessful.

(http://ocpsoft.com/docs/prettyfaces/snapshot/en-US/html_single/#config.pathparams.regext)

I would like to define here my situation and if any one has an idea, i would love to try them out.

I am using
RichFaces: 3.3.3,
Spring: 3.0.2.RELEASE,
Hibernate: 3.5.3-Final,
JSF: 2.0.2-FCS,
PrettyFaces: 3.0.1

The web application generates, following kind of URL where parameters are listed after a hash(#). The idea is to have an ajax based Bookmarkable URL. So every time I click on an element that changes the state of the system, the value is sent to the server via ajax and the URL after the hash is rewritten. There can be 1 to 3 parameters after the hash, the number of parameters are optional.

My goal is, when the user bookmarks the URL (with hash) and than revisits the saved page, the page should inject the correct values into the system and visualize the page in the previous state (like query-parameter).

Below, I have a regular expression that would catch all the parameters after the hash.

//URL:   
http://localhost:8080/nymphaea/workspace/#node=b48dd073-145c-4eb6-9ae0-e1d8ba90303c&lod=75e63fcd-f94a-49f5-b0a7-69f34d4e63d7&ln=en

//Regular Expression:    
\#(\w*\=(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}))|\&(\w*\=(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}))|\&(\w*\=\w{2})

I know there are websites that some how send the URL fragment into there server side logic,

Is there anyway to inject values from the URL fragments into server side beans?

解决方案

You can do this with help of window.onhashchange which fills an input field of a hidden form which submits itself asynchronously when the input field has changed.

Here's a kickoff example of the Facelets page:

<!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" class="hide">
            <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>

Here's how the appropriate bean look like:

package com.stackoverflow.q3475076;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.AjaxBehaviorEvent;

@ManagedBean
@RequestScoped
public 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;
    }

}

That's all.

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 code example should at least give a good kickoff. It works at at least FF3.6 and IE8.

这篇关于从URL中检索片段(哈希)并将值注入bean中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆