jsf – 在Richfaces中管理后退和前进

前端之家收集整理的这篇文章主要介绍了jsf – 在Richfaces中管理后退和前进前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用RichFaces组件库,我想管理Ajax导航的历史记录,因此最终用户可以使用浏览器的后退和前进按钮.

有没有干净的方法去做,设计模式,图书馆等?

解决方法

您可以使用 RSH来处理Ajax历史记录

对于该示例,假设您有一个用户应该选择颜色的页面.
然后,使用XmlHttpRequest将所选颜色发布到服务器.

现在,我们希望在按下后退和前进导航按钮时恢复先前的选择.

代码示例

豆:

public class Bean {

    private static final String DAFAULT_COLOR = "green";

    private Map<String,Color> colors;
    private Color selectedColor;
    private String restoredColor;

    @PostConstruct
    public void init() {
        this.colors = new HashMap<String,Color>();
        this.colors.put("green",new Color("Green","008000"));
        this.colors.put("blue",new Color("Blue","0000FF"));
        this.colors.put("red",new Color("Red","FF0000"));
        this.colors.put("purple",new Color("Purple","800080"));
        this.colors.put("yellow",new Color("Yellow","FFFF00"));
        this.colors.put("silver",new Color("Silver","C0C0C0"));
        this.colors.put("black",new Color("Black","000000"));
        this.colors.put("white",new Color("White","FFFFFF"));

        this.selectedColor = this.colors.get(DAFAULT_COLOR);
    }

    public void setSelectedColor(ActionEvent event) {
        UIComponent component = event.getComponent();
        String color = ((String)component.getAttributes().get("color")).toLowerCase();
        this.selectedColor =  this.colors.get(color);
    }

    public void restoreColor() {
        if(restoredColor.equals("") || restoredColor.equals("null")) {
            restoredColor =  DAFAULT_COLOR; 
        }

        this.selectedColor =  this.colors.get(restoredColor);
    }

    public List<Color> getColors() {
        return Arrays.asList(colors.values().toArray(new Color[0]));
    }

    public Color getSelectedColor() {
        return selectedColor;
    }

    public String getRestoredColor() {
        return restoredColor;
    }

    public void setRestoredColor(String restoredColor) {
        this.restoredColor = restoredColor.toLowerCase();
    }

}

视图:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:t="http://myfaces.apache.org/tomahawk"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"  
    template="/WEB-INF/template/default.xhtml">

<ui:define name="head">
    <script type="text/javascript" src="#{request.contextPath}/js/rsh/rsh.js"></script>
    <script type="text/javascript">
        window.dhtmlHistory.create({
            toJSON: function(o) {
                return Object.toJSON(o);
            },fromJSON: function(s) {
                return s.evalJSON();
            }
        });

        Event.observe(window,'load',function() {
            dhtmlHistory.initialize();
            dhtmlHistory.addListener(handleHistoryChange);
        });

        var registerHistoryPoint = function(newLocation,historyData) {
            dhtmlHistory.add(newLocation,historyData); 
        };
    </script>
</ui:define>    

<ui:define name="content">
    <a4j:form id="frmColor">
        <div class="colors">
            <ul>
                <a4j:repeat value="#{bean.colors}" var="color">
                    <li style="background:##{color.hex};">
                        <a4j:commandLink value="&#160;"
                            actionListener="#{bean.setSelectedColor}" 
                            reRender="frmColor"
                            oncomplete="registerHistoryPoint('#{color.name}','#{color.name}');">
                            <f:attribute name="color" value="#{color.name}"/>
                        </a4j:commandLink>
                    </li>
                </a4j:repeat>
            </ul>
        </div>
        <div class="selection" style="background:##{bean.selectedColor.hex};">
            <div class="selected-color" 
                style="color: ##{bean.selectedColor.name eq 'White' or 
                         bean.selectedColor.name eq 'Yellow' ? '000000' : 'ffffff'}">
                <h:outputText value="#{bean.selectedColor.name}"/>
            </div>
        </div>
        <a4j:jsFunction name="handleHistoryChange" reRender="frmColor"
            action="#{bean.restoreColor}">
            <a4j:actionparam name="historyData" assignTo="#{bean.restoredColor}" /> 
        </a4j:jsFunction>
    </a4j:form>
</ui:define>
</ui:composition>

现在,当用户单击颜色时,将调用registerHistoryPoint.这将注册在按下后退和前进按钮时将传递给bean的historyData.

例如

>用户选择黄色.>黄色已注册.>用户选择蓝色.> Blue已注册.>用户点击后退.>黄色恢复.>用户点击前进.>蓝色恢复.

原文链接:https://www.f2er.com/ajax/453722.html

猜你在找的Ajax相关文章