简体   繁体   English

JSF:将选定的变量值保存在数据表中以供下一个视图使用

[英]JSF: Save selected variable value in datatable for next view

I have a data table: 我有一个数据表:

    <h:dataTable binding="#{table}" value="#{chooseKittens.kittenList}">
      <h:column>
        <p:commandLink value="#{chooseKittens.kittenList[table.rowIndex]}" action="#{chooseKittens.petKitten}"/>
      </h:column>
    </h:dataTable>

The dataTable is a of links that will take you to a different page. dataTable是一个链接,可以将您带到另一个页面。 When I go to a different page, though, I want to save the value of the command link clicked so that I can display it in subsequent views. 但是,当我转到另一个页面时,我想保存单击的命令链接的值,以便我可以在后续视图中显示它。 Is there a way to do this without SessionScope? 有没有办法在没有SessionScope的情况下做到这一点? I looked into t:saveState, but I'm not sure how to save just the variable clicked in a Datatable. 我查看了t:saveState,但我不确定如何只保存在Datatable中单击的变量。 If SessionScope is the only thing available, is there a way to make the kittenList in a different scope? 如果SessionScope是唯一可用的东西,有没有办法让kittenList在不同的范围内? (The kitten list should change when a new kitten is added in the database, so it should be in RequestScope or ViewScope). (当在数据库中添加新小猫时,小猫列表应该更改,因此它应该在RequestScope或ViewScope中)。

If you want to share information between views there are two typically used alternatives you could consider. 如果要在视图之间共享信息,可以考虑两种常用的替代方法。

  1. Pass information to the next view as a GET parameter 将信息作为GET参数传递给下一个视图

    In this case you'll use a JSF component generating a plain navigational link and attach a query parameter to that link, like in: 在这种情况下,您将使用生成普通导航链接的JSF组件并将查询参数附加到该链接,如:

     <h:link value="Next page" outcome="/view-kitten"> <f:param name="id" value="#{kitten.id}"/> </h:link> 

    In the receiving page the parameter is available via: 在接收页面中,参数可通过以下方式获得:

     <f:metadata> <f:viewParam name="id" value="#{kittenBean.id}/> </f:metadata> 

    More excellent information can be found in What can <f:metadata> and <f:viewParam> be used for? 更多优秀的信息可以在<f:metadata><f:viewParam>中使用什么?

  2. Pass information to the redirected view using Flash 使用Flash将信息传递到重定向的视图

    In this case you're performing a redirect in an action method of a command component and will attach necessary data to the target view using Flash object: 在这种情况下,您在命令组件的操作方法中执行重定向,并使用Flash对象将必要的数据附加到目标视图:

     <h:commandButton value="Next page" action="#{bean.next}"/> 

    with

     public String next() { //do business job FacesContext.getCurrentInstance().getExternalContext().getFlash().put("kitten", selectedKitten); return "/view-kitten?faces-redirect=true"; } 

    It will be available in the target view via EL with #{flash.kitten} or programmatically with FacesContext.getCurrentInstance().getExternalContext().getFlash().get("kitten") . 它将在目标视图中通过EL使用#{flash.kitten}或以编程方式使用FacesContext.getCurrentInstance().getExternalContext().getFlash().get("kitten")

    An extended example can be found in JSF2.0 - How to get the values in other jsf page's bean in request scope . 可以在JSF2.0中找到扩展示例- 如何在请求范围内获取其他jsf页面的bean中的值

The last thing to note is: don't abuse HTTP session . 最后要注意的是: 不要滥用HTTP会话 You shouldn't pollute the session with information that belongs elsewhere. 您不应该使用其他地方的信息污染会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM