如何在JSF中保留回发中的滚动位置?

时间:2008-10-29 17:52:34

标签: jsf

我正在使用JSF前端来处理上传或删除图像的页面。单击上载或删除按钮会导致回发,并以更新后的状态重新加载页面。但是,这会重置页面的滚动位置。我该如何在回发操作中保留此页面的回滚。

3 个答案:

答案 0 :(得分:1)

您可以使用actionListener执行此操作。例如,在您的页面中(例如page.jsf):

<f:view>    
    <h:form>
        <h:commandLink actionListener="#{bean.method}">
            <h:outputText value="Submit" />
            <f:param name="anchor" value="image" />
        </h:commandLink>
    </h:form>
    <div id='result'>
        <h1><a name='image'>Image</a></h1>        
    </div>
</f:view>

托管bean看起来像:

public class Bean {

    public void method(ActionEvent actionEvent) {

        // Get parameter
        String ancla = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("anchor");

        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("page.jsf#" + anchor);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

希望这有帮助

答案 1 :(得分:0)

如果您使用的是Apache MyFaces Tomahawk,则可以设置参数AUTOSCROLL,然后确保已启用AutoScrollPhaseListener。

我不确定这个功能是由JSF指定的,而是由Apache MyFaces Tomahawk额外实现的。

另外,请注意,在1.1.6版之前,AUTOSCROLL实现中存在跨站点脚本漏洞。

答案 2 :(得分:0)

你可以使用jquery ..使用cookies .. 在准备好的功能

    $(function(){

           //set window scroll position if cookie is set
       window.scroll(0,getCookie('myCookie'));
       //unset cookie after setting scroll position
       deleteCookie('myCookie'); 

       //make this class objects keep page scroll position
       jQuery(window).unload(function() {
        setCookie('myCookie', getPageScroll());
       });
        //-------------------



    });

准备好功能后添加此功能..

function setCookie(name,value) {

    var date = new Date();
    date.setTime(date.getTime()+(10*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

我希望这能帮到你..