Does Java and/or Spring have the concept of properties? I have bunch of domain models, each of which has several properties. Example:
public class Person { private String name; private Date dateOfBirth; private float height; private float weight; // getters and setters not shown }
When displaying a person, the property names are hardcoded in the JSP.
Name: ${person.name}<br> Date of birth: ${person.dateOfBirth}<br> Height: ${person.height}<br> Weight: ${person.weight}<br>
Furthermore, there may be several different pages that display a person. Note that date of birth is a java.util.Date, so the JSP or controller will use java.text.SimpleDateFormat to format it. Height and weight are numbers, and they may have their own java.util.Format used for formatting.
What I’m looking for a property lookup mechanism. Each property (name, date of birth, height, etc) would have the display name, format, and description (used for help or tooltips). The properties’ attributes would be defined in a configuration file somewhere. When displaying a property, the display name and format would be looked-up via the property mechanism. This would also solve localization.
My question is if there’s already something like this implemented for Java.
If I understand you correctly, you want to be able to put the format and description of some of these properties in an external file. You might want to take a look at Spring’s
MessageSource(link to javadoc here), which somewhat wraps around and is analagous to theResourceBundleclass in the JDK.Using
MessageSource‘s will allow you to place the format and any text in an external file (and have different properties files for different languages), but I believe you will still need to include in your JSP which properties to pass as arguments, for example:where your
messages.propertiesfile contains:I’m not entirely sure how to specify the format within
messages.properties, but I know that it is possible.