何时/何地在 Java EE Web 应用程序上创建实体?

发布于 2024-12-20 09:50:56 字数 1189 浏览 3 评论 0原文

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

请参阅下面的代码(一些必要的代码可能尚未复制。)

实体将是:

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>

并且支持 bean 将是:

@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";
        }
    }
}

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.)

The entity would be:

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

    // some getters and setters
}

the web page would be:

<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>

and the backing bean would be:

@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";
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2024-12-27 09:50:56

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

如果它是一个简单的实体,则构造函数或 @PostConstruct 方法都可以工作。例如:

@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)。

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

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.

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";
    }
}

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文