gpt4 book ai didi

java - Spring - 如何在局部 View 中访问对象

转载 作者:行者123 更新时间:2023-11-30 08:33:12 24 4
gpt4 key购买 nike

我正在使用 Spring Boot 和 Fremarker 作为模板引擎创建一个简单的博客应用程序。所以基本上我有作者和帖子。我有一个 navbar.tfl,它是局部 View 。因此,当我在此部分 View 中选择“作者”下拉列表时,我想列出所有作者的姓名,并包含一个链接以显示他们的所有帖子。

<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/">FSM IT</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Posts <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/post/list">List</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Authors <span class="caret"></span></a>
<ul class="dropdown-menu">

<!-- GET ALL AUTHORS HERE. HOW? -->

</ul>
</li>
</ul>
<p class="navbar-text navbar-right">Signed in as <a href="#" class="navbar-link">Guest User</a></p>
</div>
</div>
</nav>

问题是导航栏是一个局部 View ,所以它被所有 View 共享。我知道我们可以在 Controller 中执行类似 model.addAttribute("authors", authorRepository.findAll()) 之类的操作以将其传递给 View ,但由于导航栏是部分的,因此我必须在所有 Controller 中执行此操作。

在我看来,在 Ruby on Rails 中我会做这样的事情:

<% Author.all.each do |author| %>

然后在我的导航栏部分 View 中读取所有作者的姓名。

但是对于 Spring,我不知道该怎么做。

你能帮我吗?

这是我的作者库(它扩展了 CrudRepository,所以我有一个作者的 findAll 方法):

package com.example.repository;

import com.example.domain.Author;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AuthorRepository extends CrudRepository<Author, Long> {

}

作者域:

package com.example.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.List;

@Entity
public class Author {

@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;

// Posts
@OneToMany(mappedBy = "author")
private List<Post> posts;

private Author() {}

public Author(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "Author{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}

最佳答案

您可以使用 @ModelAttribute Controller 中的注释:

@ModelAttribute methods are used to populate the model with commonly needed attributes for example to fill a drop-down with states or with pet types, or to retrieve a command object like Account in order to use it to represent the data on an HTML form.

@ModelAttribute("authors")
public Iterable<Author> getAuthors(){
return authorRepository.findAll();
}

这会将 authors 模型属性添加到您的 Spring MVC 模型中。如果您将它放在您的 Controller 中,您将在您的模板中自动使用此属性。

如果您需要在许多 Controller 上使用此模型属性,请查看此处所述的 @ControllerAdvice 注释:

@ModelAttribute methods can also be defined in an @ControllerAdvice-annotated class and such methods apply to many controllers. See the the section called “Advising controllers with @ControllerAdvice and @RestControllerAdvice section for more details.

关于java - Spring - 如何在局部 View 中访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39624814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com