简体   繁体   English

JSF,如何在另一个页面/支持bean中设置属性,然后导航到该页面?

[英]JSF, How to set a property in a different page/backing bean and then navigate to that page?

I am using JSF 2.0 and attempting to pass values between different pages in my App. 我正在使用JSF 2.0,并尝试在我的App中的不同页面之间传递值。

The setup is as follows: I have a page called userSelect that has a backing bean userSelectBacking. 设置如下:我有一个名为userSelect的页面,该页面具有支持bean userSelectBacking。 On this page I display a list of users that can be selected and submit using an h:commandbutton, when the page is submit the navigation goes to a userEdit page. 在此页面上,我显示可以使用h:commandbutton进行选择和提交的用户列表,提交页面时,导航将转到userEdit页面。

I have a page called userEdit, that has a backing bean userEditBacking which displays the information for a user and allows that user to be edited. 我有一个名为userEdit的页面,该页面具有一个后备bean userEditBacking,它显示用户的信息并允许对该用户进行编辑。

I would like to pass the user selected from the userSelect page into the userEdit page. 我想将从userSelect页面选择的用户传递到userEdit页面。 I am currently using f:setPropertyActionListener to set the user in my userEdit backing from the userSelect page, however when I navigate to the userEdit page, it loses the information I set. 我当前正在使用f:setPropertyActionListener从userSelect页面在userEdit支持中设置用户,但是当我导航到userEdit页面时,它将丢失我设置的信息。

is there a way that I can pass the values between the two pages/backing beans? 有没有一种方法可以在两个页面/备用bean之间传递值?

thanks 谢谢

I am currently using f:setPropertyActionListener to set the user in my userEdit backing from the userSelect page 我目前正在使用f:setPropertyActionListener从userSelect页面的userEdit支持中设置用户

It should work. 它应该工作。

however when I navigate to the userEdit page, it loses the information I set. 但是,当我导航到userEdit页面时,它会丢失我设置的信息。

This will happen if the data loading logic is wrong, or you fire a redirect afterwards while the bean is request scoped. 如果数据加载逻辑错误,或者您在请求bean范围之后触发重定向,就会发生这种情况。

To fix the data loading logic, just ensure that in case of a request scoped bean the same datamodel is preserved in the subsequent request. 要修复数据加载逻辑,只需确保在请求范围内的bean的情况下,在后续请求中保留相同的数据模型。 Usually you use the bean's constructor or lazy loading in the getter for this. 通常,您为此使用bean的构造函数或在getter中进行延迟加载。 If that is not an option, then you need to put the bean in a bit broader scope, eg @ViewScope or @SessionScope . 如果这不是一个选择,那么您需要将bean放在更大的范围内,例如@ViewScope@SessionScope

To fix the redirect issue, either just don't fire a redirect (ie remove <redirect/> from navigation case, or don't call ExternalContext#redirect() ), or put bean in a broader scope. 要解决重定向问题,要么不触发重定向(即从导航案例中删除<redirect/> ,要么不调用ExternalContext#redirect() ),或者将bean放在更大的范围内。

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

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