PDA

View Full Version : Using Enumerations in a View.


Lori
Sep 12th, 2007, 11:22 AM
Hi.

I am trying to write good MVC Code and i wonder if there is a clean way to supply information about enums for the view.

Since i use AndroMDA (http://andromda.org) to generate my classes, i don't even get "real" enums but classes with static fields.

e.g.

public class ActiveFlag {
public static final ActiveFlag activated = new ActiveFlag(10);
public static final ActiveFlag deactivated = new ActiveFlag(20);
...
}

So now when for example i have a FormController i overwrite the referenceData method like this:

protected Map referenceData(HttpServletRequest request) throws Exception {
...
model.put("ActiveFlag_activated", ActiveFlag.activated);
model.put("ActiveFlag_deactivated", ActiveFlag.deactivated);
...
}

I have to do this if i - for example - want to set the correct selected state of a property which is represented by this enum and is displayed in a html select box.

But when it comes to larger enums with much more entries this implementation cannot be the only way. Can it?

Thanks in advance.

Jörg Heinicke
Sep 12th, 2007, 01:58 PM
Since i use AndroMDA (http://andromda.org) to generate my classes, i don't even get "real" enums but classes with static fields.

I never used it but it should be pretty easy to adapt the cartridges (I think that's how they call their code generation templates) to generate the code you want.

But when it comes to larger enums with much more entries this implementation cannot be the only way.

We used to register enum instance on the constructor call and provide methods like getEnumList().

Joerg