导航到传递参数的其他视图 [英] Navigate to other view passing a parameter
问题描述
我当前的环境是 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 永远不会到达处理详细信息页面的 bean (EntityDetailsMB
).详情页如下:
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/>
元素专用于捕获从索引页发送的参数并将其转发到id
属性中EntityDetailsMB
,其中有以下内容:
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);
}
另见:
这篇关于导航到传递参数的其他视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!