spring - EL1007E: 在 null 上找不到属性或字段“名称”
问题描述
我正在使用带有 thymeleaf 和 mysql 的 Spring 框架。
我收到错误
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
此错误是由 html 代码中的 ${selectProject.client.name} 引起的。
我编写了一个表单,允许用户添加有关项目的信息。作为表单的一部分,有一个不同项目名称的下拉列表。但是,在下拉列表中,我还希望它在项目名称旁边给出客户的名称。
我的html代码:
<form action="#" id="informationForm" method="post" th:action="@{/add-information}" th:object="${information}">
<div class="row">
<div class="form-group col-12">
<label class="col-form-label" for="project">Project</label>
<select class="form-control" id="project" th:field="*{project}">
<option value="0">Please select a project</option>
<option th:each="selectProject : ${projectList}"
th:text="${selectProject.client.name} + ' - ' +${selectProject.name}"
th:value="${selectProject.id}"></option>
</select>
这是信息控制器:
@Controller
@Slf4j
@SessionAttributes({"project", "information"})
public class InformationController {
private final InformationService informationService;
private final ProjectsService projectsService;
@Autowired
public InformationController(final ProjectsService projectsService,
final InformationService informationService) {
this.projectsService = projectsService;
this.informationService = informationService;
}
@ModelAttribute("information")
public Information getInformation() {
return informationService.createEmptyInformation();
}
@ModelAttribute("projectList")
public List<Project> getProjects() {
return projectsService.getProjects();
}
@GetMapping("add-information")
public String showAddInformationForm(Model model, @ModelAttribute("information") Information information) {
information = informationService.createEmptyInformation();
model.addAttribute(information);
return "add-information";
}
@PostMapping("add-information")
public String addInformationForm(@Valid Information information, BindingResult result, Model model) {
if (result.hasErrors()) {
return "add-information";
}
Long id = informationService.createInformationFromInput(information);
return "redirect:/homepage";
}
信息服务中的相关方法有:
public Information createEmptyInformation() {
return new Information();
}
public Long createInformationFromInput(Information information) {
try {
informationDao.save(information);
} catch (Exception ex) {
log.error("Failed to save information for {} because of {}", information.getId(), ex.getLocalizedMessage());
}
return information.getId();
}
在项目服务中:
public List<Project> getProjects() {
return projectDao.findAll();
}
客户与项目具有多对一关系
项目领域:
@Getter
@Setter
@Entity
@Table(name = "projects")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id")
private Client client;
@Column(name = "name")
private String name;
客户域:
@Getter
@Setter
@Entity
@Table(name = "clients")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Name is required")
@Size(min = 5, max = 80, message = "Name must be between 5 and 80 characters")
@Column(name = "name", nullable = false)
private String name;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "client_id")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
private List<Project> projects;
信息域:
@Getter
@Setter
@Entity
@Table(name = "information")
public class Information {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn(name = "project_id")
private Project project;
@Column(name = "date")
private Date date;
解决方案
推荐阅读
- c++ - 如何在 cpp 应用程序中启用 openGL 调用跟踪?
- angular - 如何使用 algolia REST api 获取所有记录
- php - 无法删除标题“变化:用户代理”
- apache - Xampp 子域无法通过 Edge 和 Firefox 访问?
- c - 在C中找到具有非常大的幂和除数的模数
- scala - List("foo") foreach 打印其他同义词?
- react-native - 即使在应用程序关闭后如何使 socket-io 工作
- python - 概括 Numpy 切片 Python
- jquery - 通过jquery比较两个html输入框
- javascript - 数组属性在JS对象中是如何工作的