JSF2.0在托管bean之间传递Value对象 [英] JSF2.0 Passing Value objects between managed beans

查看:180
本文介绍了JSF2.0在托管bean之间传递Value对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Weblogic 10.3.4,PrimeFaces和JQuery编写了一个小型JSF2.0应用程序。我现在正在考虑将我们的主Web应用程序转换为JSF2.0。目前使用的是Weblogic 8.1,Java 1.4和JSP。我现在的问题是将对象从一个托管bean传递到另一个托管bean的最佳方法是什么。我们的应用程序由许多屏幕组成,但是一般模式是在第一个屏幕上输入的引用,并且在提交时从数据库中查找并填充值对象(标准java bean)。然后返回屏幕2,它通常是一个由值对象变量准备编辑的形式。

I've already written a small JSF2.0 app utilising Weblogic 10.3.4, PrimeFaces and JQuery. I'm now looking at converting our Main Web App to JSF2.0. This is currently uses Weblogic 8.1, Java 1.4 and JSP's. The question I have at the moment is what is best way to pass objects from one managed bean to another. Our app consists of many screens but the general pattern is a reference is entered on the first screen and on submit this is looked up from the Database and a Value Object is populated (standard java bean). Screen 2 is then returned which is generally a form consisting of the Value Object's variables ready for edit.

目前所有必需的对象都作为属性保存在HTTPServletRequest对象中。第一个屏幕(在自定义编写的控制器类中),然后在随后的屏幕中从中检索。

Currently all required objects are saved as an attribute in an HTTPServletRequest object in the 1st screen (within a custom written controller class) and then retrieved from this in the subsequent screen.

这仍然是这样做的方式还是有一个新的JSF 我错过了的方式。我还考虑将这些Value Objects存储在用户会话bean(我们将会拥有它)中,然后在需要时从那里检索。我假设包含值对象的Map在这种情况下是最好的方法吗?

Is this still the way to do it or is there a new "JSF" way that I've missed. I did also think about storing these Value Objects in a user session bean (which we will have anyway) and then retrieving from there when needed. I assume a Map containing Value Objects would be the best way to go in this case?

推荐答案

您可以注入一个托管bean另一个托管bean由 @ManagedProperty

You can inject a managed bean in another managed bean by @ManagedProperty.

假设你有一个像这样的会话范围的bean

Assuming that you've a session scoped bean like this

@ManagedBean
@SessionScoped
public class User {
    // ...
}

这样的请求范围bean

And a request scoped bean like this

@ManagedBean
@RequestScoped
public class Profile {

    @ManagedProperty(value="#{user}") // #{user} is the managed bean name
    private User user;

    @PostConstruct
    public void init() {
        // User is available here for the case you'd like to work with it
        // directly after bean's construction.
    }

    public String save() {
        // User is available here as well, during action methods.
        userDAO.save(user);
    }

    // +getter +setter

}

这篇关于JSF2.0在托管bean之间传递Value对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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