java JSF - 重定向到页面并记住用于搜索的参数 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7899518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:53:46  来源:igfitidea点击:

JSF - Redirect to page and remember parameter ID for searching

javajsfrichfaces

提问by gaffcz

I have two xhtml pages and two managed beans.

我有两个 xhtml 页面和两个托管 bean。

On first page I have a list of topics (records from database table - second column contains <h:commandLink>tags):

在第一页上,我有一个主题列表(来自数据库表的记录 - 第二列包含<h:commandLink>标签):

enter image description here

在此处输入图片说明

Part of reduced code:

部分精简代码:

<rich:column><h:outputText value="#{item.id}"/></rich:column>
<rich:column><h:outputText value="#{item.createdBy}"/></rich:column>
<rich:column>
  <h:commandLink value="#{item.topic}" action="#{myTools.setMenuItem('posts')}"/>
</rich:column>

I'm using action="#{myTools.setMenuItem('posts')}"to redirect to page posts.xhtml. How can I pass the parameter "#{item.id}"to be able to find all posts to topic with given ID?

我正在使用action="#{myTools.setMenuItem('posts')}"重定向到页面posts.xhtml。如何传递参数"#{item.id}"以便能够找到具有给定 ID 的主题的所有帖子?

UPDATE (using DataModel):This could be the way:

更新(使用 DataModel):这可能是这样的:

<h:commandLink value="#{item.topic}" action="#{myTopic.submit}">

public String submit()
{
  topic = model.getRowData();
  return "/posts.xhtml?faces-redirect=true&id=" + topic.getId();
}

But I still don′t know how to pass the topic.getId()parameter to another bean (MyPosts)..?

但是我仍然不知道如何将topic.getId()参数传递给另一个 bean (MyPosts)..?

回答by BalusC

Just pass it as well.

也通过它。

E.g.

例如

<h:commandLink value="#{item.topic}" action="#{myTools.navigate('posts', item.id)}"/>

with

public String navigate(String menuItem, Long id) {
    this.menuItem = menuItem;
    return menuItem + "?faces-redirect=true&id=" + id;
}

The bean doesn't and shouldn't need to be in session scope. The view scope is fine. Otherwise the enduser will face unintuitive behaviour when interacting with the same page in multiple browser tabs/windows.

bean 不需要也不应该在会话范围内。视图范围很好。否则,在多个浏览器选项卡/窗口中与同一页面交互时,最终用户将面临不直观的行为。

回答by kgautron

You can use :

您可以使用 :

<f:setPropertyActionListener target="#{propertyToSet}" value="#{item.id}" />

<f:setPropertyActionListener target="#{propertyToSet}" value="#{item.id}" />

inside your commandLink.

在您的命令链接中。

回答by medopal

You can add a hidden field, store the ID in that field before submitting (use onclickin Javascript) and bind this hidden field to a variable inside the Bean.

您可以添加一个隐藏字段,在提交之前将 ID 存储在该字段中(onclick在 Javascript 中使用)并将此隐藏字段绑定到 Bean 内的变量。

<h:inputHidden id="selectedId" value="#{beakbean.selectedId}">

<h:commandLink value="#{item.topic}" onclick="updateSelectedId()" action="#{myTools.setMenuItem('posts')}"/>

function updateSelectedId(){
    //put the selected id in the field selectedId
}