fedel1234
fedel1234

Reputation: 25

selected value in p:selectOneMenu was lost

Good day!

I'm using JSF 2.1 Primefaces 4.0 Glassfish 3.2.1

In my Form.xhtml, I have this code.

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

My Main code

@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();
        }
    }

My Bean code.

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

}

Below is the scenario.

  1. I choose a fieldName then the lists of operand was loaded. This is correct.

  2. I click the button ADD, then a p:message appear that there is no operand selected. This is correct.

  3. I had noticed that the fieldname value that I was selected when error appears was gone.

My question is how not to refresh the value of fieldNameId so that I can use it after an error is displayed in p:message. I'm using SessionScoped for my java class because I'm using p:wizard in Form.xhtml.

Please help me to solve this.

Thank you and More Power! :)

Upvotes: 0

Views: 2144

Answers (1)

fedel1234
fedel1234

Reputation: 25

What I did is this. Please see updated Main class

@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();
        }
    }

Upvotes: 1

Related Questions