Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jsf 2 - Navigate to other view passing a parameter

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...