【问题标题】:Property action listener is not called when attached to Primefaces' command link附加到 Primefaces 的命令链接时不调用属性操作侦听器
【发布时间】:2013-02-26 23:56:25
【问题描述】:

我正在使用 Primefaces,但我遇到了一个问题,即 setPropertyActionListener 未触发,因此未设置视图范围托管 bean 的属性。

我的看法:

<p:column>
    <p:commandLink value="Supprimer" oncomplete="confirmation.show()"  >
        <f:setPropertyActionListener value="#{car}" target="#{typeMB.selectedType}" />  
    </p:commandLink>
</p:column>

托管 bean 具有 selectedType 属性,其中有一个 getter 和一个 setter。

我的托管 bean:

@ManagedBean(name="typeMB")
@ViewScoped
public class TypeManagedBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private Type newtype;  
    private Type selectedType;

    @ManagedProperty(value="#{TypeDao}")
    GenericDao<Type> typeDAO;

    public TypeManagedBean(){
        newtype = new Type();
    }

    public List<Type> getList_types() {     
        return typeDAO.readAll();
    }

    public void setTypeDAO(GenericDao<Type> typeDAO) {
        this.typeDAO = typeDAO;
    }

    public GenericDao<Type> getTypeDAO() {
        return typeDAO;
    }

    public Type getNewtype() {
        return newtype;
    }

    public void setNewtype(Type newtype) {
        this.newtype = newtype;
    }       

    public Type getSelectedType() {
        if(selectedType != null)
        System.out.println("get : le selected type : "+selectedType.getLibelle());
        return selectedType;
    }

    public void setSelectedType(Type selectedType) {        
        this.selectedType = selectedType;
        System.out.println("set le selected type : "+selectedType.getLibelle());
    }

}

我可以做些什么来实现我想要的?

【问题讨论】:

  • 尝试添加&lt;p:commandLink&gt;process="@this" 属性。我希望你的组件包含在 one &lt;h:form&gt; 中,对吧?
  • 谢谢,它有效,是的,我的组件在 h:form 中
  • 谢谢@skuntsel。菜鸟错误,但你的评论让我意识到我有一个嵌套的
    导致 remoteCommand 不起作用!

标签: jsf-2 primefaces managed-bean view-scope


【解决方案1】:

根据Primefaces (3.5) user's guide&lt;p:commandLink&gt;上的部分,以及Primefaces lead on this forum的声明,process属性的默认值为@all,表示将提交整个页面。因此,此提交可能会出现一些验证错误,从而阻止调用侦听器方法。否则,它应该可以使用您发布的代码按预期工作。

对上述假设的一个很好的测试是添加process="@this" 属性。因为'与要执行的链接关联的操作,必须部分提交链接本身',正如 BalusC 在What is the function of this exactly 中完美解释的那样,我们需要添加属性来进行测试。

要检查的另一件事是您的命令组件属于表单,并且您的视图在任何地方都不包含嵌套表单。

【讨论】:

    【解决方案2】:

    以下代码正在运行:

    托管 bean:

    package app.so.dev.web.controller;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    
    import app.so.dev.web.model.Student;
    
    @ManagedBean(name = "so15344819")
    @ViewScoped
    public class SO15344819 implements Serializable {
    
        private static final long serialVersionUID = 6686378446131077581L;
        private List<Student> students;
        private Student selectedStudent;
    
        @PostConstruct
        public void init() {
            students = new ArrayList<Student>();
            students.add(new Student("Student 1"));
            students.add(new Student("Student 2"));
        }
    
        public List<Student> getStudents() {
            return students;
        }
    
        public void setStudents(List<Student> students) {
            this.students = students;
        }
    
        public Student getSelectedStudent() {
            return selectedStudent;
        }
    
        public void setSelectedStudent(Student selectedStudent) {
            this.selectedStudent = selectedStudent;
        }
    }
    

    还有xhtml:

    <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" template="/WEB-INF/templates/globalTemplate.xhtml">
    
        <ui:define name="title">15344819</ui:define>
        <ui:define name="content">
            <p:growl id="growl" showDetail="true" />
    
            <h:form id="form">
                <p:dataTable id="students" value="#{so15344819.students}" var="student">
                    <p:column>
                            <p:commandButton id="selectButton" update=":form:display" oncomplete="studentDialog.show()" icon="ui-icon-search" title="View">
                               <f:setPropertyActionListener value="#{student}" target="#{so15344819.selectedStudent}" />
                           </p:commandButton>
                       </p:column>
                </p:dataTable>
    
                <p:dialog header="Student Detail" widgetVar="studentDialog" resizable="false" id="studentDlg"
                                showEffect="fade" hideEffect="explode" modal="true">
    
                        <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
    
                            <h:outputText value="Name:" />
                            <h:outputText value="#{so15344819.selectedStudent.name}" style="font-weight:bold"/>                                               
    
                        </h:panelGrid>
    
                    </p:dialog>
            </h:form>
        </ui:define>
    
    </ui:composition>
    

    环境:

    • JSF Mojarra 2.1.7
    • Primefaces 3.4.2
    • JBoss AS 7.1

    Primefaces Showcase.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2018-02-21
      • 1970-01-01
      相关资源
      最近更新 更多