2012-02-27 18 views
2

我有一個重複的形式是這樣的:重複textarea形式 - 如何獲得變量?

<ui:repeat var="blogPost" value="#{blogPosts}"> 
    <h:form> 
     <div class="full"> 
      <label for="newcomment">write a comment<br/></label> 
      <h:inputTextarea value="#{commentController.commentText}" id="commentText" rows="10" cols="40" /> 
     </div> 
     <div class="full"> 
      <h:commandButton action="#{commentController.setComment()}" value="write comment">  
       <f:setPropertyActionListener target="#{commentController.blogPostId}" value="#{blogPost.id}" /> 
      </h:commandButton> 
    </h:form> 
</ui:repeat> 

我發現這個Post形式BalusC - 幫了我很多,所以我現在已經在blogPostID在我的控制器!

但問題是:
表單重複1 - x次!

所以,如果我有呈現3種形式,所有文字區域使用相同的commentController.commentText

所以,如果我寫的第一textarea的東西,第二和第三被刪除commentText在控制器中。
如果我把東西放入第三個textarea,它的工作原理!

任何想法如何解決這個問題?
在此先感謝!

p.s.我也想有<ui:repeat><h:form> - 沒有什麼區別

這裏是類:

控制器:

@ManagedBean(name="commentController") 
@RequestScoped 
public class CommentController { 

@EJB 
private CommentProvider commentProvider; 

private String commentText; 

private List<String> commentTextListToWrite; 

private Comment comment; 

private Integer blogPostId; 




@PostConstruct 
protected void init(){ 
    comment = new Comment(); 
    commentTextListToWrite = new ArrayList<String>(); 
} 

public void setComment(){ 
    int count = 0; 
    for (String ct : commentTextListToWrite) { 
     System.out.println("test " + count + ct); 
     count++; 
    } 

} 

/** 
* Returns the commentText. 
* 
* @return the commentText. 
*/ 
public String getCommentText() { 
    return commentText; 
} 

/** 
* Sets the commentText. 
* 
* @param commentText the commentText to set. 
*/ 
public void setCommentText(String commentText) { 
    this.commentText = commentText; 
} 

public void delete(Comment comment){ 
    commentProvider.delete(comment); 
} 

/** 
* Returns the blogPostId. 
* 
* @return the blogPostId. 
*/ 
public Integer getBlogPostId() { 
    return blogPostId; 
} 

/** 
* Sets the blogPostId. 
* 
* @param blogPostId the blogPostId to set. 
*/ 
public void setBlogPostId(Integer blogPostId) { 
    this.blogPostId = blogPostId; 
} 

/** 
* Returns the commentTextListToWrite. 
* 
* @return the commentTextListToWrite. 
*/ 
public List<String> getCommentTextListToWrite() { 
    return commentTextListToWrite; 
} 

/** 
* Sets the commentTextListToWrite. 
* 
* @param commentTextListToWrite the commentTextListToWrite to set. 
*/ 
public void setCommentTextListToWrite(List<String> commentTextListToWrite) { 
    this.commentTextListToWrite = commentTextListToWrite; 
} 

} 

的index.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:s="http://jboss.com/products/seam/taglib" 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    template="/WEB-INF/templates/default.xhtml"> 

    <ui:define name="content"> 


      <h2>Show all BlogPosts</h2> 

      <ui:repeat var="blogPost" value="#{blogPosts}" varStatus="loop" > 
     <h:form>  
       <div class="posting"> 
        <h3>#{blogPost.headline}</h3> 
        <div class="posts">#{blogPost.blogPost}</div> 
        <div class="posts"> 
          vom #{blogPost.date} mit ID: #{blogPost.id}<br /> 
         <h:commandButton value="delete" action="#{blogPostController.delete(blogPost)}" /> 
         </div> 
        </div> 


       <ui:include src="pages/components/_write-comment.xhtml"> 
        <ui:param name="blogPostId" value="#{blogPost.id}" /> 
        <ui:param name="loopCount" value="#{loop.index}" /> 
       </ui:include> 


       </h:form> 
      </ui:repeat> 
     </ui:define> 
</ui:composition> 

_write- comment.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html"> 


      <div class="full"> 
       <span class="half"> 
        <label for="newcomment">write comment</label><br/> 
        <h:outputLabel for="commentText" value="loop index #{loopCount}" /> 
         <h:inputTextarea value="#{commentController.commentText[loopCount]}" id="commentText" rows="10" cols="40"> 
        </h:inputTextarea> 
       </span> 
       <span class="quarter error"> 
        <h:message id="newcomment_error" for="newcomment" value="fehler" /> 
       </span> 
      </div> 
      <div class="full"> 
       <h:commandButton action="#{commentController.setComment()}" value="write comment"> 

       </h:commandButton> 
      </div> 

</ui:composition> 

是列表的Setter是否正確?

存在(目前)沒有真正的錯誤,但系統出故障,因爲該列表是空(System.out的只是站在其他功能...它只是用於測試!)

+0

有沒有任何機會,不是在 Joerg 2012-02-27 11:14:08

+0

setter被正確聲明。我用可能的解決方案更新了我的答案。 – Ionut 2012-02-27 19:00:42

回答

3

燦您也請發佈Managed Bean代碼。 從我所看到的commentText屬性commentController是一個簡單的String而不是List<String>。當你有三個文本區域時,你必須有三個不同的變量來存儲textarea的內容。這就是爲什麼你需要一個列表。

Here你可以閱讀一些更多的細節,因爲我有和你一樣的問題。

知道所有你的代碼應該是這樣的:

託管Bean:

private List<String> commentText; 
// get& set 

的facelet:

<ui:repeat var="blogPost" value="#{blogPosts}" varStatus = "loop"> 
<h:form> 
    <div class="full"> 
     <label for="newComment" value = "Write a comment" /> 
     <h:inputTextarea id = "newComment" value="#{commentController.commentText[loop.index]}" rows="10" cols="40" /> 
    </div> 
    <div class="full"> 
     <h:commandButton action="#{commentController.setComment()}" value="write comment" /> 
    </div>   
</h:form> 

一些其他指針:

  • 在標籤標籤中避免了<br />
  • 注意textarea id屬性。它應該與標籤所鏈接的相同。如果沒有,您將收到一些視圖警告,因爲框架將無法找到您指定的ID。不要擔心ui:repeat循環中的ID相同。如果你分析HTML代碼,你會看到SF處理這些ID並且它們有所不同。

LE:

我認爲你有這個問題的原因是您爲List<String>分配的內存,但列表中不包含任何字符串。該框架想要將值放入List,但沒有String對象要設置。

爲了解決這個問題,在CommentController你有需要的空字符串添加到列表:

@PostConstruct 
protected void init(){ 
    comment = new Comment(); 
    commentTextListToWrite = new ArrayList<String>(); 

    for (int i = 0; i < blogPosts.size(); i++){ 
      commentTextListToWrite.add(new String()); 
    } 
} 

blogPosts.size()是相關博客文章屬性您所使用的<ui:repeat>標籤。我可以想象,您也可以在CommentController中訪問它。這樣,每次實例化CommentController實體時,commentTextListToWrite都將被實例化,並將包含將在視圖上完成的所需數量的空字符串。

+0

您好lonut,thx您的回覆。你是對的,它只是一個'字符串'而不是'列表''但如果我嘗試設置它,如你所說,它不工作... 我有這3個文件: index.html (在第32行我包括_write-comment.xhtml)http://pastebin.com/6k8ZDR5A _write-comment.xhtml http://pastebin.com/KXGAeuSi CommentController.java http://pastebin.com/AUrCc0MS 希望這有助於? 我明白你的意思,但它不是做什麼工作的? 我做一個對每個通過列表 commentTextListToWrite 但它是空的:( – Joerg 2012-02-27 17:09:50

+0

@Joerg能否請您提供關於你所得到的錯誤的詳細信息你可以更新這個問題併發布HTML代碼嗎? – Ionut 2012-02-27 17:43:54

+0

@lonut:好的,我編輯了原來的...也許我的setter在這種情況下是錯誤的? – Joerg 2012-02-27 18:34:31