Navigate to other view passing a parameter

笑着哭i 提交于 2019-11-30 19:03:45

问题


My current environment is JRE 1.7, JSF 2.2, Eclipse Luna. In a certain page (entity_index.xhtml) of my application I have the following (PrimeFaces) button:

<p:commandButton value="Details" action="entity_details"
                 ajax="false" onclick="this.form.target='_blank'">
    <f:param name="id" value="#{entity.id}" />
</p:commandButton>

The idea is to provide a button so the user can click it and some details on the current entity will be shown in another browser tab (page entity_details.xhtml). This is one button of many, so the entity_index.xhtml page shows many instances of Entity, each one with a details button.

The button works in the sense a new tab opens and that the correct page (entity_details.xhtml) is shown, but the entity id never gets to the bean that handles the detail page (EntityDetailsMB). The details page is as follows:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:dsi="http://www.cce.ufpr.br"
    template="/private/template/sbadmin.xhtml">
<f:metadata>
    <f:viewParam name="id" value="#{entityDetailsMB.id}"/>
</f:metadata>

<ui:define name="content">
<h2 class="page-header">#{entityDetailsMB.entity.name}</h2>
<h:form id="form">
...
</ui:composition>

Notice that there is a <f:metadata/> element dedicated to capture the parameter sent from the index page and forward it to the id property in EntityDetailsMB, where there is the following:

public Entity getEntity() {
    return entityById(id);
}

public Long getId() {
    return id;
}

public void setId(Long value) {
    id = value;
}

Because the setId() method is never called, getEntity() always returns null.

What is missing to make it to work?


回答1:


p:commandButton performs a POST request. You want simply to GET a view with your entity detail, not to POST the server, so you need a h:link:

<h:link value="Details" outcome="entity_details" target="_blank">
    <f:param name="id" value="#{entity.id}" />
</h:link>

Then, the f:viewParam in the destination page will be able to process the url parameter:

<f:metadata>
    <f:viewParam name="id" value="#{entityDetailsMB.id}"/>
    <f:viewAction action="#{entityDetailsMB.init}" />
</f:metadata>

Use a f:viewAction to initialize your entity instead of doing it in a getter, which is discouraged:

public void init(){
    entity = entityById(id);
}

See also:

  • Choosing how to pass parameters to a target bean/page using JSF
  • When to use f:viewAction / preRenderView versus PostConstruct?


来源:https://stackoverflow.com/questions/38997418/navigate-to-other-view-passing-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!