【问题标题】:Primefaces Partial Rendering - commandButton actionListener not firedPrimefaces 部分渲染 - commandButton actionListener 未触发
【发布时间】:2012-08-02 00:22:42
【问题描述】:

我正在尝试为应用程序制作部分呈现导航。下面的代码是一个概念测试,除了 x.xhtml 文件上的命令按钮外,它运行良好。它不会触发 actionListeneron 单击。这用于更改包含部分的 url。

index.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <f:view contentType="text/html">
        <h:head>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
                <title>PrimeFaces</title>
            </f:facet>
            <style type="text/css">
                .ui-layout-north {
                    z-index:20 !important;
                    overflow:visible !important;;
                }

                .ui-layout-north .ui-layout-unit-content {
                    overflow:visible !important;
                }
            </style>
        </h:head>
        <h:body>
            <p:layout fullPage="true">
                <p:layoutUnit position="north" size="100" header="Top" resizable="true" closable="true" collapsible="true">  
                    <h:form>
                        <p:menubar>
                            <p:submenu label="File" icon="ui-icon-document">
                                <p:menuitem value="XXX" update=":wrapper" actionListener="#{tbean.doNav}">
                                    <f:attribute name="xxx_page" value="x.xhtml" />
                                </p:menuitem>
                                <p:menuitem value="YYY" update=":wrapper" actionListener="#{tbean.doNav}">
                                    <f:attribute name="xxx_page" value="y.xhtml" />
                                </p:menuitem>
                            </p:submenu>
                        </p:menubar>
                    </h:form>
                </p:layoutUnit>
                <p:layoutUnit position="center">
                    <p:outputPanel id="wrapper">
                        <ui:include src="#{tbean.url}"/>
                    </p:outputPanel>
                </p:layoutUnit>
            </p:layout>
        </h:body>
    </f:view>
</html>

x.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:component xmlns="http://www.w3.org/1999/xhtml"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:p="http://primefaces.org/ui" 
              xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:form>
        <p:commandButton value="zzz" update=":wrapper" actionListener="#{tbean.doNav}">
            <f:attribute name="xxx_page" value="z.xhtml" />
        </p:commandButton>

        <p:dataTable var="car" value="#{tbean.cars}">
            <p:column>
                <f:facet name="header">  
                    Name
                </f:facet>
                <h:outputText value="#{car.name}" />
            </p:column>
        </p:dataTable>
    </h:form>
</ui:component>

y.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:component xmlns="http://www.w3.org/1999/xhtml"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:p="http://primefaces.org/ui" 
              xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:outputText value="yyy"/>
</ui:component>

z.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<ui:component xmlns="http://www.w3.org/1999/xhtml"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:p="http://primefaces.org/ui" 
              xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:outputText value="zzz"/>
</ui:component>

tbean.java

package com.teste;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

@ManagedBean
@RequestScoped
public class tbean {

    private String url = "y.xhtml";
    private List<Car> cars = new ArrayList<>();

    public tbean() {
        for (int i = 0; i < 10; i++) {
            cars.add(new Car(i));
        }
    }

    public void setUrl(String url) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "setUrl :{0}", this.url);
        this.url = url;
    }

    public String getUrl() {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "getUrl :{0}", this.url);
        return this.url;
    }

    public void doNav(ActionEvent event) {
        this.url = (String) event.getComponent().getAttributes().get("xxx_page");
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "doNav :{0}", this.url);
    }

    public List<Car> getCars() {
        return cars;
    }
}

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    您的支持 bean 是请求范围的。这意味着它是在每个 HTTP 请求上创建的。所以url 属性将默认为y.xhtml 在每个请求上。

    通过命令按钮提交表单会创建一个新的 HTTP 请求。因此,它获取了请求范围 bean 的新实例,其 url 属性默认为 y.xhtml。当 JSF 需要处理表单提交时,它无法确定按下的按钮,因为它不在 y.xhtml 中。所以 JSF 不能调用与按下的按钮相关的动作。

    将 bean 放在视图范围内应该可以解决您的问题。

    @ManagedBean
    @ViewScoped
    public class tbean {
    

    这将在同一视图上的 HTTP 请求中正确记住 url 属性(通过在每个操作上返回 null 或 void)。

    另见:


    就整个设计而言,您需要绝对确保所有 ajax 操作都由 PrimeFaces 组件调用,而不是由标准 JSF &lt;f:ajax&gt; 调用,否则由于存在错误,仍然无法调用命令按钮JSF JS。

    另见:

    【讨论】:

    • 嗨 BalusC,我已经阅读了您的链接建议并尝试了 ViewScoped bean,但我继续使用未调用 actionlisterner 的相同结果。
    • 我解决了将 bean 范围从 @RequestScoped 更改为 @SessionScoped 并将我的 Mojarra 更新到 2.1.11 的问题。我的环境是 Glassfish-3.1.2 和 Primefaces-3.3.1
    • 嗨,在@BalusC 的类似回答之后,我做了同样的事情:使用 SessionScoped。但是 viewscoped 确实更正确并且可以工作,检查导入语句,看看你是否不混合面孔和企业导入语句(Netbeans 混合了我发现的两者)。查看我的问题和回复:stackoverflow.com/questions/12349255/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    相关资源
    最近更新 更多