Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse fieldlength in form, validation and ddl?

I'm working on an Spring application with lots of input forms. I'd like to reuse the field length in the UI-form, validation and JPA annotations. Is there an elegant way to solve this. My solution at the moment is, to use constants to declare the length:

public class Person
{
   public static final int FIRSTNAME_LENGTH = 25;

   @Column(length=FIRSTNAME_LENGTH)
   private String firstName;

   ...
}

and then reuse the constant in the Validator and the Jsp

...

<form:input path="firstName" 
    maxlength="<%= Integer.toString(Person.FIRSTNAME_LENGTH) %>"/>

...

which is pretty verbose.

Is there any more elegant solution to this problem?

like image 840
Dominik Avatar asked Nov 29 '09 21:11

Dominik


2 Answers

It's quite possible to access the information stored in annotations. In fact, this is their main purpose: storing meta information on a class/ method/ field. Here is an example of how to access the length stored in a @Column annotation:

import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
public class Person {

   @Column(length=30)
   private String firstName;

   public static void main(String[] args) throws SecurityException, NoSuchFieldException {
      Object person = new Person();
      //find out length    
      System.out.println(person.getClass().getDeclaredField("firstName").getAnnotation(Column.class).length());
   }
}

You should be able to create some custom tag or bean to extract this info generically.

It's not difficult to create your own annotations. You could consider creating one that specifies which fields are to be included on the form, how they should be rendered, description, etc. Than you could create a generic form. Then again you may not like to mix domain and presentation.

like image 90
EJB Avatar answered Nov 04 '22 19:11

EJB


I asked a similar question when I was using Hibernate,

Getting column length from Hibernate mappings?

No idea how to pull this information under JPA however, but if you are using Hibernate as your underlying layer you could try access Hibernate directly and implement the solution above.

However the Hibernate solution is just as in-elegant as the solution that you outlined in your answer, with the added disadvantage that it ties your JPA implementation to Hibernate.

like image 1
James McMahon Avatar answered Nov 04 '22 17:11

James McMahon