我有一個重複的形式是這樣的:重複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的只是站在其他功能...它只是用於測試!)
有沒有任何機會,不是在
Joerg
2012-02-27 11:14:08
setter被正確聲明。我用可能的解決方案更新了我的答案。 – Ionut 2012-02-27 19:00:42