Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java web application properties

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.

like image 369
Steve Kuo Avatar asked Oct 26 '22 07:10

Steve Kuo


2 Answers

To do this using JSP and JSTL, you can use the "fmt" tag library which supports localization and formatting for numbers, dates, etc.

In the example you posted, the code would be something like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%-- ..assuming bundle and locale are already set... --%>
<fmt:message key="NAME" bundle="${myBundle}"/>: ${person.name}<br>
<fmt:message key="DOB" bundle="${myBundle}"/>: <fmt:formatDate type="date" value="${person.dateOfBirth}"/><br>
<fmt:message key="HEIGHT" bundle="${myBundle}"/>: <fmt:formatNumber pattern="##.##" value="${person.height}"/><br>
<fmt:message key="WEIGHT" bundle="${myBundle}"/>: <fmt:formatNumber pattern="##.##" value="${person.weight}"/><br>
like image 185
Todd Avatar answered Nov 04 '22 01:11

Todd


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 the ResourceBundle class 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:

<spring:message code="user.dob" arguments="${user.dateOfBirth}"/>

where your messages.properties file contains:

user.dob = Your date of birth is {0}

I'm not entirely sure how to specify the format within messages.properties, but I know that it is possible.

like image 20
matt b Avatar answered Nov 04 '22 02:11

matt b