英文:
Cannot iterate over List in Thymeleaf
问题
我在尝试从控制器中显示一个List中的对象时遇到了错误。我正在使用Spring Boot框架。
以下是错误信息:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: 无法在null上找到属性或字段'description'
我正在使用Spring Boot 2.3.4。以下是maven pom.xml文件中的依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
这是我的Thymeleaf HTML视图:
<tbody>
<tr th:each="proj : ${projectList}">
<td th:text="${proj.name}"/>
<td th:text="${proj.stage}"/>
<td th:text="${project.description}"/>
</tr>
</tbody>
我的Project @Entity工作得很好:
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int projectID;
private String name;
private String stage; //incomplete, not started, in progress
private String description;
...
}
这是将我的Project数据添加到模型中的控制器:
@Controller
public class HomeController {
@Autowired
iProjectRepo projRepo;
@GetMapping("/")
public String showHome(Model model) {
List<Project> projects = projRepo.findAll();
model.addAttribute("projectList", projects);
return "home";
}
}
findAll()返回的是一个List
@Repository
public interface iProjectRepo extends CrudRepository<Project, Integer> {
@Override
List<Project> findAll(); //默认情况下,findAll()返回一个Iterable。我们重写它并将返回类型更改为List。
}
最后,这是我的项目结构:(图片链接略)
英文:
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 :
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'description' cannot be found on null
I am using Spring Boot 2.3.4. below is the dependency in the maven pom.xml file :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Here is my thymeleaf html view :
<tbody>
<tr th:each="proj : ${projectList}">
<td th:text="${proj.name}"/>
<td th:text="${proj.stage}"/>
<td th:text="${project.description}"/>
</tr>
</tbody>
My Project @Entity works just fine :
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int projectID;
private String name;
private String stage; //incomplete, not started, in progress
private String description;
...
}
And here is the controller that is taking my Project data and attempting to add it to the model.
@Controller
public class HomeController {
@Autowired
iProjectRepo projRepo;
@GetMapping("/")
public String showHome(Model model) {
List<Project> projects = projRepo.findAll();
model.addAttribute("projectList",projects);
return "home";
}
}
The findAll() is returning a List<Project> instead of an Iterable because I changed it in the ProjectRepository extending CrudRepository
@Repository
public interface iProjectRepo extends CrudRepository<Project,Integer> {
@Override
List<Project> findAll(); //the findAll() by default returns an Iterable. we override it and change the return type to be a list.
}
And finally, here is my project structure :
答案1
得分: 2
Sure, here's the translation:
将
th:text="${project.description}"/>
更改为
th:text="${proj.description}"/>
英文:
Change
th:text="${project.description}"/>
To
th:text="${proj.description}"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论