无法在Thymeleaf中迭代List。

huangapple go评论89阅读模式
英文:

Cannot iterate over List in Thymeleaf

问题

我在尝试从控制器中显示一个List中的对象时遇到了错误。我正在使用Spring Boot框架。

以下是错误信息:

  1. org.springframework.expression.spel.SpelEvaluationException: EL1007E: 无法在null上找到属性或字段'description'

我正在使用Spring Boot 2.3.4。以下是maven pom.xml文件中的依赖项:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.3.4.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

这是我的Thymeleaf HTML视图:

  1. <tbody>
  2. <tr th:each="proj : ${projectList}">
  3. <td th:text="${proj.name}"/>
  4. <td th:text="${proj.stage}"/>
  5. <td th:text="${project.description}"/>
  6. </tr>
  7. </tbody>

我的Project @Entity工作得很好:

  1. @Entity
  2. public class Project {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.AUTO)
  5. private int projectID;
  6. private String name;
  7. private String stage; //incomplete, not started, in progress
  8. private String description;
  9. ...
  10. }

这是将我的Project数据添加到模型中的控制器:

  1. @Controller
  2. public class HomeController {
  3. @Autowired
  4. iProjectRepo projRepo;
  5. @GetMapping("/")
  6. public String showHome(Model model) {
  7. List<Project> projects = projRepo.findAll();
  8. model.addAttribute("projectList", projects);
  9. return "home";
  10. }
  11. }

findAll()返回的是一个List,而不是Iterable,因为我在ProjectRepository中对它进行了更改,继承了CrudRepository:

  1. @Repository
  2. public interface iProjectRepo extends CrudRepository<Project, Integer> {
  3. @Override
  4. List<Project> findAll(); //默认情况下,findAll()返回一个Iterable。我们重写它并将返回类型更改为List。
  5. }

最后,这是我的项目结构:(图片链接略)

英文:

I am getting an error when trying to display objects from a List in my controller. I am using the Spring Boot Framework.

Here is the error :

  1. org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field &#39;description&#39; cannot be found on null

I am using Spring Boot 2.3.4. below is the dependency in the maven pom.xml file :

  1. &lt;parent&gt;
  2. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  3. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  4. &lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
  5. &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  6. &lt;/parent&gt;

Here is my thymeleaf html view :

  1. &lt;tbody&gt;
  2. &lt;tr th:each=&quot;proj : ${projectList}&quot;&gt;
  3. &lt;td th:text=&quot;${proj.name}&quot;/&gt;
  4. &lt;td th:text=&quot;${proj.stage}&quot;/&gt;
  5. &lt;td th:text=&quot;${project.description}&quot;/&gt;
  6. &lt;/tr&gt;
  7. &lt;/tbody&gt;

My Project @Entity works just fine :

  1. @Entity
  2. public class Project {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.AUTO)
  5. private int projectID;
  6. private String name;
  7. private String stage; //incomplete, not started, in progress
  8. private String description;
  9. ...
  10. }

And here is the controller that is taking my Project data and attempting to add it to the model.

  1. @Controller
  2. public class HomeController {
  3. @Autowired
  4. iProjectRepo projRepo;
  5. @GetMapping(&quot;/&quot;)
  6. public String showHome(Model model) {
  7. List&lt;Project&gt; projects = projRepo.findAll();
  8. model.addAttribute(&quot;projectList&quot;,projects);
  9. return &quot;home&quot;;
  10. }
  11. }

The findAll() is returning a List<Project> instead of an Iterable because I changed it in the ProjectRepository extending CrudRepository

  1. @Repository
  2. public interface iProjectRepo extends CrudRepository&lt;Project,Integer&gt; {
  3. @Override
  4. List&lt;Project&gt; findAll(); //the findAll() by default returns an Iterable. we override it and change the return type to be a list.
  5. }

And finally, here is my project structure :

无法在Thymeleaf中迭代List。

答案1

得分: 2

Sure, here's the translation:

  1. th:text=&quot;${project.description}&quot;/&gt;

更改为

  1. th:text=&quot;${proj.description}&quot;/&gt;
英文:

Change

  1. th:text=&quot;${project.description}&quot;/&gt;

To

  1. th:text=&quot;${proj.description}&quot;/&gt;

huangapple
  • 本文由 发表于 2020年10月13日 01:34:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64322660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定