EL1007E:找不到属性或字段“名称”为空

时间:2020-07-06 15:50:05

标签: spring thymeleaf

我正在将Spring框架与thymeleaf和mysql一起使用。

我遇到了错误

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;

0 个答案:

没有答案
相关问题