在Java EE Web应用程序上何时/何地创建实体? [英] When/where do entities get created on a Java EE web application?

查看:93
本文介绍了在Java EE Web应用程序上何时/何地创建实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时或在何处创建实体?
XHTML页面通过托管bean加载并访问实体时是否会创建它们?
还是在托管bean中自动创建它们?
我们需要从托管bean的构造函数中手动创建它吗?

When or where do entities get created?
Do they get created when the XHTML page loads and accesses the entities via the managed bean?
Or do they get automatically created in the managed bean?
Do we need to manually create it from the managed bean's constructor?

请参见下面的代码(某些必要的代码可能尚未复制.)

Please see the code below (some necessary code might not have been copied.)

该实体为:

public class PersonalInfo implements Serializable {
    @Size(max = 50)
    @Column(name = "FIRST_NAME", length = 50)
    private String firstName;

    // some getters and setters
}

该网页将是:

<h:form>
    <h:outputText value="first name"/> 
    <h:inputText value="#{personalInforController.personalInfo.firstName}" />

    <h:commandButton value="hit me" 
        action="#{personalInforController.create}" 
        immediate="true"/>
</h:form>

和后备豆将是:

@Named(value = "personalInfoController")
@SessionScoped
public class PersonalInforController {
    @EJB
    PersonalInfoFacade ejbFacade;
    PersonalInfo personalInfo;
    String defaultPage = "index";

    public String create() {
        try {
            ejbFacade.create(personalInfo);
            return "prepareCreate";
        } catch (Exception e) {
            return "success";
        }
    }
}

推荐答案

在给出的示例代码中,create动作确实似乎无法工作.该实体必须在此之前由支持bean创建.

In the example code given, the create action indeed doesn't seem to be able to work. The entity must be created by the backing bean before that.

如果它是一个简单的实体,则可以使用构造函数或@PostConstruct方法.例如:

If it's a simple entity, either the constructor or an @PostConstruct method would work. For instance:

@Named(value = "personalInfoController")
@SessionScoped
public class PersonalInforController {

    @EJB
    PersonalInfoFacade ejbFacade;
    PersonalInfo personalInfo;
    String defaultPage = "index";

    @PostConstruct
    public void init() {
        personalInfo = new PersonalInfo();
    }

    public String create() {
    try {
        ejbFacade.create(personalInfo);
        return "prepareCreate";
    } catch (Exception e) {
        return "success";
    }
}

关于代码的一些注释.将您的bean声明为@SessionScoped是高度可疑的,并且很可能是错误的.如果在两个选项卡或窗口中编辑personalInfo,您将大受打击.我建议将您的bean设为@ViewScoped(对于CDI,Seam3有一个单独的扩展名可以启用此功能,如果您不能/不使用此扩展名,请考虑使用@ManagedBean而不是@Named).

Some notes about the code. It's highly suspicious, and most likely plain wrong, to declare your bean to be @SessionScoped. If personalInfo is being edited in two tabs or windows you'll be in a world of hurt. I suggest making your bean @ViewScoped (for CDI, there's a separate extension made by the Seam3 that enables this, if you can't/won't use this extension consider using @ManagedBean instead of @Named).

此外,您可能希望将实例变量声明为私有变量,并为ejbFacade提供更好的名称(例如personalInfoFacade).我还怀疑在commandButton上是否需要immediate,并且由于outputText显然是给定inputText的标签,因此您可能要考虑使用outputLabel和for属性.

Also, you might want to declare your instance variables to be private and give ejbFacade a better name (e.g. personalInfoFacade). I also doubt whether immediate is necessary on the commandButton, and since the outputText is obviously a label for the given inputText, you might want to consider using outputLabel and the for attribute.

这篇关于在Java EE Web应用程序上何时/何地创建实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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