0

So I am working on a small project (I'm a newbie) and I have been unable to access my fields declared in my model class from my view in Thymeleaf. I checked the documentation and I am certain I am doing everything as it should be but for some reason, IntelliJ gives me a "cannot resolve field" error. I checked here and there online and saw that It used to be an issue with IntelliJ which was supposedly fixed in their 2017.X version but I still get a similar error(I am using IntelliJ 2022.2.4). Can anyone please help?

This is my view page and I get an error at the level of th:field="*{loginDate}"

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>ADD NEW RECORD</title>
    <link rel="stylesheet" type="text/css" th:href = "@{/webjars/bootstrap/css/bootstrap.min.css}"/>
</head>
<body>
<div class="container-fluid text-center">
    <div><h2>ADD NEW RECORD</h2></div>

    <form th:action="@{students/save}" th:object="${newStudent}" method="post" >
        <div class ="border">
            <div class="form-group row">
                <label class="col-sm-4 col-form-label">Date</label>
                <div class="col-sm-8">
                    <input type="date" **th:field="*{loginDate}"** class="form-control">


                </div>


            </div>

        </div>


    </form>




</div>

</body>
</html>

Here is my controller:

package com.sfsb.studentsSFSB.students.controller;

import com.sfsb.studentsSFSB.students.model.Students;
import com.sfsb.studentsSFSB.students.service.StudentsServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class StudentController {
    private StudentsServiceImpl service;

    public StudentController(StudentsServiceImpl service) {
        this.service = service;
    }

    @GetMapping("")
    public String showHomePage(){
        return "index";
    }

    @GetMapping("/students/new")
    public String showForm(Model model){
        model.addAttribute("newStudent", new Students());
        return "student_form";

    }

    @GetMapping("/students")
    public String showStudentsList(Model model){
        List<Students> listStudents =  service.findAll();
        model.addAttribute("listStudents", listStudents);

        return "students";
    }




}

And here is my model class

package com.sfsb.studentsSFSB.students.model;

import jakarta.persistence.*;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Entity
@Table(name = "students")
public class Students {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "FirstName")
    private String firstName;

    @Column(name = "LastName")
    private String lastName;

    @Column(name = "Date")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate loginDate;

    @Column(name = "LoginTime")
    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime loginTime;

    @Column(name = "BreakPeriod")
    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime breakPeriod;

    @Column(name = "LogoutTime")
    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime logoutTime;

    @Column(name = "HoursWorkedPerDay")
    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime workHoursPerDay;

    @Column(name = "Task")
    private String task;

    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime workHoursPerWeek;

    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime totalHoursForCurrentContract;

    //@DateTimeFormat(iso = DateTimeFormat.ISO.TIME) if above does not work
    @DateTimeFormat(pattern = "HH:mm")
    private LocalTime hoursLeftOnCurrentContract;


    //Method to calculate work hours per day based on login,break and logout values
    public LocalTime calculateWorkHoursPerDay(){
        //DateTimeFormatter Formatter = DateTimeFormatter.ofPattern("H:mm");
        Duration breakDuration = Duration.between(LocalTime.MIDNIGHT, breakPeriod);
        Duration workDuration = Duration.between(loginTime,logoutTime).minus(breakDuration);
        String workHoursInString = LocalTime.MIDNIGHT.plus(workDuration).toString();
        workHoursPerDay = LocalTime.parse(workHoursInString);
        return workHoursPerDay;

    }

}

Kevin
  • 3
  • 2
  • do you have public getters/setters for the Students class ? Also, you haven't shown us the code for the students/save endpoint and that's where the problem seems to be. – dsp_user Dec 11 '22 at 09:52
  • I think I forgot to include getters/setters in my model class. I just added that and that seem to have resolved the issue. Can't believe I spent an entire night on this. Thanks alot!! – Kevin Dec 11 '22 at 17:24

0 Answers0