2

What is the best way in a native MVC web project (can't use Hibernate or Spring) to provide a "translation table" for modifying the values in a bean's variables to presentation-friendly format? It seems poor OO practice to supply custom "web Getter methods" for each variable.

For example: a variable named status can be populated with values 'A', 'I', 'D', or 'U' in the peristence layer, so in the bean it is stored that way. But on a page these four values translate to 'Active' 'Inactive' 'Deleted' and 'Undefined', respectively. Where and how to house this translation knowledge?

(Also, does the Java webdev community have a specific name for this kind of "translation table"? I was sort of at a vocabulary deficit when searching here for an already answered solution.)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
David Neuschulz
  • 959
  • 1
  • 8
  • 14

2 Answers2

2

For that, normally resource bundles are used which can be accessed with standard JSTL <fmt:xxx> tags or even programmatically using ResourceBundle API which the JSTL tags are using under the covers.

E.g. status.properties file in com.example.i18n package

status.A = Active
status.I = Inactive
status.D = Deleted
status.U = Undefined

with (provided that you've a bean with this property as ${bean})

<fmt:bundle basename="com.example.i18n.status">
    Status: <fmt:message key="status.${bean.status}" />
</fmt:bundle>

or

<fmt:bundle basename="com.example.i18n.status" prefix="status.">
    Status: <fmt:message key="${bean.status}" />
</fmt:bundle>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

One common way to do this is to have a presentation level bean as well as a domain data bean. Put code in the presentation bean to copy values from the domain bean (which is the one you already have) to displayable values. Similarly you have code, that can also be in the presentation bean, to convert back when the form is submitted. (Assuming you have a way to create on the web site for that data component.

If the conversion code gets complex or there are significant bits of commonality between conversion for the various data objects across the whole application you can either extract the conversion to a set of classes that have as their job to convert a specific type of data OR you can have some utility classes that have methods to do the common conversions and they get used by the presentation beans to make the conversion code small and compact.

Lee Meador
  • 12,829
  • 2
  • 36
  • 42