jsf - <h :link> vs <h:commandLink> in <h:dataTable>

标签 jsf jsf-2 hyperlink datatable commandlink

当我想查看我的书的详细信息页面时,我将获取我的书并将其重定向到我的概述页面。现在当我使用<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> .

另请参阅:


也就是说,您想要解决的真正问题在这里得到了解答: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/

相关文章:

jsf - 识别和解决javax.el.PropertyNotFoundException:目标不可访问

jsf - 在 Servlet 2.4 容器上运行 JSF 2.0

xml - 创建 RSS 提要 JSF 2.0?

ruby - 在 axlsx 中设置超链接文本颜色

javascript - p :menuitem onclick update not working

java - 如何更改我的导航栏的颜色

jsf - 在 JSF 中单击按钮异步调用长时间运行的进程

ajax - UI 中的组件 ID :repeat problems

html - 列表中的某些超链接不可点击

html - 使用 FOXIT(或其他)从 HTML 代码链接到 PDF 中的特定书签/页面