【问题标题】:selected value in p:selectOneMenu was lostp:selectOneMenu 中的选定值丢失
【发布时间】:2014-07-19 18:06:35
【问题描述】:

美好的一天!

我正在使用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. 我选择了一个字段名,然后加载了操作数列表。这是正确的。

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

  3. 我注意到出现错误时选择的字段名值不见了。

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

请帮我解决这个问题。

谢谢你和更多的力量! :)

【问题讨论】:

  • 您在点击添加button 后更新整个dialog box。尝试仅更新您需要的特定 id,并忽略在消息消失后​​失去其价值的 id
  • 你好!谢谢回答。是的,我已经做了你的建议,是的,它的工作,但我对你的建议的问题是在我点击添加按钮后,它保存在数据表中(这是正确的)但是字段名称、操作数和值的 p:inputText 不为空.它的内容来自您输入的最后一个数据。我怎么能那样做?填写“”字段名称、操作数和值,而不更新整个对话框形式。谢谢:)
  • 在您的 add 方法中设置字符串(您在其中输入空字符串的字符串)然后您也可以更新该 id...请注意,有时更新整个表单并不总是有用您需要更改一些数据(例如在使用render 时),因此您只需要更新该 id 而不是整个表单。如果您不明白,请告诉我您要在提交表单后更改哪个字段,我将发布其相关代码并回答
  • 嗨,anas,对不起,我没有得到你的解释。您能否告诉我要更改哪些代码才能实现我的需要?非常感谢:D
  • 显示您的 bean 代码并突出显示您希望在提交表单后保持空白的字段名称

标签: jsf jsf-2 primefaces


【解决方案1】:

我做的是这个。 请查看更新的 Main

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

【讨论】:

  • 验证后,如果有错误我把return,所以它会返回。从那里它不会刷新 fieldName 的值。谢谢@anas 的回答:)
猜你喜欢
  • 2014-09-28
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多