Search code examples
javaspringgenericsmodelattribute

ModelAttribute method for generic class


I want add this method in a generic controller used by my spring application:

@ModelAttribute("lista")
public List<E> populateList() {
  return serv.lista();
}

this method is part of this class:

public class basicController<E> {
  @Autowired
  private basicService<E> serv;

  protected Class<?> clazz;

  public basicController(Class<?> clazz) {
    this.clazz = clazz;
  }

  ...

}

But instead of lista, I want use the name of the class (clazz.getSimpleName() or otherwise).

Anyone have an idea of how to accomplish that?


Solution

  • I don't think this is possible.

    When working with annotations, the attribute values have to be compile-time constant expression. And clazz.getSimpleName() is not a such.