2012-01-07 52 views
2

我想实现一个复合组件,它将显示数组列表中的注释。JSF facelet验证条件为真

<cc:interface> 
    <cc:attribute name="value" type="java.util.List" required="true" shortDescription="The list of objects that should be displayed"/> 
</cc:interface> 

<cc:implementation> 

    <ui:repeat var="comment" value="#{cc.attrs.commentList}"> 
     <div class = "comment-block"> 
      <h3>#{comment.title}</h3> 
      <h4>#{comment.author}</h4> 
      <p>#{comment.body}</p> 
      <h:link outcome = "editComment?id={comment.id}" value = "edit" /> 
     </div> 
    </ui:repeat> 

</cc:implementation> 

现在的问题是,只有当记录的用户是评论的作者时才应该显示标签。通常我会这样做:

<c:if test = "${comment.authId == authUser.id}"> 
    <a href = "editComment.jsp?id=${comment.id}"> 
</c:if> 

我该如何在JSF中做这样的事情?

回答

3

大多数JSF组件都具有属性rendered,您可以在其中放置返回true或false的EL表达式。根据返回值,组件将被渲染或不渲染。在你的情况下,你可以试试这个:

<h:link rendered="${comment.authId == authUser.id}" 
     outcome = "editComment" value = "edit"> 
    <f:param name="id" value="#{comment.id}" /> 
<h:link> 
+1

实际上所有的JSF组件都有这个属性,因为它是在javax.faces.component.UIComponent中定义的。 – 2012-01-08 06:35:10

+0

我想,如果我想为不同的url参数显示不同的组件,我应该使用相同的方法吗?例如:对于mysite.com/user.xhtml?action=view或mysite.com/user.xhtml?action=edit – TGM 2012-01-08 10:00:09

+0

@TGM我刚刚更新了我的答案,使用''标签。将来,您可以将上述代码重新用于不同的网址。您只需修改“结果”和“”的列表以符合您的要求。 – 2012-01-08 10:30:46