2013-04-26 29 views
6

Tôi có biểu mẫu trong một thành phần p:dialog đang được gửi qua AJAX. Biểu mẫu trong hộp thoại thông qua một số ui:include của một số khác .xhtml chỉ chứa biểu mẫu và các thành phần của biểu mẫu (không, tôi không làm tổ các biểu mẫu; chính hộp thoại đó không nằm trong bất kỳ biểu mẫu nào). Đậu sao của trang được bao gồm là 'ViewScoped'.Xóa biểu mẫu trong p: hộp thoại khi đóng sau khi gọi hành động/phương thức không thành công

điều MOST hoạt động tốt:

  • On xác nhận thành công, hình thức sẽ thực hiện các phương pháp tiết kiệm; sau khi lưu thành công, hộp thoại sẽ đóng; mở một bản ghi tiếp theo cho thấy thông tin hồ sơ thích hợp từ cơ sở dữ liệu.
  • Khi xác thực không thành công, các giá trị đã gửi vẫn tồn tại và hộp thoại vẫn mở và hiển thị lỗi xác thực, sửa các trường không hợp lệ và gửi lại kết quả khi lưu thành công.

Sự cố xảy ra khi action gọi là p:commandButton không thành công. Trong trường hợp không thành công, hộp thoại vẫn mở và hiển thị "Lỗi lưu thay đổi" qua h:messages cho người dùng; ngoài ra, các giá trị đã gửi vẫn tồn tại trong các trường nhập. Điều này là tốt (và thậm chí là mong muốn) trong khi biểu mẫu vẫn mở, tuy nhiên-- khi đóng hộp thoại và mở lại nó, các giá trị đã gửi là STILL trong các trường văn bản. Điều này là xấu bởi vì nó mang lại cho người dùng ấn tượng rằng việc lưu đã thành công (vì thành phần h:messages hiện bị trống do cập nhật hộp thoại).

Cho đến nay tôi đã cố gắng sau, NONE trong số đó đã khắc phục vấn đề này:

  • 1,1) Thiết lập đậu ủng hộ của các hình thức bao gồm một trường hợp mới của đậu trên mở hộp thoại.
  • 1.2) Kể từ khi đậu bây giờ là một trường hợp mới, tôi cũng repopulating đối tượng địa chỉ với các chi tiết từ một bean dữ liệu.
  • 2.1) Tắt nút đóng trên hộp thoại và thêm nút p:commandButton "Hủy" với thành phần p:resetInput trong để đóng hộp thoại/đặt lại biểu mẫu. (hộp thoại đóng, nhưng gửi giá trị trong biểu mẫu vẫn tồn tại).
  • 2.2) Gọi phương thức từ số p:commandButton này để xóa RequestMap cho bean sao lưu của biểu mẫu được bao gồm.
  • 2.3) Đã thử với nút được đặt thành cả hai immediate=trueimmediate=false.
  • 2.4) Đã thử với nút được đặt thành [email protected], một chiến thuật tương tự mà tôi đã sử dụng để đóng biểu mẫu để đảm bảo rằng nó sẽ có các trường tươi khi mở lại.

Bạn sẽ nghĩ giữa việc thiết lập bean sao lưu sang một thể hiện mới và tái tạo lại đối tượng địa chỉ tôi sẽ thấy một biểu mẫu mới, nhưng NOPE.

Dưới đây là một số các nguồn:

Phần của mainform.xhtml

<p:dialog id="dlgAddress" 
       header="Address Edit" 
       widgetVar="dialogAddress" 
       dynamic="true" 
       modal="false" 
       closable="false" 
       resizable="false" 
       styleClass="dlgAddress" 
       visible="#{addressform.showDlgAddress}"> 
     <ui:include src="addressform.xhtml"/> 
    </p:dialog> 

addressform.xhtml

<h:form id="fDlgAddress"> 
    <div id="addresses-container"> 
     <h:messages id="msgDlgAddress" errorClass="errormsg" infoClass="infomsg1" layout="table"/> 
     <h:panelGrid columns="1" 
        styleClass="pgAddresses" 
        rendered="#{!addressform.address.exists}"> 
      <h:outputText value="No active #{addressform.address.atypCode} address found." 
          styleClass="record-not-exists"/> 
      <h:outputText value="Use the form below to create one."/> 
     </h:panelGrid> 
     <p:panelGrid columns="2" 
        styleClass="pgDlgForm pgAddresses"> 
      <h:outputLabel value="Address Type"/> 
      <h:inputText id="atypCode1" 
         value="#{addressform.address.atypCode}" 
         disabled="true" 
         rendered="#{addressform.address.exists}"/> 
      <h:selectOneMenu id="atypCode2" 
          value="#{addressform.address.atypCode}" 
          rendered="#{!addressform.address.exists}"> 
       <f:selectItems value="#{addressform.atypCodeList}" 
           var="atyp" 
           itemValue="#{atyp}" 
           itemLabel="#{atyp}"/> 
      </h:selectOneMenu> 
      <h:outputLabel value="Street 1" 
          for="street1"/> 
      <h:inputText id="street1" 
         value="#{addressform.address.addressLine1}"/> 
      <h:outputLabel value="Street 2" 
          for="street2"/> 
      <h:inputText id="street2" 
         value="#{addressform.address.addressLine2}"/> 
      <h:outputLabel value="Street 3" 
          for="street3"/> 
      <h:inputText id="street3" 
         value="#{addressform.address.addressLine3}"/> 
      <h:outputLabel value="Street 4" 
          for="street4"/> 
      <h:inputText id="street4" 
         value="#{addressform.address.addressLine4}"/> 
      <h:outputLabel value="City" 
          for="city"/> 
      <h:inputText id="city" 
         value="#{addressform.address.city}" 
         required="true" 
         requiredMessage="Please enter a city."/> 
      <h:outputLabel value="State" 
          for="statCode"/> 
      <h:selectOneMenu id="statCode" 
          value="#{addressform.address.stateCode}"> 
       <f:selectItem itemLabel="Select State/Province" 
           itemValue=""/> 
       <f:selectItems value="#{states.statesList}" 
           var="stat" 
           itemValue="#{stat.statCode}" 
           itemLabel="#{stat.statDesc}"/> 
      </h:selectOneMenu> 
      <h:outputLabel value="Zip Code" 
          for="zipCode"/> 
      <h:inputText id="zipCode" 
         value="#{addressform.address.zip}"/> 
      <h:outputLabel value="Country" 
          for="natnCode"/> 
      <h:selectOneMenu id="natnCode" 
          value="#{addressform.address.nationCode}" 
          required="true" 
          requiredMessage="Please choose a nation."> 
       <f:selectItem itemLabel="Select Country" 
           itemValue=""/> 
       <f:selectItems value="#{nations.nationsList}" 
           var="natn" 
           itemValue="#{natn.natnCode}" 
           itemLabel="#{natn.natnDesc}"/> 
      </h:selectOneMenu> 
      <h:outputLabel value="From Date" 
          for="fromDate"/> 
      <p:calendar id="fromDate" 
         value="#{addressform.address.fromDate}" 
         showButtonPanel="true"/> 
      <h:outputLabel value="To Date" 
          for="toDate"/> 
      <p:calendar id="toDate" 
         value="#{addressform.address.toDate}" 
         showButtonPanel="true"/> 
      <h:outputLabel value="Inactivate" 
          for="inactivateAddress" 
          rendered="#{addressform.address.exists}"/> 
      <h:selectBooleanCheckbox id="inactivateAddress" 
            value="#{addressform.address.inactivate}" 
            rendered="#{addressform.address.exists}"/> 
      <h:outputLabel value="Delete" 
          for="deleteAddress" 
          rendered="#{addressform.address.exists}"/> 
      <h:selectBooleanCheckbox id="deleteAddress" 
            value="#{addressform.address.delete}" 
            rendered="#{addressform.address.exists}"/> 
     </p:panelGrid> 
    </div> 
    <div class="button-container"> 
     <p:commandButton value="Save Changes" 
         action="#{addressform.save}" 
         type="submit" 
         ajax="true" 
         process="@form" 
         update="@form"/> 
     <p:commandButton value="Cancel" 
         process="@this" 
         onclick="dialogAddress.hide();"> 
      <p:resetInput target="fDlgAddress"/> 
     </p:commandButton> 
    </div> 
</h:form> 

Recorddetailsform (sao lưu đậu cho hình thức mà từ đó hộp thoại địa chỉ được gọi là)

public void showDlgAddress(){ 
     FacesContext ctx = FacesContext.getCurrentInstance(); 
     ELResolver resolver = ctx.getApplication().getELResolver(); 
     RecordDetails recordDetails = (RecordDetails) resolver.getValue(ctx.getELContext(), null, "recordDetails"); 

     //Set addressform backing bean to new instance and load address into it from recordDetails 
     resolver.setValue(ctx.getELContext(), null, "addressform", new Addressform()); 
     Addressform addressform = (Addressform) resolver.getValue(ctx.getELContext(), null, "addressform"); 
     addressform.setAddress(recordDetails.getAddress()); 
    } 

Addressform (mẫu địa chỉ của đậu ủng hộ)

public void save() { 
    FacesContext ctx = FacesContext.getCurrentInstance(); 
    RequestContext rctx = RequestContext.getCurrentInstance(); 
    ELResolver resolver = ctx.getApplication().getELResolver(); 
    Records records = (Records) resolver.getValue(ctx.getELContext(), null, "records"); 
    RecordDetails recordDetails = (RecordDetails) resolver.getValue(ctx.getELContext(), null, "recordDetails"); 
    Mainform mainform = (Mainform) resolver.getValue(ctx.getELContext(), null, "mainform"); 
    Person person = (Person) resolver.getValue(ctx.getELContext(), null, "person"); 

    //Pretty lengthy SQL stuff here. Commented out. Just wanted to display the display logic below for the dialog. 

    if (errorMsg != null) {//If errorMsg is not null, then error occurred. 
     showDlgAddress = true;//Ensures address dialog remains open in event of action error. 
     queueErrorMessage(errorMsg); 
     rctx.update("dlgAddress"); 
     return;//break out of method on error. 
    } else { 
     showDlgAddress = false; 
     rctx.update("dlgAddress"); 
    } 

    //If everything saves without error, repopulate address and update recordDetails dialog. 
    recordDetails.populateAddress(records.getSelectedRecord()); 
    mainform.updateDlgRecordDetails(); 
} 

Thông tin khác:

  • JSF2
  • Primefaces 3,5
  • Tomcat 6.0
  • Netbeans

Trả lời

13

Hãy thử điều này:

  1. Đặt thoại của bạn bên trong một hình thức (dialogInputForm)

  2. Add attri Bute update = ": dialogInputForm"

  3. Nest thẻ resetInput trong nút mở ra hộp thoại và xác định mục tiêu là hình thức vừa tạo

Các CommandButton:

<p:commandButton process="@this" actionListener="#{bean.prepare}" update=":dialogInputForm" oncomplete="thirdPartyDialog.show()" value="Open Input Dialog" > 
    <p:resetInput target=":dialogInputForm" /> 
</p:commandButton> 

Và hình thức liên lạc với hộp thoại

<h:form id="dialogInputForm" > 
... your dialog ... 
</h:form> 
+0

Hmmm ... Tôi có thể thử công việc này vào ngày mai, nhưng tôi nghĩ rằng tôi đã thử điều này. Kết quả là p: resetInput đã làm cho biểu mẫu xuất hiện như thể không có bản ghi nào được tải, không chỉ xóa các giá trị đã gửi "" khỏi bản ghi trước đó mà còn cả các giá trị của bản ghi mới. – mousouchop

+0

Vô cùng giải pháp này hoạt động! Tôi đã tuyệt vọng về vấn đề này của đặt lại các giá trị của hộp thoại của tôi. '' thực hiện thủ thuật. Cảm ơn rất nhiều! –

+1

Có. Tôi đã thấy rằng điều này hoạt động đáng tin cậy. Tuy nhiên tôi làm tổ của tôi hình thức trong hộp thoại của tôi, mà dường như không gây ra một vấn đề. Tôi nghĩ rằng vấn đề của tôi xuất hiện khi ban đầu đã thử sử dụng '@ form' làm đích cho' '. Bạn ** PHẢI ** sử dụng một ID biểu mẫu thực tế trong thành phần này và cũng cập nhật cùng một biểu mẫu giống như trong ví dụ của bạn ở trên. Cảm ơn. – mousouchop

Các vấn đề liên quan