【问题标题】:Primefaces ajax event change one menu selectPrimefaces ajax 事件改变一个菜单选择
【发布时间】:2014-08-06 08:17:31
【问题描述】:

我正在使用 Primefaces 5。

我想在选择时调用监听器,监听器被调用但它返回一个空对象;

我的 JSF 页面中有以下内容

<ui:define name="page-content">
        <h:form prependId="false">
            <p:commandButton image="back" ajax="false" immediate="true" style="margin-right:20px;" value="#{phasemsgs['navigation.back']}" action="/boo/viewMesTaches.xhtml"/>

            <p:panel header="#{phasemsgs['tache.title']} Details" style="margin-top:10px">
                <p:messages />
                <h:panelGrid id="detail" columns="2" styleClass="grid" columnClasses="label,value">
                    <h:outputText value="#{phasemsgs['tache.nomtache.title']}:" />
                            <h:inputText id="phase_nomtache" value="#{TacheComponent.tache.nomtache}" required="false" label="phase_nomtache" />
<h:outputText value="Projet :*" />

                    <h:selectOneMenu id="selectProjet"
                        value="#{ProjetComponent.projet.idprojet}" required="false">

                        <f:selectItem itemLabel="Select One" itemValue="" noSelectionOption="true" />
                        <f:selectItems value="#{ProjetComponent.listProjets()}"
                            var="projet" itemValue="#{projet.idprojet}"
                            itemLabel="#{projet.nomprojet}" />
                            <p:ajax listener="#{ProjetComponent.onProjetChange()}"
                            update="selectPhase" />

                        <f:converter converterId="entityConverter" />

                    </h:selectOneMenu>

                    <h:outputText value="Phase :*" />

                <h:selectOneMenu id="selectPhase"
                        value="#{TacheComponent.tache.phase.idphase}" required="false">
                        <f:selectItem itemLabel="Select One" itemValue="" />
                        <f:selectItems value="#{ProjetComponent.listPhases}" var="phase"
                        itemValue="#{phase}" itemLabel="#{phase.typephase}" />
                        <f:converter converterId="entityConverter" />
                    </h:selectOneMenu>

                </h:panelGrid>

这是我的组件中的 onProjetChange 方法:

@Scope("session")
@Component("ProjetComponent")
public class ProjetComponentImpl implements ProjetComponent {
    private Projet projet;
    List<Phase> listPhases;
    @Autowired
    private ProjetDAO projetDAO;
        getters and setters ..
    public ProjetComponentImpl() {
    }
    @PostConstruct
        public void init() {
            //initialize the data here

            this.projet=new Projet();
            this.listPhases=new ArrayList<Phase>();
        }

    public void onProjetChange() {
            if(projet !=null && !projet.equals(""))
            {
               listPhases = new ArrayList<Phase> (projet.getPhases());
            System.out.println("onProjetChange Clause if:" + projet.getNomprojet());

            }
            else{
                listPhases = new ArrayList<Phase>();
                System.out.println("onprojetChange else:"+projet.getNomprojet());

            }

这是堆栈跟踪:

onProjetChange Clause if:null

这是一个截图:

【问题讨论】:

    标签: jquery ajax jsf jsf-2 primefaces


    【解决方案1】:

    正如@Jaqen H'ghar 所说,您没有为您的 Bean 设置 Phase 对象。

    因为您提供了一个列表作为选择选项

    <f:selectItems value="#{ProjetComponent.listPhases}" .../>
    

    您应该将所选项目反馈给您的 bean

    <h:selectOneMenu value="#{TacheComponent.selectedPhase}" ..>
    

    在哪里,您还应该在名为“TacheComponent”的 Bean 中为 selectedPhase 设置一个 get/set

        private Phase selectedPhase;
    public Phase getSelectedPhase() {
        return selectedPhase;
    }
    public void setSelectedPhase(Phase selectedPhase) {
        this.selectedPhase = selectedPhase;
    }
    

    最后一件事是,您的 Phase 类可能是 Pojo不是字符串 对象,因此您不能将它与 p 一起使用:selectXYZ 组件而不使用转换器。我怀疑这就是在某些时候尝试设置phase.idphase(我也怀疑是字符串属性)的原因,然后您尝试使用转换器,但您可能没有正确设置它。由于您没有为 entityConverter 提供代码,因此很难判断您做错了什么。

    使用转换器是一项非常简单的工作。你只需要花 10 分钟来了解它们是如何工作的,你就可以开始了。有很多关于转换器的教程,例如你可以试试this one

    【讨论】:

      【解决方案2】:

      如果我理解正确,我认为您必须直接绑定到 bean 上的实体,而不是绑定到它的 id。所以,使用

      <h:selectOneMenu id="selectProjet" value="#{ProjetComponent.projet}" required="false">
      

      itemValue="#{projet}"
      

      这样,entityconverter 就会完成这项工作。

      【讨论】:

        猜你喜欢
        • 2014-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多