2013-02-23 81 views
1

我有一個問題,我有以下幾點:JSF primefaces更新對話框不及格的價值豆

我可以添加角色沒有問題,但是當我需要刪除或修改其不因工作roleController.selectedRolenull。 起初,我將託管的bean作爲requestScope,而且我儘管可能會從那裏返回null,因爲每次請求都會重新創建bean。 所以我更改爲ViewScoped修復添加按鈕,以再次工作,但我仍然遇到編輯和刪除相同的問題。

發生的情況如下:我選擇一行並單擊按鈕編輯。這將正確顯示角色信息的對話框。但是當我點擊編輯時,我得到空值。 我見過幾個例子,看來我沒有做錯任何事情。但我可能會錯過一些非常基本的東西:/

任何見解將不勝感激!

至於豆我有以下幾點:

@ManagedBean 
@RequestScoped 
.... 
private Roles selectedRole = new Roles(); 
(I have the normal setter and getter) 
    public void edit() { 
     Logger.getGlobal().log(Level.INFO, "====> EDIT ROLE" + selectedRole.getRole()); 
    } 

的頁面如下ommitting用戶界面:定義和頭的東西。

<h:form id="contentView"> 
    <p:dataTable id="lstRoles" var="r" value="#{roleController.rolesList}" selectionMode="single" 
       selection="#{roleController.selectedRole}" rowKey="#{r.role}" paginator="true" 
       paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {CurrentPageReport} {RowsPerPageDropdown}" 
       rowsPerPageTemplate="10,15,50" rows="10"> 
     <p:column headerText="Role" sortBy="#{r.role}"> 
      <p:outputLabel value="#{r.role}"></p:outputLabel> 
     </p:column> 
     <p:column headerText="Description"> 
      <h:outputLabel value="#{r.description}"></h:outputLabel> 
     </p:column>     
     <f:facet name="footer"> 
      <p:commandButton value="New" icon="ui-icon-star" oncomplete="newRoleDialog.show()"></p:commandButton> 
      <p:commandButton value="Edit" icon="ui-icon-check" oncomplete="editRoleDialog.show()" update=":editRoleForm:editRolePanel"></p:commandButton> 
      <p:commandButton value="Delete" icon="ui-icon-trash"></p:commandButton> 
     </f:facet> 
    </p:dataTable> 
    <p:blockUI block="lstRoles" trigger="lstRoles"> 
     LOADING 
    </p:blockUI> 
</h:form> 

<!-- Edit User --> 
<p:dialog header="Edit User" widgetVar="editRoleDialog" resizable="false"> 
    <h:form id="editRoleForm"> 
     <p:panelGrid id="editRolePanel" columns="2"> 
      <h:outputText value="Role: "></h:outputText> 
      <h:outputText value="#{roleController.selectedRole.role}"></h:outputText> 

      <h:outputText value="Description: "></h:outputText> 
      <p:inputText value="#{roleController.selectedRole.description}" required="true"></p:inputText> 

      <f:facet name="footer"> 
       <p:commandButton value="Confirm" update=":contentView:lstRoles :growl" oncomplete="handleSubmitRequest(xhr, status, args, 'editRoleDialog','editRoleForm');" actionListener="#{roleController.edit()}"></p:commandButton> 
       <p:commandButton type="reset" value="reset"></p:commandButton> 
      </f:facet> 
     </p:panelGrid> 
    </h:form> 
</p:dialog> 

編輯:我使用Glassfish的3.1與3.5 primefaces

編輯2:所以,似乎我不能使用outputlabel。如果我改變輸入,然後我在managedbean中得到所需的值(我想這是因爲它調用setter,但我認爲它已經在選擇行時被照顧)。但我不想編輯第一個字段,因爲這是PK鍵,它在表中也用作FK。但至少知道我知道發生了什麼,或多或少。

回答

1

您需要設置從數據表到managedbean的selectedrole屬性選擇的角色,試試這個:

<p:commandButton value="Edit" icon="ui-icon-check" oncomplete="editRoleDialog.show()" update=":editRoleForm:editRolePanel"> 
    <f:setPropertyActionListener target="#{roleController.selectedRole}" value="#{r}"/> 
</p:commandButton> 

你很可能需要作出豆ViewScoped了。

編輯:我不知道datatable的選擇功能,以澄清,如果你使用,你不需要上述代碼。

+0

感謝,但不工作,要麼:/如果我改變bean來viewscoped全部停止工作,我想我會需要添加額外的如果我離開的RequestScope和屬性它總是指向第一個記錄,並且該bean仍爲空。 我會嘗試一切轉換成ViewScope和看到的結果!謝謝 – 2013-02-23 15:24:36

+1

仔細查看代碼。 OP正在使用''。在編輯對話框中打開數據工作正常。 OP在抱怨說在提交編輯對話框時它不再可用。 – BalusC 2013-02-23 17:10:21

-1

嘗試valueChangeListener在inputText的屬性。您通常很難在@SessionScope中擁有實體的一部分。它確保的工作,如果u使用

<inputText value="#{bean.value}"/> 

但如果u使用

<inputText value="#{bean.entity.value}"/> 

可能無法正常工作。使用valueChangeListener並創建一個像這樣的方法可以強制爲您的實體保存值。

public void saveValue(ValueChangeEvent event) { 
     Integer newValue = (Integer) event.getNewValue();//u can use getOldValue to get value before 
     entity.setValue(newValue); 
    } 

Goodluck!