views:

1571

answers:

6

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.

A: 

Some of the info you want to provide fits nicely in a BeanInfo object, originally meant to support JavaBeans stuff -- I have no idea if it still is being used and if it would be usable in the way you want.

Additionaly the spring:message tag provides support for i18n of static text, but I don't think it will tie in with your beans easily.

Simon Groenewolt
+1  A: 

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.

matt b
A: 

Are you looking for support for expression evaluation, or are you looking for a way to output bean properties?

Spring has both, for the former check spring docs: Spring Expression Evaluator For the latter check the docs: Spring MVC.

The Java language per se does not specify a language for accessing properties nor supports the property concept at the level you want, altough some stuff like Swing has support for properties and java has also some support for related stuff, check the java.beans package.

Miguel Ping
+2  A: 

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>
Todd
A: 

You want something that formats the person properties and you want to use this in multiple pages. In my opinon, you should use a cutom tag to do this:

<you:person = "${person}"/>

When you use facelets, that Tag could be just another facelet where you compose outputTexts to print the information. Thereby, you could add support for different locations.

checkout: https://facelets.dev.java.net/ and http://andrewfacelets.blogspot.com/2006/06/creating-composite-controls-with-jsf.html

Tim

Tim Büthe
A: 

Have a look at Wicket Web Beans.

Wicket Web Beans (WWB) is an Apache Wicket (http://wicket.apache.org) component toolkit for displaying and editing POJOs that conform to the JavaBeans specification. Web pages are automatically generated based on bean properties and certain conventions. If necessary, the layout, editability, and actions of these pages can be customized on an exception basis. In other words, the toolkit normally does what you'd expect, but when it doesn't, you can override its behavior.

At the highest-level, the net.sourceforge.wicketwebbeans.containers.BeanForm component provides rich AJAX form functionality. The form is embedded in a Page designed by you. This allows you to create customized page designs. Also, this allows multiple BeanForms to be incorporated on a single page. At your choosing, other lower-level components may be used independently of BeanForm (e.g., BeanGridPanel). WWB does not try to force you into a certain way of doing things, but BeanForm makes it very convenient to implement a bean-based form if you don't want to go to a lot of extra work. You focus on the model (beans), WWB handles the user interface.

Fields within a form are dynamically sent back to the server-side bean as they are changed, which eliminates the typical submit cycle. This makes WWB act more like a rich client application and less like a standard forms-based application.

Loki