Spring MVC表单支持对象树初始化的最佳实践 [英] Best Practice for Spring MVC form-backing object tree initialization

查看:96
本文介绍了Spring MVC表单支持对象树初始化的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个具有复杂对象树的表单支持对象 - 比如一个具有Contact Info对象的Person,该对象具有一堆具有一串字符串的Address对象 - 似乎该对象需要完全在我可以绑定到组件对象之前填充它们。因此,如果我正在创建一个新的Person,我需要确保它已经从bat中填充了所有组件对象,如果我从数据库中检索Person,我需要确保任何不是的对象从数据库填充得到的空对象填充。

If I have a form-backing object that has a complicated object tree -- say a Person that has a Contact Info object that has an Address object that has a bunch of Strings -- it seems that the object needs to be fully populated with component objects before I can bind to it. So if I'm creating a new Person, I need to make sure it has all the component objects populated off the bat, and if I'm retrieving a Person from the database, I need to make sure that any objects that aren't populated from the database get populated with empty objects.

第一个问题,当然 - 我的上述假设是否正确?似乎如果我尝试绑定到person.contactInfo.homeAddress.street并且没有ContactInfo,我会得到一个空指针异常。

First question, of course -- am I correct in my assumptions above? It does seem that if I try to bind to person.contactInfo.homeAddress.street and there is no ContactInfo, I get a null pointer exception.

其次,什么是最好的初始化我的对象的方法。我可以想到几种方法。一种是在声明时初始化所有成员对象:

Second, what's the best way to initialize my object. I can think of a couple of approaches. One is to initialize all member objects at declaration:

public class Person {
     String name;
     ContactInfo contactInfo = new ContactInfo();
     //getters, setters, etc.
}

public class ContactInfo {
     String phone;
     Address homeAddress = new Address();
}

等等。

另一种方法是让一个PersonFactory初始化所有东西(或者有一个工厂方法Person.getInstance来初始化所有东西)。

Another approach is to have a PersonFactory that initializes everything (or to have a factory method Person.getInstance that initializes everything).

在检索一个人的情况下从数据库,第一种方法将解决问题(即如果这个特定的人没有数据库中的地址,该对象仍将有一个地址),但这将意味着创建每个对象两次。不知道如何处理这个问题,除非使DAO显式填充所有内容,即使从数据库中没有检索到任何内容。或者给工厂一个方法来浏览对象并填写任何遗漏的东西。

In the case of retrieving a Person from the database, the first approach will solve the issue (i.e. if this particular person doesn't have an address in the database, the object will still have an Address), but this will mean creating each object twice. Not sure how to handle this otherwise, except to make the DAO explicitly populate everything even if nothing has been retrieved from the database. Or to give the factory a method to go through the object and "fill in" anything that's missing.

建议?

推荐答案

如果您愿意,请将其称为过度杀戮,但我们最终做的是创建一个通用工厂,它将接受任何对象并使用反射(递归地)查找所有null属性并实例化一个正确类型的对象。我使用Apache Commons BeanUtils做到了这一点。

Call it overkill if you like, but what we actually ended up doing was to create a generic factory that will take any object and use reflection to (recursively) find all the null properties and instantiate an object of the correct type. I did this using Apache Commons BeanUtils.

这样你就可以获取一个你可能从各种来源获得的对象(DAO,从XML反序列化,无论如何),传递给它通过这个工厂,并使用它作为一个表单支持对象而不用担心绑定所需的东西可能为null。

This way you can take an object that you may have gotten from various sources (a DAO, deserialization from XML, whatever), pass it through this factory, and use it as a form-backing object without worrying that something you need for binding may be null.

不可否认,这意味着实例化我们可能不需要给定表单的属性,但是在我们的情况下通常不适用。

Admittedly, this means instantiating properties that we may not need for a given form, but in our case that doesn't typically apply.

这篇关于Spring MVC表单支持对象树初始化的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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