How to convert Address object?!?

Hi,

I have a form with three text fields: Street, City, Zipcode.

How can I convert these text fields into bean's property Address (String street, String city, String zipcode) using Converter interface?

Is it possible in JSF?

All books and tutorials converts only one-valued objects.

thank you

[330 byte] By [tomla] at [2007-11-27 1:44:21]
# 1

> three text fields: Street, City, Zipcode.

> String street, String city, String zipcode

You want to convert String to String? Fairly meaningless, isn't it?

Or don't you mean that? Could you please rephrase the question/problem. Eventually supplied with pseudocode to show what you want to achieve.

BalusCa at 2007-7-12 1:03:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Ooops, sorry for bad explanation.

I have a managed bean with:

private Address address;

and I want to convert three text fields (street, city, zipcode) into address object.

Simply, I want to replace this:

public void createCustomer(name, age, new Address(street, city, zipcode))

with this:

public void createCustomer(name, age, address)

!!!without the "manual" creation of the address object.!!!

(=> NO new Address(street, city, zipcode) in my managed bean)

I am not sure whether JSF can do that and whether the idea of Converters covers this use case.

tomla at 2007-7-12 1:03:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You're aware that you can "deeplink" the getters?

Basic working example:<h:form>

<h:inputText value="#{myBean.name}" />

<h:inputText value="#{myBean.age}" />

<h:inputText value="#{myBean.address.street}" />

<h:inputText value="#{myBean.address.city}" />

<h:inputText value="#{myBean.address.zipcode}" />

<h:commandButton value="submit" action="#{myBean.action}" />

</h:form>

MyBeanprivate String user;

private Integer age;

private Address address = new Address();

// + getters + setters

public void action() {

// Do your thing here.

createCustomer(name, age, address);

}

BalusCa at 2007-7-12 1:03:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Now, I am ;-) (I feel so stupid)thank you
tomla at 2007-7-12 1:03:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I have one more question:

Can I validate whether all 3 attributes of address or none of them were filled? (required=true isn't solution, when none of attibutes is filled then it's correct).

I know I can validate it in the bean manually but can I write a Validator for this?

thank you

tomla at 2007-7-12 1:03:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

There are several ways.

You can hardcode the ID names ("name", "address", "zipcode") of the components in the validator and then inside the validator do a UIViewRoot#findComponent() to lookup the other components and finally get it's values.

You can setup a f:validator inside the first component only and pass a f:attribute along it indicating the ID's of the other two components. You can get the attributevalue inside the validator using UIComponent#getAttributes().

Better way is to create a custom component and validate inside the component using attribute names. Something like:

<my:address value="#{myBean.address}" requireAll="true" />

which should translate to three inputText fields.

But if this is one (or more) steps too far away for you yet, then it isn't harmful to (temporary) validate it inside the backing bean.

BalusCa at 2007-7-12 1:03:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...