无法在Thymeleaf中迭代List。

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

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,而不是Iterable,因为我在ProjectRepository中对它进行了更改,继承了CrudRepository:

@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 &#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 :

&lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;

Here is my thymeleaf html view :

&lt;tbody&gt;
    &lt;tr th:each=&quot;proj : ${projectList}&quot;&gt;
        &lt;td th:text=&quot;${proj.name}&quot;/&gt;
        &lt;td th:text=&quot;${proj.stage}&quot;/&gt;
        &lt;td th:text=&quot;${project.description}&quot;/&gt;
    &lt;/tr&gt;
&lt;/tbody&gt;

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(&quot;/&quot;)
	public String showHome(Model model) {
		List&lt;Project&gt; projects = projRepo.findAll();
		model.addAttribute(&quot;projectList&quot;,projects);
		return &quot;home&quot;;
	}
}

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&lt;Project,Integer&gt; {

	@Override
	List&lt;Project&gt; 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 :

无法在Thymeleaf中迭代List。

答案1

得分: 2

Sure, here's the translation:

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

更改为

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

Change

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

To

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:

确定