p中的选定值:selectOneMenu丢失了

时间:2014-05-30 04:02:17

标签: jsf jsf-2 primefaces

美好的一天!

我正在使用JSF 2.1 Primefaces 4.0 Glassfish 3.2.1

在我的 Form.xhtml 中,我有这段代码。

<h:form id="modDlgFormc">
      <p:dialog id="modIDc" header="Criteria Info" 
            showEffect="fade" hideEffect="fade" modal="true"
             resizable="false" widgetVar="modDlgc" closable="false"
               position="center" visible ="#{Fame.modVisiblec}">

      <p:messages id="pmsgCrit" closable="true"/>
          <h:panelGrid id="criteriapGrid" columns="1">
             <h:panelGrid id="critLabelId" columns="2">
                  <p:selectOneMenu id="fieldNameId" value="# 
                     {Fame.fieldName}" var="fieldSelect">
                          <f:selectItem itemLabel="Select Field Name" 
                            itemValue="" /> 
                                <f:selectItems value="#{Fame.fieldNameMap}" />
                                <p:ajax event="change" update="fieldNameId  
                                  operandId" listener="#{Fame.fieldNameChange}"/>                                                    
                            </p:selectOneMenu>

        <p:selectOneMenu id="operandId" value="#{Fame.operator}" >
            <f:selectItem itemLabel="Select Operand" itemValue="" /> 
                 <f:selectItems value="#{Fame.operandMap}" /> 
                     <p:ajax event="change" update="operandId valueId"   
                         listener="#{Fame.operandChange}" />
                            </p:selectOneMenu>
                        </h:panelGrid>

             <h:panelGrid id="valueId" columns="2">
                  <c:if test="#{Fame.showValue == 'txt'}">
                       <h:outputLabel for="inputVal" value="Value : " style="font-weight: bolder"/>
                                <p:inputText id="inputVal" size="30" value="#{Fame.value}"/>

                                <c:if test="#{Fame.operator == 'Is Between'}">
                                    <h:outputText value=""/>
                                    <p:inputText size="30" value="#{Fame.value2}"/>
                                </c:if>
                            </c:if>

                            <c:if test="#{Fame.showValue == 'cal'}">
                                <h:outputLabel for="fromDateId" value="From : " style="font-weight: bolder"/>
                                <p:calendar id="fromDateId" value="#{Fame.date}" showButtonPanel="true" maxdate="#{Fame.maxDate}"/>

                                <c:if test="#{Fame.operator == 'Is Between'}">
                                    <h:outputLabel for="toDateId" value="To : " style="font-weight: bolder"/>
                                    <p:calendar id="toDateId" value="#{Fame.date2}" showButtonPanel="true" maxdate="#{Fame.maxDate}"/>
                                </c:if>
                            </c:if>

                            <c:if test="#{Fame.showValue == 'upload'}">
                                <h:outputText value=""/>
                                <p:commandButton value="UPLOAD" onclick="uconfirmation.show()" type="button" />
                            </c:if>
                        </h:panelGrid>

                        <h:panelGrid id="critBtnId" columns="4">
                            <p:commandButton id="addBtn" value="ADD" actionListener="#{Fame.saveCriteria}" update=":modDlgFormc:modIDc" rendered="#{empty Fame.editMode}" />
                            <p:commandButton value="SAVE" actionListener="#{Fame.updateCriteria}" update=":modDlgFormc:modIDc" rendered="#{not empty Fame.editMode}"/>
                            <p:commandButton value="CANCEL" actionListener="#{Fame.cancelCriteria}" update=":modDlgFormc" rendered="#{not empty Fame.editMode}"/>
                            <p:commandButton id="mCancelButtonc" value="CLOSE" update=":modDlgFormc:modIDc :wbookForm" actionListener="#{Fame.closealldialogsopen}"/>
                        </h:panelGrid>

                        <br/>
                    </h:panelGrid>
                    <center>
                        <h:panelGrid id="critDTable" columns="1">
                            <p:dataTable id="criteriaTbl" var="criteria" value="#{Fame.createdCriteria}" paginator="true" rows="5" rowIndexVar="rowIndex" paginatorPosition="bottom"
                                         paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                         currentPageReportTemplate="{totalRecords} record(s) in {totalPages} page(s)">                    

                                <p:column headerText="WORKSHEET NAME">  
                                    #{criteria.wsheetName}  
                                </p:column>
                                <p:column headerText="FIELD NAME">  
                                    #{criteria.fieldName}  
                                </p:column>
                                <p:column headerText="OPERATOR">  
                                    #{criteria.operator}  
                                </p:column>
                                <p:column headerText="VALUE">  
                                    #{criteria.value}  
                                </p:column>

                                <p:column headerText="UPDATE">
                                    <p:panelGrid>
                                        <p:row>
                                            <p:column style="vertical-align: top;border: none">
                                                <p:commandButton actionListener="#{Fame.editCri(rowIndex)}" style="width:30px;text-align:center;border:none;background-color:transparent;"  icon="ui-icon-pencil" update=":modDlgFormc" />
                                                <p:commandButton actionListener="#{Fame.deleteCri(rowIndex)}" style="width:30px;text-align:center;border:none;background-color:transparent;"  icon="ui-icon-close" update=":modDlgFormc" />
                                            </p:column>
                                        </p:row>
                                    </p:panelGrid>
                                </p:column>

                            </p:dataTable>
                        </h:panelGrid>
                    </center>

                </p:dialog>
            </h:form>

我的主要代码

@ManagedBean(name = "Fame")
@SessionScoped

private FameCriteriaDAOImpl fameCriteriaDAOImpl = new FameCriteriaDAOImpl();
private ArrayList<FameCriteriaBean> createdCriteria = new ArrayList();

public void saveCriteria() {
        fameCriteriaDAOImpl = new FameCriteriaDAOImpl();
        try {
            if (this.validate() == true) {
                //String wsheetId = fameManagementDP.getWsheetId(group_Id, workbookName);
                FameCriteriaBean bean = new FameCriteriaBean();
                bean.setWsheetId(this.worksheetId);
                bean.setFieldName(this.fieldName);
                bean.setOperator(this.operator);
                bean.setValue(this.value);

                String actionStr = "inserted";
                fameCriteriaDAOImpl.save(bean);
            }

        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
            error(converter.getConvertedError(sqlEx.getMessage()));
        } finally {
            this.initialize();
            this.initializeCreatedCriteria();
            this.resetCriteria();
        }
    }

我的 Bean 代码。

public class FameCriteriaBean {

private String wsheetName = "";
private String wsheetId = "";
private String fieldName = "";
private String operator = "";
private String value = "";


public FameCriteriaBean(){

}

/**
 * @return the fieldName
 */
public String getFieldName() {
    return fieldName;
}

/**
 * @param fieldName the fieldName to set
 */
public void setFieldName(String fieldName) {
    this.fieldName = fieldName;
}

/**
 * @return the operator
 */
public String getOperator() {
    return operator;
}

/**
 * @param operator the operator to set
 */
public void setOperator(String operator) {
    this.operator = operator;
}

/**
 * @return the value
 */
public String getValue() {
    return value;
}

/**
 * @param value the value to set
 */
public void setValue(String value) {
    this.value = value;
}

/**
 * @return the wsheetId
 */
public String getWsheetId() {
    return wsheetId;
}

/**
 * @param wsheetId the wsheetId to set
 */
public void setWsheetId(String wsheetId) {
    this.wsheetId = wsheetId;
}

/**
 * @return the wsheetName
 */
public String getWsheetName() {
    return wsheetName;
}

/**
 * @param wsheetName the wsheetName to set
 */
public void setWsheetName(String wsheetName) {
    this.wsheetName = wsheetName;
}

}

以下是该方案。

  1. 我选择了一个fieldName,然后加载了操作数列表。这是正确的。

  2. 我单击ADD按钮,然后显示p:消息,表示没有选择操作数。这是正确的。

  3. 我注意到错误出现时我选择的字段名值已经消失。

  4. 我的问题是如何不刷新fieldNameId的值,以便在p:message中显示错误后可以使用它。 我在我的java课程中使用SessionScoped是因为我在 Form.xhtml 中使用p:wizard

    请帮我解决这个问题。

    谢谢你,更多力量! :)

1 个答案:

答案 0 :(得分:1)

我做的是这个。 请参阅更新的主要

@ManagedBean(name = "Fame")
@SessionScoped

private FameCriteriaDAOImpl fameCriteriaDAOImpl = new FameCriteriaDAOImpl();
private ArrayList<FameCriteriaBean> createdCriteria = new ArrayList();

public void saveCriteria() {
        fameCriteriaDAOImpl = new FameCriteriaDAOImpl();
        if (!this.validate()) {
          return;
         }            
               try {

                FameCriteriaBean bean = new FameCriteriaBean();
                bean.setWsheetId(this.worksheetId);
                bean.setFieldName(this.fieldName);
                bean.setOperator(this.operator);
                bean.setValue(this.value);

                String actionStr = "inserted";
                fameCriteriaDAOImpl.save(bean);

        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
            error(converter.getConvertedError(sqlEx.getMessage()));
        } finally {
            this.initialize();
            this.initializeCreatedCriteria();
            this.resetCriteria();
        }
    }