导航到其他传递参数的视图 [英] Navigate to other view passing a parameter

查看:139
本文介绍了导航到其他传递参数的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的环境是JRE 1.7,JSF 2.2,Eclipse Luna.在我的应用程序的特定页面(entity_index.xhtml)中,具有以下(PrimeFaces)按钮:

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>

这个想法是提供一个按钮,以便用户可以单击它,并且当前实体的一些详细信息将显示在另一个浏览器选项卡中(页面entity_details.xhtml).这是很多按钮中的一个,因此entity_index.xhtml页显示了许多Entity实例,每个实例都有一个详细信息按钮.

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.

从某种意义上说,该按钮可以正常工作,即会打开一个新选项卡,并显示正确的页面(entity_details.xhtml),但是实体ID永远不会到达处理详细信息页面(EntityDetailsMB)的bean.详细信息页面如下:

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>

请注意,有一个<f:metadata/>元素专用于捕获从索引页发送的参数,并将其转发到EntityDetailsMB中的id属性,其中包含以下内容:

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;
}

由于从未调用setId()方法,因此getEntity()始终返回null.

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

要使它正常工作还缺少什么?

What is missing to make it to work?

推荐答案

p:commandButton执行 POST 请求.您只想 GET 一个包含您的实体详细信息的视图,而不是 POST 服务器,因此您需要一个h:link:

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>

然后,目标页面中的f:viewParam将能够处理url参数:

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>

使用f:viewAction来初始化您的实体,而不是使用getter进行初始化,不鼓励使用:

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

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

另请参见:

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

这篇关于导航到其他传递参数的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆