简体   繁体   English

JSF2.0在托管bean之间传递Value对象

[英]JSF2.0 Passing Value objects between managed beans

I've already written a small JSF2.0 app utilising Weblogic 10.3.4, PrimeFaces and JQuery. 我已经使用Weblogic 10.3.4,PrimeFaces和JQuery编写了一个小型JSF2.0应用程序。 I'm now looking at converting our Main Web App to JSF2.0. 我现在正在考虑将我们的主Web应用程序转换为JSF2.0。 This is currently uses Weblogic 8.1, Java 1.4 and JSP's. 目前使用的是Weblogic 8.1,Java 1.4和JSP。 The question I have at the moment is what is best way to pass objects from one managed bean to another. 我现在的问题是将对象从一个托管bean传递到另一个托管bean的最佳方法是什么。 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). 我们的应用程序由许多屏幕组成,但是一般模式是在第一个屏幕上输入的引用,在提交时,这将从数据库中查找并填充值对象(标准java bean)。 Screen 2 is then returned which is generally a form consisting of the Value Object's variables ready for edit. 然后返回屏幕2,其通常是由值对象的变量组成的形式,准备编辑。

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. 目前,所有必需对象都作为属性保存在第一个屏幕中的HTTPServletRequest对象中(在自定义编写的控制器类中),然后在后续屏幕中从中检索。

Is this still the way to do it or is there a new "JSF" way that I've missed. 这仍然是这样做的方式,还是我错过了一种新的“JSF”方式。 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. 我还考虑将这些值对象存储在用户会话bean(我们将会拥有它)中,然后在需要时从那里检索。 I assume a Map containing Value Objects would be the best way to go in this case? 我假设包含值对象的Map在这种情况下是最好的方法吗?

You can inject a managed bean in another managed bean by @ManagedProperty . 您可以通过@ManagedProperty在另一个托管bean中注入托管bean。

Assuming that you've a session scoped bean like this 假设你有一个像这样的会话范围的bean

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

And a request scoped bean like this 还有像这样的请求范围的bean

@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

}

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

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