Note that there are some explanatory texts on larger screens.

plurals
  1. POForm with repeating textarea - how to get the variable?
    primarykey
    data
    text
    <p>i have a repeating form like this: </p> <pre><code>&lt;ui:repeat var="blogPost" value="#{blogPosts}"&gt; &lt;h:form&gt; &lt;div class="full"&gt; &lt;label for="newcomment"&gt;write a comment&lt;br/&gt;&lt;/label&gt; &lt;h:inputTextarea value="#{commentController.commentText}" id="commentText" rows="10" cols="40" /&gt; &lt;/div&gt; &lt;div class="full"&gt; &lt;h:commandButton action="#{commentController.setComment()}" value="write comment"&gt; &lt;f:setPropertyActionListener target="#{commentController.blogPostId}" value="#{blogPost.id}" /&gt; &lt;/h:commandButton&gt; &lt;/h:form&gt; &lt;/ui:repeat&gt; </code></pre> <p>I found this <a href="https://stackoverflow.com/questions/2082958/can-i-pass-an-object-with-a-jsf-param-tag">Post</a> form BalusC - helped me a lot so i have now the blogPostID in my controller! </p> <p>But the problem is:<br> The form is repeating 1 - x times! </p> <p>So, if i have 3 forms rendered, all textareas use the same commentController.commentText</p> <p>So if i write something in the first textarea, the 2nd and 3rd are deleting the commentText in the Controller.<br> If i put something into the 3rd textarea, it works! </p> <p>Any idea how to fix tis problem?<br> thanks in advance!</p> <p>p.s. i also tried to have the <code>&lt;h:form&gt;</code> outside the <code>&lt;ui:repeat&gt;</code> - makes no difference</p> <p>Here are the classes:</p> <p>The Controller:</p> <pre><code>@ManagedBean(name="commentController") @RequestScoped public class CommentController { @EJB private CommentProvider commentProvider; private String commentText; private List&lt;String&gt; commentTextListToWrite; private Comment comment; private Integer blogPostId; @PostConstruct protected void init(){ comment = new Comment(); commentTextListToWrite = new ArrayList&lt;String&gt;(); } 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&lt;String&gt; getCommentTextListToWrite() { return commentTextListToWrite; } /** * Sets the commentTextListToWrite. * * @param commentTextListToWrite the commentTextListToWrite to set. */ public void setCommentTextListToWrite(List&lt;String&gt; commentTextListToWrite) { this.commentTextListToWrite = commentTextListToWrite; } } </code></pre> <p>index.xhtml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;ui:define name="content"&gt; &lt;h2&gt;Show all BlogPosts&lt;/h2&gt; &lt;ui:repeat var="blogPost" value="#{blogPosts}" varStatus="loop" &gt; &lt;h:form&gt; &lt;div class="posting"&gt; &lt;h3&gt;#{blogPost.headline}&lt;/h3&gt; &lt;div class="posts"&gt;#{blogPost.blogPost}&lt;/div&gt; &lt;div class="posts"&gt; vom #{blogPost.date} mit ID: #{blogPost.id}&lt;br /&gt; &lt;h:commandButton value="delete" action="#{blogPostController.delete(blogPost)}" /&gt; &lt;/div&gt; &lt;/div&gt; &lt;ui:include src="pages/components/_write-comment.xhtml"&gt; &lt;ui:param name="blogPostId" value="#{blogPost.id}" /&gt; &lt;ui:param name="loopCount" value="#{loop.index}" /&gt; &lt;/ui:include&gt; &lt;/h:form&gt; &lt;/ui:repeat&gt; &lt;/ui:define&gt; &lt;/ui:composition&gt; </code></pre> <p>_write-comment.xhtml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;div class="full"&gt; &lt;span class="half"&gt; &lt;label for="newcomment"&gt;write comment&lt;/label&gt;&lt;br/&gt; &lt;h:outputLabel for="commentText" value="loop index #{loopCount}" /&gt; &lt;h:inputTextarea value="#{commentController.commentText[loopCount]}" id="commentText" rows="10" cols="40"&gt; &lt;/h:inputTextarea&gt; &lt;/span&gt; &lt;span class="quarter error"&gt; &lt;h:message id="newcomment_error" for="newcomment" value="fehler" /&gt; &lt;/span&gt; &lt;/div&gt; &lt;div class="full"&gt; &lt;h:commandButton action="#{commentController.setComment()}" value="write comment"&gt; &lt;/h:commandButton&gt; &lt;/div&gt; &lt;/ui:composition&gt; </code></pre> <p>is the Setter of the List correct?</p> <p>there is (at the moment) no real error, but the system out fails, because the list is empty (the System.Out just stands for another function... it's just for testing!)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload