开发者

JSF : dynamically loaded page commandButton not working

I have a page that contains a PrimeFaces accordion panel, in each panel is a PrimeFaces datatable. Below the datatable is a button "Add study" for example and when this button is click I want to dynamically load a page into a <h:panelGroup> using a <ui:include>. When the button is being click I want to dynamically change the src value from the <ui:include> so that my page is loaded in the <h:panelGroup>. Now the problem is the following:

My managed bean is set to @RequestScoped and when I click on my button nothing is being rendered. If I set my managed bean to @SessionScoped the <h:panelGroup> is filled after the second click on the button, the first click doesn't do anything.

Used technologies : JSF2.0, PrimeFaces, Facelets

Here are some code snippets :

Main Page

<h:panelGroup id="content" layout="block">
                <ui:include src="#{studyBean.includePage}"/>
            </h:panelGroup>
<div id="dashboard">
                <h:form id="form" prependId="false">
                    <h:outputText value="Welkom #{loginBean.username}"/>
                    <p:accordionPanel autoHeight="true" effect="bounceslide">

                        <p:tab title="My studies">
                            <p:dataTable var="study" value="#{studyBean.studiesByUser}" id="studyTable"
                                         emptyMessage="No studies found for given user" paginator="true" rows="10"
                                         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                                         rowsPerPageTemplate="5,10,15" rowSelectListener="#{studyBean.onRowSelect}"
                                         selectionMode="single">

                                <p:column headerText="StartDate" sortBy="#{study.startDate}"
                                          filterBy="#{study.startDate}"
                                          filterMatchMode="contains" style="width: 100px;">
                                    <h:outputText value="#{study.startDate}">
         开发者_如何学JAVA                               <f:convertDateTime pattern="dd/MM/yyyy" type="date" timeZone="CET"/>
                                    </h:outputText>
                                </p:column>

                                <p:column headerText="EndDate" sortBy="#{study.endDate}"
                                          filterBy="#{study.endDate}}"
                                          filterMatchMode="contains" style="width: 100px;">
                                    <h:outputText value="#{study.endDate}">
                                        <f:convertDateTime pattern="dd/MM/yyyy" type="date" timeZone="CET"/>
                                    </h:outputText>
                                </p:column>
                                <p:column headerText="School" sortBy="#{study.school}" filterBy="#{study.school}}"
                                          filterMatchMode="contains" style="width: 100px;">
                                    <h:outputText value="#{study.school}"/>
                                </p:column>
                                <p:column headerText="Specialization" sortBy="#{study.specialization}"
                                          filterBy="#{study.specialization}}" filterMatchMode="contains"
                                          style="width: 100px;">
                                    <h:outputText value="#{study.specialization}"/>
                                </p:column>
                                <p:column headerText="Remarks" sortBy="#{study.remarks}"
                                          filterBy="#{study.remarks}}"
                                          filterMatchMode="contains" style="width: 100px;">
                                    <h:outputText value="#{study.remarks}"/>
                                </p:column>
                            </p:dataTable>
<h:commandButton id="btnAddStudy" title="Add Study" value="Add Study">
                                <f:ajax  render=":content" execute="form" listener="# {studyBean.navigate}"/>
                                <f:param name="action" value="add"/>
                            </h:commandButton>
                        </p:tab>
                    </p:accordionPanel>
                </h:form>

So as you can see is my <h:panelGroup> outside my form.

This is the included page (as example)

    <h:body>
    <ui:composition template="../template/template.xhtml">
        <ui:define name="body">
            <f:view contentType="text/html">
                <ui:composition>
                    <h:form id="studyForm" prependId="false">
                        <p:messages />
                        <h:message for="startDate"/>
                        <h:message for="endDate"/>
                        <h:message for="school"/>
                        <h:message for="diploma1"/>
                        <h:message for="diploma2"/>
                        <h:message for="diploma3"/>
                        <h:message for="specialization1"/>
                        <h:message for="specialization2"/>
                        <h:message for="specialization3"/>
                        <h:message for="remarks1"/>
                        <h:message for="remarks2"/>
                        <h:message for="remarks3"/>
                        <h:outputText class="label" value="Start date: "/>
                        <h:inputText id="startDate" styleClass="inputtext validate[required,custom[date]]"
                                     value="#{studyBean.startDate}">
                            <f:convertDateTime pattern="dd/MM/yyyy" timeZone="CET"/>
                        </h:inputText>
                        <h:outputText class="label" value="End date: "/>
                        <h:inputText id="endDate" styleClass="inputtext validate[required,custom[date]]"
                                     value="#{studyBean.endDate}">
                            <f:convertDateTime pattern="dd/MM/yyyy" timeZone="CET"/>
                        </h:inputText>
                        <h:outputText class="label" value="school: "/>
                        <h:inputText id="school" styleClass="inputtext validate[required, maxSize[5]]"
                                     value="#{studyBean.school}"/>
                        <h:outputText class="label" value="Specialization: "/>
                        <h:inputText id="specialization1" styleClass="inputtext validate[maxSize[50]]"
                                     value="#{studyBean.specialization[0]}"/>
                        <h:inputText id="specialization2" styleClass="inputtext validate[maxSize[50]]"
                                     value="#{studyBean.specialization[1]}"/>
                        <h:inputText id="specialization3" styleClass="inputtext validate[maxSize[50]]"
                                     value="#{studyBean.specialization[2]}"/>
                        <h:outputText class="label" value="Diploma: "/>
                        <h:inputText id="diploma1" styleClass="inputtext validate[maxSize[50]]"
                                     value="#{studyBean.diploma[0]}"/>
                        <h:inputText id="diploma2" styleClass="inputtext validate[maxSize[50]]"
                                     value="#{studyBean.diploma[1]}"/>
                        <h:inputText id="diploma3" styleClass="inputtext validate[maxSize[50]]"
                                     value="#{studyBean.diploma[2]}"/>
                        <h:outputText class="label" value="remarks: "/>
                        <h:inputText id="remarks1" styleClass="inputtext validate[maxSize[200]]"
                                     value="#{studyBean.remarks[0]}"/>
                        <h:inputText id="remarks2" styleClass="inputtext validate[maxSize[200]]"
                                     value="#{studyBean.remarks[1]}"/>
                        <h:inputText id="remarks3" styleClass="inputtext validate[maxSize[200]]"
                                     value="#{studyBean.remarks[2]}"/>
                        <h:commandButton action="cancel" value="cancel" immediate="true"/>
                        <p:commandButton id="btnAddStudyDetail" value="add study" actionListener="#{studyBean.addStudy}"
                                         update="studyTable" oncomplete="clearAddStudy()"/>
                    </h:form>
                </ui:composition>
            </f:view>
        </ui:define>
    </ui:composition>
</h:body>

Managed Bean @RequestScoped

@ManagedBean
@RequestScoped
public class StudyBean {

public String navigate() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        try {

            action = (((HttpServletRequest) facesContext.getExternalContext().getRequest()).getParameter("action"));
            if (action.equals("add")) {
                //vull de velden
                includePage = "studyDetail.xhtml";
            }
            facesContext = FacesContext.getCurrentInstance();
            UIViewRoot root = facesContext.getViewRoot();
            UIComponent form = root.findComponent("form");
            dataTable = form.findComponent("studyTable");
            System.out.println(dataTable);
            System.out.println(root);
        } catch (Exception e) {
            facesContext.addMessage("form", new FacesMessage("You must select a study."));
        }
        return null;
    }
}

Thanks in advance.

UPDATE Now I am able to dynamically load the page in my <ui:include> but I have another problem now, when the page is loaded and I want to click on my button in the loaded page the action to which it is bound doesn't execute. So I don't get into my backing bean, I already tried to put the scope of my bean to @ViewScoped but no success.

Please help me as I am getting desperate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜