在JSF + EJB 3.1中的请求范围内的受管Bean之间传递参数 [英] Passing parameters between Request Scoped Managed Beans in JSF + EJB 3.1

查看:155
本文介绍了在JSF + EJB 3.1中的请求范围内的受管Bean之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的问题是使用JSF + EJB编辑数据库的非常基本,简单的实现.

Our problem is a very basic, simple implementation of editing the database using JSF + EJB.

保持简短: 两个独立的XHTML视图,使用两个独立的Managed Beans @RequestScope.

Keeping things short: two separate XHTML views, use two separate Managed Beans @RequestScope.

WebuserListBean和EditWebuserBean,并使用@ManagedProperty注入WebuserListBean,以便获得选定的用户数据.到目前为止没有问题.视图成功地填充了数据!

WebuserListBean and EditWebuserBean, and with @ManagedProperty we inject WebuserListBean, so we could obtain selected user data. So far no problems. Views are filled with data succesfully!

但是! 我们希望能够编辑用户! 在这里(令我惊讶),我们无法克服这个问题.

BUT! We want to be able to edit the user! And here (to my surprise) we cannot overcome the problem.

第一次尝试: 由于请求范围内的bean在填充视图后已死,因此在Save()方法上,@ PostConstruct尝试再次启动,当然不能.因此,我们甚至无法从数据库或其他任何东西中获取它.

1st try: Because the request scoped bean is dead after filling the view, on Save() method the @PostConstruct tries to launch again, of course it can't. So we couldn't even obtain it from database or anything.

第二次尝试: 因为在填满视图之后,请求范围的bean已死,所以如果我们未在@postconstruct中将用户设置为字段,则我们将失去与先前视图上链接的用户对象的连接(并被注入,但现在该视图也已死)

2nd try: Because the request scoped bean is dead after filling the view, if we do not set up user as field in @postconstruct, we lose our connection with user object which was linked on previous view (and injected, but now that view is dead too).

第三次尝试: 无法在ViewScoped中注入RequestScope

3rd try: Cannot inject RequestScope in ViewScoped

好的,还有我们的限制,因为我们认为这是错误的方式:

Ok and our restrictions, because we think it's wrong way:

  1. 我们不想为此创建一个SessionScoped托管Bean
  2. 我们不想使用任何参数等.我们想使用EJB
  3. 我们不确定是否可以将数据存储在有状态会话Bean中,这是我们的 模块的端点?这是合适的方法吗?
  1. We dont want to create a SessionScoped Managed Bean for this
  2. We dont want to use any params etc. We want to use EJB
  3. We are not sure if we could store data in Stateful session bean which is our endpoint for the module? Is it proper approach?

感谢您的任何建议,我们可以粘贴一些代码,但是我想这是没有意义的! 干杯!

Thanks for any advice, we could paste some code but i guess it is pointless! Cheers!

推荐答案

有很多方法,但是如果导航中涉及的页面位于同一文件夹中,我建议您使用Flash(我最近发现阅读 BalusC 回答是闪存存在一个已知问题,即在不同文件夹中的页面之间导航时,它将不保存值!).

There are many ways to do it, but I recommend using the flash if the pages involved in the navigation are in the same folder (I recently found out reading a BalusC answer that there is a known issue with the flash, in which it will not hold values when navigating between pages in different folders!).

闪存是一种将参数保存的时间比单个请求的上下文长一点的方法(如果我没记错的话,则取自Ruby;如果我记错了,请改正我的意思),从而允许将发送的参数传递给例如在后续视图中获取.如果我没有记错的话,这些值将在保存它们后发出的第二个请求中丢弃.您可以将Flash注入托管bean中,如下所示:

The flash is a way to hold parameters for a little longer than the context of a single request (concept taken from Ruby if I'm not mistaken, someone correct me if I'm wrong), allowing for the sent parameters to be fetched in a subsequent view, for example. Those values are discarded in the second request issued after saving them, if I'm not mistaken. You can inject the flash in your managed beans like this:

@ManagedProperty("#{flash}")
private Flash flash;

public void setFlash(Flash newFlash) {
    flash = newFlash;
}

然后,您可以使用put和get方法像访问地图一样访问它.如果您在bean中使用put方法,请返回重定向规则,然后在第二个bean上使用get方法,您的对象应该在其中.

Then, you access it like a map with the put and get methods. If you use the put method in a bean, return a redirection rule and, on the second bean, use the get method your object should be there.

您还可以在JSF中找到一个易于理解的通信指南(列出了非常广泛的选项列表)

You can also find a highly comprehensible guide of communication in JSF (listing a really extensive list of options) here, in particular if you need to navigate between pages in different folders.

这篇关于在JSF + EJB 3.1中的请求范围内的受管Bean之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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