2011-12-28 10 views
0

PrimeFacesのインプレース編集機能を使用して行を編集すると、変更が適用されないようです。ここでPrimeFacesの1ページあたりの行数を変更した後のデータテーブルの奇妙な動作

は私のコードです:

properties.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:c="http://java.sun.com/jsp/jstl/core"> 

    <f:event type="javax.faces.event.PreRenderViewEvent" listener="${propertyManagedBean.preRenderView}"/> 

    <h:head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title>Properties</title> 
    </h:head> 

    <h:body> 
      <div id="content" class="left_content"> 
       <h:form> 
        <p:dataTable id="tbl" var="property" value="#{propertyManagedBean.properties}" paginator="true" rows="10" rowsPerPageTemplate="5,10,20"> 

         <p:ajax event="rowEdit" update="@this" listener="#{propertyManagedBean.propertyRowEdit}"/> 

         <f:facet name="header"> 
          Properties Manager 
         </f:facet> 

         <p:column headerText="Key" >        
          <h:outputText value="#{property.propKey}" /> 
         </p:column> 

         <p:column headerText="Value"> 
          <p:cellEditor> 
           <f:facet name="output"> 
            <h:outputText value="#{property.propValue}" /> 
           </f:facet> 
           <f:facet name="input"> 
            <p:inputText value="#{property.propValue}" style="width:100%"/> 
           </f:facet> 
          </p:cellEditor> 
         </p:column> 

         <p:column headerText="Options" style="width:15px"> 
          <p:rowEditor /> 
         </p:column> 
        </p:dataTable>  
       </h:form> 
      </div> 
    </h:body> 
</html> 

PropertyManagedBean.java

@ManagedBean 
@SessionScoped 
public class PropertyManagedBean { 

    private PropertyJpaController controller; 
    private List<Property> properties; 

    /** Creates a new instance of propertyManagedBean */ 
    public PropertyManagedBean() { 
     controller = new PropertyJpaController(); 
     extractProperties(); 
    } 

    public void propertyRowEdit(RowEditEvent event) { 
     Property prop = (Property) event.getObject(); 
     try { 
      controller.update(prop);   
     } catch (Exception ex) { 
      //TODO: Customize work with exceptions 
     } 
     extractProperties(); 
    } 

    public List<Property> getProperties() { 
     return properties; 
    } 

    public void preRenderView() { 
     HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true); 
    } 

    private void extractProperties() { 
     properties = controller.findPropertyEntities(); 
    } 
} 

問題は、ページ上の行のプロパティを編集した後に起こりました。この行を変更すると、テーブルのプロパティの古い値が取得されます。

+1

AS JBossでPrimeFaces 3rc2でところで試験しました。 –

+0

私はそれを試してみましたが、DBテーブルに何も起こりません。 – Nazar

答えて

3

私は、controller.update(prop)には表示されない例外が発生しているため、実際には更新されません。

モックサービスでコードをすばやくテストすると、期待どおりに機能します。

にfacelet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:p="http://primefaces.org/ui" 
> 

    <h:head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     <title>Properties</title> 
    </h:head> 

    <h:body> 
     <h:form> 
      <p:dataTable id="tbl" value="#{propertyManagedBean.properties}" var="property" paginator="true" rows="10" rowsPerPageTemplate="5,10,20"> 

       <p:ajax event="rowEdit" update="@this" listener="#{propertyManagedBean.propertyRowEdit}"/> 

       <f:facet name="header"> 
        Properties Manager 
       </f:facet> 

       <p:column headerText="Key" >        
        #{property.propKey} 
       </p:column> 

       <p:column headerText="Value"> 
        <p:cellEditor> 
         <f:facet name="output"> 
          #{property.propValue} 
         </f:facet> 
         <f:facet name="input"> 
          <p:inputText value="#{property.propValue}" style="width:100%"/> 
         </f:facet> 
        </p:cellEditor> 
       </p:column> 

       <p:column headerText="Options" style="width:15px"> 
        <p:rowEditor /> 
       </p:column> 
      </p:dataTable> 
     </h:form> 
    </h:body> 
</html> 

バッキングBean:

@ManagedBean 
@SessionScoped 
public class PropertyManagedBean { 

    private PropertyJpaController controller; 
    private List<Property> properties; 

    public PropertyManagedBean() { 
     controller = new PropertyJpaController(); 
     extractProperties(); 
    } 

    public void propertyRowEdit(RowEditEvent event) { 
     Property property = (Property) event.getObject(); 
     try { 
      controller.update(property); 
     } catch (Exception ex) { 
      // TODO: Customize work with exceptions 
     } 
     extractProperties(); 
    } 

    public List<Property> getProperties() { 
     return properties; 
    } 

    private void extractProperties() { 
     properties = controller.findPropertyEntities(); 
    } 
} 

モックサービス:

public class PropertyJpaController { 

    private List<Property> properties = new ArrayList<Property>(); 

    { 
     properties.add(new Property("KeyA", "ValueA")); 
     properties.add(new Property("KeyB", "ValueB")); 
    } 

    public List<Property> findPropertyEntities() { 
     // Copy list and entries to prevent direct in-memory updating 
     List<Property> newList = new ArrayList<Property>(); 
     for (Property property : properties) { 
      newList.add(new Property(property.getPropKey(), property.getPropValue())); 
     } 

     return newList; 
    } 

    public void update(Property updatedProperty) { 
     for (Property property : properties) { 
      if (property.getPropKey().equals(updatedProperty.getPropKey())) { 
       property.setPropValue(updatedProperty.getPropValue()); 
      } 
     } 
    } 
} 

モックモデル:

public class Property { 

    private String propKey; 
    private String propValue; 

    public Property(String propKey, String propValue) { 
     this.propKey = propKey; 
     this.propValue = propValue; 
    } 

    public String getPropKey() { 
     return propKey; 
    } 

    public void setPropKey(String propKey) { 
     this.propKey = propKey; 
    } 

    public String getPropValue() { 
     return propValue; 
    } 

    public void setPropValue(String propValue) { 
     this.propValue = propValue; 
    } 
} 

preRenderViewイベントリスナーはむしろ不要だったことに注意してください。また、通常、バッキング・ビーンズは@SessionScopedにはしませんが、モック・ケースの場合は、実際に変更が「永続化」したことをテストするのに便利です。

これは私がこれの原因はよく分からないが、あなたが `` ラインを必要としない7

+0

助けてくれてありがとう! – Nazar

+0

あなたは大歓迎です! :) –

0

Primeface dataTableには、rowEditListenerという属性があります。内部の<p:ajax>コンポーネントを使用する代わりに、この属性を使用する必要があります。これにより、更新の問題が解決される可能性があります。

<p:dataTable ... rowEditListener="#{propertyManagedBean.propertyRowEdit}"> 

EDIT:それは、この属性は、もはやユーザーガイドごとPrimefaces 3.0に存在していることを表示されません。私の答えは無視してください。でも、これはPrimeface 2.2.1にも当てはまり、他の人に恩恵を受ける可能性があるので、私はそのまま残します。

+0

NetBeansの "rowEditListenerが不明な属性です"というメッセージ – Nazar

+0

@ user1119046うーん... Primefaces 3.0でこの属性を使用したようです。 –

関連する問題