JSF:动态加载页面commandButton无法正常工作

时间:2011-05-06 20:42:13

标签: ajax jsf facelets primefaces dynamic

我有一个包含PrimeFaces手风琴面板的页面,每个面板都是PrimeFaces数据表。例如,在数据表下面是一个“添加研究”按钮,当单击此按钮时,我想使用<h:panelGroup>将页面动态加载到<ui:include>。当单击该按钮时,我想动态更改<ui:include>中的src值,以便我的页面加载到<h:panelGroup>中。现在问题如下:

我的托管bean设置为@RequestScoped,当我点击我的按钮时,没有呈现任何内容。 如果我将托管bean设置为@SessionScoped,则在第二次单击按钮后填充<h:panelGroup>,则第一次单击不会执行任何操作。

使用过的技术:JSF2.0,PrimeFaces,Facelets

以下是一些代码段:

主页

<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}">
                                        <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>

所以你可以看到我的<h:panelGroup>在我的表格之外。

这是包含的页面(例如)

    <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>

托管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;
    }
}

提前致谢。

更新 现在我能够在我的<ui:include>中动态加载页面,但我现在有另一个问题,当页面加载并且我想在加载的页面中单击我的按钮时它绑定的动作不会执行。所以我没有进入我的支持bean,我已经尝试将我的bean的范围放到@ViewScoped但是没有成功。

请帮助我,因为我变得绝望。

0 个答案:

没有答案