如何在Primefaces + JSF2中显示用户点击的网址

时间:2012-10-06 16:49:59

标签: jsf-2 primefaces

我正在使用带有Prime面孔的JSF2。我想在每个页面中显示用户点击的所有以前的链接或网址。我该怎么做?

2 个答案:

答案 0 :(得分:0)

这是一个很好的链接:How can I get the request URL from a Java Filter?

我要做的就是使用类似的例子来做一组三个网址。

//Using linked example
    public class MyFilter implements Filter {
        public void init(FilterConfig config) throws ServletException { }

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
            chain.doFilter(request, response);

            String url = ((HttpServletRequest) request).getPathTranslated();
            UrlBean urlBean = (UrlBean) FacesContext.getCurrentInstance()getApplication().getValueBinding("#{urlBean}");
            urlBean.storeUrl(url);
        }

    public void destroy() { }
}

这是完全未经测试的,但这个想法应该有效。您只需要为组件实现一些逻辑(可能是堆栈),以便存储您需要的内容。 (显然你可能会多次使用同一个URL)。我应该注意到UrlBean只是一个抽象的想法,你必须实现它

答案 1 :(得分:-1)

您好我已经创建了这个Singleton类来让用户浏览Url。

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class UrlHistory {

    @SuppressWarnings("rawtypes")
    private static Map<String, List> store = new HashMap<String, List>();

    private static UrlHistoryBean instance = null;


    public static UrlHistoryBean getInstance() {
        if (instance == null) {
            instance = new UrlHistoryBean();
        }
        return instance;
    }

    LinkedList<UrlData> urlList = new LinkedList<UrlHistoryBean.UrlData>();

    public void addUrl(String sessionId, String urlString, int urlId) {
        UrlData data = new UrlData();
        data.setUrlName(urlString);
        data.setUrlId(companyId);
        if (urlList.isEmpty()) {
            urlList.add(data);
        } else {
            boolean isEqual = false;
            for (UrlData urlDataObj : urlList) {
                if (urlDataObj.equals(data))
                    isEqual = true;
            }
            if(!isEqual)

                urlList.addFirst(data);
        }
        store.put(sessionId, urlList);

    }

    @SuppressWarnings("rawtypes")
    public static Map<String, List> getStore() {
        return store;
    }

    @SuppressWarnings("rawtypes")
    public static void setStore(Map<String, List> store) {
        UrlHistoryBean.store = store;
    }

    public class UrlData {

        String urlName;
        int urlId;

        public String getUrlName() {
            return UrlName;
        }

        public void setUrlName(String UrlName) {
            this.UrlName = UrlName;
        }

        public int getUrlId() {
            return urlId;
        }

        public void setUrlId(int urlId) {
            this.urlId = urlId;
        }

        public boolean equals(UrlData rData) {
            boolean bEqual = false;
            if (this.getUrlId() > 0 && rData.getUrlId() > 0 && this.getUrlId() == rData.getUrlId()) {
                bEqual = true;
            }
            return bEqual;
        }

    }

}