2012-02-27 17 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