【问题标题】:Retrieve data from ModelAttribute method in the view从视图中的 ModelAttribute 方法中检索数据
【发布时间】:2015-02-09 05:32:57
【问题描述】:

在我当前的 spring 项目中,我有一个像这样的通用控制器:

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

  protected Class<?> clazz;

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

  ...

}

使用这样的方法:

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

我想知道是否可以在这样的结构中(在 html 页面中)使用 lista 的值:

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

这个页面被映射到控制器中,方法如下:

通用控制器

  @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";
  }

家庭控制器(包含公共视图的映射等)

  @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";
  }

有人对此有任何想法吗?

【问题讨论】:

  • lista 可以通过视图访问。你遇到什么问题了吗?
  • @zeroflagL 是的,当我运行项目并访问视图时,&lt;option th:each="opt : ${lista}" th:value="${opt.getId()}"&gt; 在最后一页中不生成任何选项,就像lista 为空一样,即使它不是。
  • 您的控制器是否继承自basicController?我不确定 Spring MVC 是否处理继承的注释。所以populateList 可能不会被调用。
  • @zeroflagL 是的,控制器继承自basicController

标签: spring spring-mvc controller modelattribute


【解决方案1】:

好的,我只是测试并验证使用 ModelAttribute 方法中的值不需要额外的配置。所以,我只是在我的控制器中添加这样的方法:

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

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

当我访问任何映射视图时,我可以以我喜欢的方式使用此方法返回的值:

  <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>

【讨论】:

    【解决方案2】:

    @ModelAttribute 在您的控制器方法之前启动,并且我相信一旦您的方法运行就会消失。所以视图中不再有对象,它更像@RequestParam。

    但是,如果您使用的是较新版本的 Spring(我相信 4+),您可以尝试添加 @SessionAttributes("lista")。不过,您必须小心确保关闭会话属性。要关闭,请执行此人所做的 - link

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-20
    • 2020-11-14
    • 2015-10-01
    • 2021-08-05
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2017-07-07
    相关资源
    最近更新 更多