当我想查看我的书的详细信息页面时,我将获取我的书并将其重定向到我的概述页面。现在当我使用<h:link>
时他总是给出我的数据表的最后一项。当我使用<h:commandLink>
时一切正常。现在我的问题是,为什么 <h:link outcome="...">
不起作用?当 <h:commandLink>
中有“相同”代码时会起作用吗?
托管 Bean
@Named(value = "bookController")
@SessionScoped
public class BookController implements Serializable {
@EJB private BookRepository dao;
private LibraryBook book = new LibraryBook();
...
public String getLibraryBook(String isbn)
{
this.book = this.dao.getBook(isbn);
return "bookOverview";
}
...
}
书.xhtml
<f:view>
<h:form>
<h:dataTable value="#{bookController.books}" var="item" class="table">
<h:column>
<f:facet name="header">
<h:outputText value="Isbn"/>
</f:facet>
<h:link value="#{item.isbn}" outcome="#{bookController.getLibraryBook(item.isbn)}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{item.title}"/>
</h:column>
<h:column>
<h:commandButton value='Update' class="btn btn-warning" action="#{bookController.EditBook(item)}"></h:commandButton>
<h:commandButton value='Delete' class="btn btn-danger" action="#{bookController.deleteBook(item.isbn)}"></h:commandButton>
</h:column>
</h:dataTable>
</h:form>
</f:view>
最佳答案
因为 <h:link outcome>
是在页面呈现时评估的,而不是在您单击链接时评估的。您在 outcome
中提到的方法将 session 作用域 bean 属性设置为当前项,然后返回字符串 bookOverview
。因此,实际上,您最终会得到每个链接都有 outcome="bookOverview"
在你点击它之前。与此同时,bean 只记住了最后一项。
<h:commandLink action>
有效,因为它是在您单击链接后进行评估的。
这个差异是因为<h:link>
适用于不需要有状态的幂等 (GET) 请求 <h:form>
,以及<h:commandLink>
适用于需要有状态的非幂等 (POST) 请求 <h:form>
.
另请参阅:
- When should I use h:outputLink instead of h:commandLink?
- How to navigate in JSF? How to make URL reflect current page (and not previous one)
也就是说,您想要解决的真正问题在这里得到了解答:Creating master-detail pages for entities, how to link them and which bean scope to choose 。它展示了如何正确使用<h:link>
以及您实际应该使用哪个 bean 范围。
关于jsf - <h :link> vs <h:commandLink> in <h:dataTable>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34379813/