JSF-Primefaces-如何拦截p:menuitem结果

JSF-Primefaces-如何拦截p:menuitem结果,jsf,primefaces,actionlistener,menuitem,Jsf,Primefaces,Actionlistener,Menuitem,我正在尝试使用自定义ActionListener拦截p:menuitem结果,但我不能。看起来,如果我将结果更改为操作,它会起作用(当然),但我无法在这些链接中使f:param起作用 截取p:menuitem结果的正确方法是什么 更新:以下是我目前的方法/想法。听起来很麻烦,但可能有用。不过,我们欢迎采用更好的方法来实现这一点: 我正在考虑使用一个过滤器,就像BalusC在这里描述的那样 我最初的想法是拦截任何请求(不仅仅是动作请求),并对其应用一些安全规则 我做了一些研究,似乎有很多JSF应

我正在尝试使用自定义ActionListener拦截p:menuitem结果,但我不能。看起来,如果我将结果更改为操作,它会起作用(当然),但我无法在这些链接中使f:param起作用

截取p:menuitem结果的正确方法是什么

更新:以下是我目前的方法/想法。听起来很麻烦,但可能有用。不过,我们欢迎采用更好的方法来实现这一点:

我正在考虑使用一个过滤器,就像BalusC在这里描述的那样

我最初的想法是拦截任何请求(不仅仅是动作请求),并对其应用一些安全规则

我做了一些研究,似乎有很多JSF应用程序的安全模型,一些会受到bean方法的限制,一些会受到URL的限制,但在我的例子中,我想要一些可以通过xhtml控制的东西

所以我打算这样做-

我希望我的应用程序允许用户为每个xhtml应用权限规则,所以我正在考虑这个想法

我可以这样列出所有xhtml

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
List<String> pages = getResources("/private", ".xhtml");
for (String page : pages) {
    try {
        System.out.println(page);
        System.out.println(getMetaData(context.getResourceAsStream(page)));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
return pages;
ExternalContext=FacesContext.getCurrentInstance().getExternalContext();
列表页面=getResources(“/private”,“.xhtml”);
用于(字符串页:页){
试一试{
系统输出打印项次(第页);
System.out.println(getMetaData(context.getResourceAsStream(page));
}捕获(例外e){
e、 printStackTrace();
}
}
返回页面;
在哪里

private List getResources(字符串路径、字符串后缀){
ExternalContext=FacesContext.getCurrentInstance().getExternalContext();
Set resources=context.getResourcePaths(路径);
List filteredResources=new ArrayList();
for(字符串资源:资源){
if(resource.endsWith(后缀)){
filteredResources.add(资源);
}else if(resource.endsWith(“/”){
addAll(getResources(resource,后缀));
}
}
返回过滤器资源;
}
(以上代码从此处借用-)

每个xhtml在[h:head]中都有自己的元标记,如[meta name=“module”content=“Security”]

我可以使用SAX解析器解析每个xhtml以提取这些信息,如

private class MyDefaultHandler extends DefaultHandler{
    private String titleName = null;
    private boolean skip = false;

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if (!skip && qName.equalsIgnoreCase("meta")) {
            boolean isThis = false;

            for (int i = 0; i < attributes.getLength(); i++) {

                String attributeName = attributes.getLocalName(i);
                String attributeValue = attributes.getValue(i);
                System.out.println("found attribute with localname=" + attributeName + " and value=" + attributeValue);

                if (attributeName.equals("name") && attributeValue.equals("module")){
                    titleName = attributes.getValue(i);
                    isThis = true;
                    break;
                }
            }

            if (isThis){
                for (int i = 0; i < attributes.getLength(); i++) {
                    String attributeName = attributes.getLocalName(i);
                    String attributeValue = attributes.getValue(i);

                    if (attributeName.equals("content")){
                        titleName = attributeValue;
                        skip = true;
                        break;
                    }
                }
            }
        }
    }

    public String getTitleName(){
        return titleName;
    }
}

public String parseTitle(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {

    XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();

    //if you comment the line below, your parser will take 1 minute EACH XML
    reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    MyDefaultHandler handler = new MyDefaultHandler();
    reader.setContentHandler(handler);
    InputSource inputSource = new InputSource(new BufferedInputStream(inputStream));
    reader.parse(inputSource);

    return handler.getTitleName();
}
私有类MyDefaultHandler扩展了DefaultHandler{
私有字符串titleName=null;
private boolean skip=false;
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
if(!skip&&qName.equalsIgnoreCase(“meta”)){
布尔值isThis=false;
对于(int i=0;i
最后,正如Balluc所建议的,我可以从过滤器中获得一些会话范围的托管bean

public void doFilter(ServletRequest arg0, ServletResponse arg1,
        FilterChain arg2) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) arg0;
    HttpServletResponse response = (HttpServletResponse) arg1;
    HttpSession session = ((HttpServletRequest) request).getSession();
    Enumeration<String> names = session.getAttributeNames();
    while(names.hasMoreElements()){
        System.out.println(names.nextElement());
    }
    AuthorizationManagerMB authorizationManagerMB = (AuthorizationManagerMB)session.getAttribute("authorizationManagerMB");
    if (authorizationManagerMB == null){
        System.out.println("Filter = not logged");
    }else{
        System.out.println("Filtering = "+request.getRequestURI());
        authorizationManagerMB.doSomething();
    }
public void doFilter(ServletRequest arg0,ServletResponse arg1,
FilterChain arg2)引发IOException,ServletException{
HttpServletRequest=(HttpServletRequest)arg0;
HttpServletResponse=(HttpServletResponse)arg1;
HttpSession会话=((HttpServletRequest)请求).getSession();
枚举名称=session.getAttributeNames();
while(names.hasMoreElements()){
System.out.println(names.nextElement());
}
AuthorizationManagerMB AuthorizationManagerMB=(AuthorizationManagerMB)session.getAttribute(“AuthorizationManagerMB”);
if(authorizationManagerMB==null){
System.out.println(“过滤器=未记录”);
}否则{
System.out.println(“筛选=“+request.getRequestURI());
授权管理RMB.doSomething();
}
很麻烦:-)我知道

但是我可以通过xhtml管理访问,即使用户在浏览器中提供了一个直接的URL,不管它是否使用ajax

如果有人知道一种更简单、更优雅的方法,允许用户在运行中配置角色,那么欢迎:-)


另外,有趣的是,JSF使用xhtml的方法太少了。你不能列出,不能添加元数据,也不能检索元数据。它非常以bean为中心:-)

一个
结果
生成一个简单的GET请求,该请求可以与参数一起使用,不需要被表单包围。为了在服务器上执行一些逻辑右边,你需要先发一个帖子

对于您的具体情况,最好的选择是使用public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) arg0; HttpServletResponse response = (HttpServletResponse) arg1; HttpSession session = ((HttpServletRequest) request).getSession(); Enumeration<String> names = session.getAttributeNames(); while(names.hasMoreElements()){ System.out.println(names.nextElement()); } AuthorizationManagerMB authorizationManagerMB = (AuthorizationManagerMB)session.getAttribute("authorizationManagerMB"); if (authorizationManagerMB == null){ System.out.println("Filter = not logged"); }else{ System.out.println("Filtering = "+request.getRequestURI()); authorizationManagerMB.doSomething(); }