JSF/Java设计问题 [英] JSF / Java Design Question

查看:78
本文介绍了JSF/Java设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行JSF 2.0

I am running JSF 2.0

我目前有一个名为Contacts.java的Java控制器.在其中,我有一个存储为成员变量的地址列表

I currently have one java controller that is called Contacts.java. Inside there I have a list of addresses stored as a member variable

List<Addresses> addresses;

在我的应用程序加载时,它会显示一堆联系人,您可以单击某个联系人以打开"它以查看其数据.当您单击联系人时,会将contactId传递给基于contactId填充列表的方法.然后依次显示数据.

When my application loads it displays a bunch of contacts and you are able to click on a contact to "open it" to view its data. When you click on the contact the contactId is passed to a method that populates the List based on the contactId. It in turn then displays the data.

我还有很多功能,可以将地址添加到列表,删除地址,更新等,这些都取决于此contactId.

I also have a bunch of functionality that will add an address to the list, remove an address, updated etc. which are all dependent on this contactId.

我正在考虑将Contacts.java分为两个控制器Contacts.java和Addresses.java,因为与地址有关的逻辑太多,我认为它应该在自己的类中.

I was considering splitting up my Contacts.java into two controllers Contacts.java and Addresses.java because there is just so much logic having to do with addresses that I figured it should be in its own class.

我苦苦挣扎的问题是,如果我将所有处理Addresses的逻辑转移到Addresses.java中,它将需要以某种方式引用在编译时间之后选择的contactId.

The issue that I am struggling with is that if I move all of the logic dealing with Addresses into Addresses.java it will need to somehow have a reference to the contactId that was selected after compile time.

在编译时,地址将在当前的contactId填充到构造函数中的列表地址时进行编译.不过,由于用户在应用程序中选择联系人时已设置contactId,因此目前尚未设置.

At compile time Addresses will compile looking for the current contactId when it is populating the List addresses in the constructor. It will have not been set at this point though because the contactId is set when the user selects a contact in the application.

这是一个不好的设计想法吗?我对Java和OOP还是很陌生,还没有完全掌握如何将它们分开的概念.任何帮助将不胜感激.

Is this a bad design idea ? I am very new to Java and OOP and have not full grasped the concepts of how to break these apart. Any help would be appreciated.

推荐答案

通常,每个视图/表单最好使用单个bean,但这还取决于您是否使用异步请求(是否使用了ajax).至于传递数据,有几种方法取决于源,目标和/或路线.

In general, a single bean per view/form is preferable, but that also depends on whether you're using asynchronous requests or not (ajax or not). As to passing the data around, there are several ways depending on the source, the target and/or the route.

在编译时,地址将在当前的contactId填充到构造函数中的列表地址时进行编译.不过,由于用户在应用程序中选择联系人时已设置contactId,因此目前尚未设置.

在您的特定情况下,您似乎希望构造函数/后构造函数是加载数据的唯一方法.这不是真的.您也可以在事件(操作)方法中执行此操作.例如

In your particular case, you seem to expect that the constructor/postconstruct is the only way to load the data. This is not true. You can also do it in the event (action) methods. E.g.

<h:dataTable value="#{contacts.list}" var="contact">
  <h:column>
    #{contact.name}
  </h:column>
  <h:column>
    <h:commandButton value="View info" action="#{contacts.view(contact)}">
  </h:column>
</h:dataTable>

然后在Contacts辅助bean中(只需将List<Address>List<Phone>设置为Contact的属性,并带有适当的getter和setter)

And then in the Contacts backing bean (you only need to make List<Address> and List<Phone> a property of the Contact, with appropriate getters and setters)

public String view(Contact contact) {
    this.contact = contact;
    contact.setAddresses(addressDAO.list(contact));
    contact.setPhones(phoneDAO.list(contact));
    return "view";
}

然后在与view关联的页面中:

And then in the page associated with view:

<h:dataTable value="#{contacts.contact.addresses}" var="address">
    ...
</h:dataTable>
<h:dataTable value="#{contacts.contact.phones}" var="phone">
    ...
</h:dataTable>

或者,如果您使用的是JPA,则还可以利用延迟加载.但这是一个故事和设计.

Or if you're using JPA, you can also take benefit of lazy loading. But that's a story and design apart.

这篇关于JSF/Java设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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