当前位置: 首页 > 知识库问答 >
问题:

JSF-PrimeFaces-如何截取p:MenuItem结果

水焱
2023-03-14

更新:这里是我目前的方法/想法。听起来很麻烦,但可能有用。虽然,更好的实施方式是受欢迎的--

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

我如何从过滤器中读取JSF会话bean?

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;

哪里

private List<String> getResources(String path, String suffix) {
    ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
    Set<String> resources = context.getResourcePaths(path);
    List<String> filteredResources = new ArrayList<String>();
    for (String resource : resources) {
        if (resource.endsWith(suffix)) {
            filteredResources.add(resource);
        } else if (resource.endsWith("/")) {
            filteredResources.addAll(getResources(resource, suffix));
        }
    }
    return filteredResources;
}

(上面的代码借用自这里-https://community.jboss.org/thread/189427)

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

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();
}
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();
    }

有趣的是,JSF对XHTML的处理方法如此之少。不能列出,不能添加元数据,不能检索元数据。它相当以豆为中心:-)

共有1个答案

爱刚捷
2023-03-14

outpute生成一个简单的GET请求,该请求可以与参数一起使用,并且不需要被窗体包围。为了在服务器端执行一些逻辑,首先需要一个POST。

对于您的具体情况,最好的选择是使用action方法,它让您在导航到新视图之前执行操作。只需执行您想要的操作并返回与您的目标视图匹配的导航结果:

<p:menuitem action="#{bean.changePage(param)}" />
public String changePage(String param){
    //Do some stuff
    return "page2?faces-redirect=true&includeViewParams=true&p1="+param;
}

此外,您必须在目标视图中捕获该参数,并将其设置到bean中:

<f:metadata>
    <f:viewParam name="p1" value="#{destinationBean.param}" />
</f:metadata>
 类似资料:
  • 我将Java EE与JDK1.8、Maven和PrimeFaces一起使用。关于Primefaces的tabMenu,我有以下问题:如何将最后一个menuitem向右对齐?请看下面我的代码: 此代码段在最后一个menuitem中仍未将其右侧对齐:

  • 该场景是使用JSF和Primeface Mobile开发的移动页面。我想在多个页面之间的同一个xhtml页面中导航。当我单击主页中的按钮时,内容必须从第二页中的托管bean加载。我http://www.primefaces.org/showcase/mobile/navigation.xhtml遵循了这个示例,但我无法导航,我有以下消息“没有与viewIdmobile.xhtml匹配的导航用例,操

  • 我在我的UI上使用JSF的Primefaces。我对p:datatable上的ajax有一个特定的要求。这是我的项目的样本页面: 在这里,我有一个数据表,其中列出了创建的所有行。每行都有 id 和值。值是可编辑的。窗体中还有一个面板用于创建新行。后备 Bean 预先填充了所有创建的行,其中包含@PostConstruct。支持Bean有其他方法来编辑,取消和创建。此页面正在运行,下面给出的是新要求

  • 我与Spring的接触只有几个月,最近在浏览指南部分时遇到了Spring Boot。这些指南非常容易完成,并且能够很好地初步掌握项目的基本(也是很棒的)思想,即能够以最少的配置构建和部署企业级应用程序,同时坚持一系列Spring/JEE的良好实践。我真的对使用Spring Boot进行测试项目很感兴趣,因为有了它,设置和运行起来更容易、更快,而且仍然非常接近我的生产环境。我目前正在尝试用Sprin

  • JSF-2.0、Mojarra 2.1.19、PrimeFaces 3.4.1 问题摘要:在内部有一个和激发的inputText操作,该操作使用将dataTable行索引作为参数传递。但它总是传递dataTable的最后一行,而不是包含当前单击的的行的索引。 从我前面的问题中可以看出,我正在尝试使用作为一个状态的注释接受者,比如在Facebook等中。实现包括一个。它的行代表每种状态。好像是: 上

  • 上图显示了数据表中图像的id。当用户单击download时,应该下载对应于该id的图像。然而,我不确定如何做到这一点。有人能给我链接到教程或给我一些如何实现这一点的指针吗?