2017-11-16 12 views
0

「ドキュメント」エンティティ(JSF2とHibernateを使用)の基本的な編集フォームがありますが、「作成」フォームは正常に機能しますが、「編集」は機能しません。したがって、編集フォームは現在のドキュメント値を表示しますが、新しい値を送信すると、バックビーンはそれを取得せず、古いエンティティのように機能します。私edit.xhtmlのJSFの更新ビューでBeanが変更されない

パーツ:私の文書Beanの

<ui:param name="doc" value="#{document.getDocument()}" /> 

<h:form id="editForm"> 
    <h:inputHidden name="ID" id="ID" value="#{doc.ID}" /> 

    <div class="input-group"> 
     <span class="input-group-label">Title *</span> 
     <h:inputText type="text" class="input-group-field" size="30" name="title" id="title" a:required="required" a:placeholder="title" value="#{doc.title}"/> 
    </div> 
    <h:message for="title"></h:message> 

    <div class="input-group"> 
     <span class="input-group-label">Subtitle</span> 
     <h:inputText type="text" class="input-group-field" size="30" name="subtitle" id="subtitle" a:placeholder="subtitle" value="#{doc.subtitle}" /> 
    </div> 
    <h:message for="subtitle"></h:message> 

    <div class="button-group"> 
     <h:button outcome="/main?faces-redirect=true" type="button" value="Cancel"></h:button> 
     <h:commandButton action="${doc.save}" value="Save"> 
      <f:ajax render="@form" execute=":editForm" event="keyup"/> 
     </h:commandButton> 
    </div> 

</h:form> 

パーツ:

@ManagedBean 
@Entity 
@ViewScoped 
public class document implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name = "ID") 
    private int ID; 
    @NotNull 
    private String title; 
    private String subtitle; 

    //Getters Setters.... 

    @Transient 
    public document getDocument() { 
     documentDAO = new DocumentDAO(); 
     Optional<document> doc = documentDAO.read(this.ID); 
     if(doc.isPresent()) {   
      return doc.get(); 
     }else { 
      return null; 
     } 
    } 

    public String save() { 
     documentDAO = new DocumentDAO(); 
     System.out.println("Saving Document (" + this.getTitle() + ")..."); 
     documentDAO.update(this); 
     if(this.error == null || this.error.isEmpty()) 
       return "/list?faces-redirect=true"; 
     else 
      return "edit";    
    } 

私は、文書のタイトルを編集する場合、 "testok" により、例えば "テスト" でした、 "testok"がpost htmlリクエストで送信されていることがわかりましたが、フォームを保存して、システムに "Document(testok)"を保存するのではなく、 "Save Document(test)ナビゲータのWeb開発パネルを使用して)。 フォームの中にフォームがありません。

答えて

0

私は、編集のためにBeanのコピーを使用して解決策を見つけ、それをビュー・パラメータにロードして、前のリスト・ページからIDを取り出します。私の文書豆で

<f:metadata> 
    <f:viewParam name="id" value="#{document.ID}"/> 
    <f:viewAction action="#{document.getDocument()}" /> 
</f:metadata> 

:私のedit.xhtmlのヘッダで

@Transient 
public void getDocument() { 
    documentDAO = new DocumentDAO(); 
    Optional<document> doc = documentDAO.read(this.ID); 
    if(doc.isPresent()) {   
     document d = doc.get();   
     this.title = d.title; 
     this.subtitle = d.subtitle; 
    } 
} 

そして今、それが正常に動作し、それはBeanを更新します。メンバーの変数をすべてコピーするのはちょっと気が間違いです。多分もっと良い方法があります。

関連する問題