English 中文(简体)
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?

最佳回答

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.

问题回答

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.

The solution provided by EJB reads the annotated value for column length. James McMahon pointed out that this exposes the Hibernate implementation. That can be resolved fairly simply with getter methods. Assuming there is an interface for Person with a declared method getFirstNameLength:

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

@Entity
public class PersonImplementation extends Person {

   @Column(length=30)
   private String firstName;

   public int getFirstNameLength() {
       return this.getClass().getDeclaredField("firstName").getAnnotation(Column.class).length();
   }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签