找到你要的答案

Q:Best way to store selected tabbedPanel tab in session?

Q:存储tabbedpanel选项卡中选择会话方式最好?

What's the best way to store what tab a user has selected, so that it returns them to the same page if they navigate away from the page then back.

什么是最好的方式来存储什么选项卡的用户选择,使其返回到同一页,如果他们浏览页面,然后返回。

answer1: 回答1:

You can set Selected Tab index into session or PageParameter and use that value to set default setSelectedTab on page load. Example as below:

//addReportTabs is method to get List of ITab   

    TabbedPanel<ITab> panel = new AjaxTabbedPanel<ITab>("reportTabs", addReportTabs());

    panel.setSelectedTab(tabIndex);

你可以设置选定的选项卡索引会话或pageparameter用以设置默认setselectedtab页面加载值。例子如下:

//addReportTabs is method to get List of ITab   

    TabbedPanel<ITab> panel = new AjaxTabbedPanel<ITab>("reportTabs", addReportTabs());

    panel.setSelectedTab(tabIndex);
answer2: 回答2:

TabbedPanel uses its model to store the currently selected tab:

public class IndexInSessionModel implements IModel<Integer> {

    private static MetaDataKey<HashMap<String,Integer>> KEY = new MetaDataKey<HashMap<String,Integer>>() {
    };

    private Component component;

    public IndexInSessionModel(Component component) {
        this.component = component;
    }

    private Map<String,Integer> map() {
        HashMap<String,Integer> map = WebSession.get().getMetaData(KEY);
        if (map == null) {
            map = new HashMap<String, Integer>();
            WebSession.get().setMetaData(KEY, map);
        }
        return map;
    }

    @Override
    public Integer getObject() {
        Integer integer = map().get(component.getPageRelativePath());
        if (integer == null) {
            integer = 0;
        }
        return integer;
    }

    @Override
    public void setObject(Integer index) {
        map().put(component.getPageRelativePath(), index);
    }

    @Override
    public void detach() {
    }
}

Use it as so:

tabbedPanel.setDefaultModel(new IndexInSessionModel(tabbedPanel));

tabbedpanel使用模型来存储当前选定的选项卡:

public class IndexInSessionModel implements IModel<Integer> {

    private static MetaDataKey<HashMap<String,Integer>> KEY = new MetaDataKey<HashMap<String,Integer>>() {
    };

    private Component component;

    public IndexInSessionModel(Component component) {
        this.component = component;
    }

    private Map<String,Integer> map() {
        HashMap<String,Integer> map = WebSession.get().getMetaData(KEY);
        if (map == null) {
            map = new HashMap<String, Integer>();
            WebSession.get().setMetaData(KEY, map);
        }
        return map;
    }

    @Override
    public Integer getObject() {
        Integer integer = map().get(component.getPageRelativePath());
        if (integer == null) {
            integer = 0;
        }
        return integer;
    }

    @Override
    public void setObject(Integer index) {
        map().put(component.getPageRelativePath(), index);
    }

    @Override
    public void detach() {
    }
}

使用它作为:

tabbedPanel.setDefaultModel(new IndexInSessionModel(tabbedPanel));
wicket