Java Web Application Properties

Does Java and / or Spring have a concept of properties? I have a group 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 face, property names are hardcoded in JSP.

 Name: ${person.name}<br> Date of birth: ${person.dateOfBirth}<br> Height: ${person.height}<br> Weight: ${person.weight}<br> 

In addition, there may be several different pages on which a person is displayed. Note that the date of birth is java.util.Date , so the JSP or controller will use java.text.SimpleDateFormat to format it. Height and weight are numbers, and they can have their own java.util.Format , used for formatting.

I am looking for a property search mechanism. Each property (name, date of birth, height, etc.) will have a display name, format and description (used for reference or tooltips). Property attributes will be defined in the configuration file somewhere. When a property is displayed, the display name and format will be viewed using the property mechanism. It would also solve localization.

My question is that something similar has already been implemented for Java.

+4
source share
6 answers

If you understand correctly, you want to take into account the format and description of some of these properties in an external file. You can take a look at the Spring MessageSource (link to javadoc here ), which is somewhat wrapped and similar to the ResourceBundle class in the JDK.

Using MessageSource 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 that you 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 quite sure how to specify the format in messages.properties , but I know that this is possible.

+1
source

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

In the above example, the code would look 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> 
+2
source

Some of the information you want to provide fits nicely into the BeanInfo object, originally designed to support JavaBeans material - I have no idea if it is still used and whether it can be used the way you want.

Extra spring tag: provides i18n support for static text, but I donโ€™t think it will contact your beans easily.

0
source

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

Spring has both for the first check spring docs: Spring Expression Evaluator For the last check the docs: Spring MVC .

The Java language itself does not specify a language for accessing properties and does not support the concept of a property at the right level, although some things, such as Swing, support properties, and java also supports some support for related things, check java.beans .

0
source

You want something that formats the properties of a person, and you want to use it on several pages. In my opinion, you should use the cutom tag for this:

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

When you use facelets, this tag may just be another face in which you create output tags for printing information. This way you can 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

0
source

Check out the Wicket Web Beans .

Wicket Web Beans (WWB) is the Apache Wicket toolkit ( http://wicket.apache.org ) for displaying and editing POJOs that comply with the JavaBeans specification. Web pages are automatically generated based on bean properties and specific conventions. If necessary, the layout, editable and actions of these pages can be customized based on the exception. In other words, a toolbox usually does what you expect, but when it is not, 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 the page you developed. This allows you to create custom page designs. In addition, it allows you to include multiple BeanForms on the same page. Other low-level components of your choice can be used independently of the BeanForm (e.g., BeanGridPanel). WWB is not trying to force you to a certain way to do something, but BeanForm makes it very convenient to implement a bean-based form if you do not want to do a lot of extra work. You focus on beans, WWB processes the user interface.

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

0
source

All Articles