1

In my current spring project, I have a generic controller like this:

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

  protected Class<?> clazz;

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

  ...

}

with a method like this:

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

I wonder if it's possible use the value for lista in a structure like that (in the html page):

<select class="form-control" th:name="...">
    <option th:each="opt : ${lista}" th:value="${opt.getId()}"><span th:text="${opt}"/>
    </option>
</select>

this page is mapped in the controllers with methods like that:

generic controller

  @RequestMapping(value = "cadastra")
  @PreAuthorize("hasPermission(#user, 'cadastra_'+#this.this.name)")
  @Menu(label = "cadastra")
  public String cadastra(Model model) throws Exception {
    model.addAttribute("command", serv.newObject());
    return "private/cadastra";
  }

home controller (contains mappings for public views, among others things)

  @RequestMapping(value = "/settings")
  public String settings(Model model) throws Exception {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    model.addAttribute("usuario", auth.getName());
    model.addAttribute("menu", MenuList.index());
    model.addAttribute("settings", MenuList.settings());
    return "private/settings";
  }

  @RequestMapping(value = "/profile")
  public String profile(Model model) throws Exception {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    model.addAttribute("usuario", auth.getName());
    model.addAttribute("menu", MenuList.index());
    model.addAttribute("command", usuario(auth.getName()));
    return "private/profile";
  }

Anyone have any idea about this?

Kleber Mota
  • 8,521
  • 31
  • 94
  • 188

2 Answers2

1

Ok, I just test and verify no extra configuration is needed for use the value from a ModelAttribute method. So, I just add methods like this in my controller:

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

  @ModelAttribute("classe")
  public String getName() {
    return clazz.getSimpleName();
  }

and when I access any mapped view, I can use the value returned by this method in the way I like:

  <tbody class="content">
    <tr th:each="item : ${lista}">
      <th></th>
      <th th:each="coluna : ${campos}" th:text="${item[coluna]}"></th>
      <th>
        <div class="btn-group" role="group" aria-label="menu_item">
          <button th:each="btn : ${menu_item}" type="button" class="btn btn-default link" th:attr="data-href=@{/__${classe}__/__${btn}__/__${item.getId()}__}" th:text="${btn}"></button>
        </div>
      </th>
    </tr>
  </tbody>
Kleber Mota
  • 8,521
  • 31
  • 94
  • 188
-1

@ModelAttribute fires up before your controller methods do, and will disappear once your method runs I believe. So you won't have the object in the view anymore, it acts more like a @RequestParam.

However, you can try adding @SessionAttributes("lista") if you're using newer version of Spring ( I believe 4+). You have to be careful to make sure you close the session attributes though. To close, do what this guy did - link.

Community
  • 1
  • 1
tehras
  • 741
  • 1
  • 7
  • 19
  • Do have any idea what is done here with the variable `teams`? controller: https://github.com/thymeleaf/thymeleafexamples-extrathyme/blob/2.1-master/src/main/java/thymeleafexamples/extrathyme/web/controller/ExtraThymeController.java and view: https://github.com/thymeleaf/thymeleafexamples-extrathyme/blob/2.1-master/src/main/webapp/WEB-INF/templates/extrathyme.html. That's what I wish accomplish. – Kleber Mota Dec 11 '14 at 10:36
  • The whole purpose of the model is to be used by a view. Your answer is as wrong as it can be. – a better oliver Dec 11 '14 at 11:43