D
D
Danil Andreev2020-02-03 09:35:28
Java
Danil Andreev, 2020-02-03 09:35:28

How to transfer an object to the form, make changes and send back?

Good day. I want to apologize right away for a possibly stupid question and for a bad, in my opinion, implementation. There is such a situation, I want to transfer information about the user (last name, first name) and one more field (in which the required parameter will be selected) to the form.
In my case, I have two Student objects (which is taken from the database and stores the last name and first name) and the Absent list (which must first store two values, and after the user makes his choice, one).
At first I wanted to implement this using Map, but due to my inexperience, I ran into the problem of displaying last names in alphabetical order (this is a prerequisite), so I created an intermediate DAO (probably should have called it DTO, since it is only responsible for transporting data ), which in turn stores a list of objects of type AbsentWithStudent , which stores the necessary data for the Student object and a list of Absent.

Controller:

@Autowired
    private APP_UserRepository userRepository;

    @Autowired
    private TeachingRepository teachingRepository;

    @Autowired
    private GroupRepository groupRepository;

    @Autowired
    private SubjectRepository subjectRepository;

    @Autowired
    private AbsentRepository absentRepository;

   @PostMapping("/teacher/createLesson")
    public String postCreateLessonChoose(Model model, Principal principal, @RequestParam(name = "goodGroup") String goodGroup, @RequestParam(name = "goodSubject") String goodSubject){


        //Select info from database
        APP_User user = userRepository.findByUsername(principal.getName()).get();
        Group group = groupRepository.findByName(goodGroup).get();
        Subject subject = subjectRepository.findByName(goodSubject).get();
        List<APP_User> students = userRepository.findByGroup(group);

        //Create list with absent
        List<Absent> absents = new ArrayList<>();
        absents.add(absentRepository.findByName("Присутствует").get());     // be careful!
        absents.add(absentRepository.findByShortname("НБ").get());

        //Take current date
        DateTimeFormatter d = DateTimeFormatter.ofPattern("dd.MM.yyyy");
        String date = LocalDate.now().format(d);

        //DAO for form in frontend
        createAbsentDAO absentDAO = new createAbsentDAO();
        for (int i = 0; i < students.size(); i++) {
            APP_User student = students.get(i);
            absentDAO.addObjectToList(new AbsentWithStudent(student.getId(), student.getFirstname(), student.getLastname(), absents, date));
        }

        //Sent DAO to frontend
        model.addAttribute("absentDAO", absentDAO);


        return "/teacher/createLesson";
    }


The page that should be created as a result of the controller's work:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>Текст</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
</head>
<body>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>

<div class="row justify-content-center">



    <div class="col-6 mt-4 shadow p-3 mb-5 ml-3 mr-3 bg-white rounded">

        <p class="mt-1 mb-1 font-weight-light text-primary h2 mx-auto bg-white rounded"></p>

        <form th:action="@{/teacher/createLesson/confirm}" method="post" modelAttribute="absentDAO">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>Студент</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>




                <td th:each="dao,itemStat : ${absentDAO.list}" th:text="*{dao.lastname}" th:field="*{dao.lastname}" th:value="*{dao.lastname}"></td>


                <td th:each="list : ${absentDAO.list.absentList}">
                    <select>
                        <option th:value="${list.shortname}" th:text="${list.shortname}"></option>
                    </select>
                </td>

<!--                                             HIDDEN!!!!!-->
<!--                <input type="hidden" name="group" th:field="*{group}">-->

                </tbody>
            </table>

            <button type="submit" class="btn btn-primary mx-auto">Создать</button>
        </form>
    </div>


</div>





</body>
</html>


Model that stores the data of the Student object and the Absent list
public class AbsentWithStudent{

    private int id;
    private String firstname;
    private String lastname;
    private List<Absent> absentList;
    private String date;


    public AbsentWithStudent(int id, String firstname, String lastname, List<Absent> absentList, String date) {
        this.id = id;
        this.firstname = firstname;
        this.lastname = lastname;
        this.absentList = absentList;
        this.date = date;
    }



    public AbsentWithStudent() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public List<Absent> getAbsentList() {
        return absentList;
    }

    public void setAbsentList(List<Absent> absentList) {
        this.absentList = absentList;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}


DAO
public class createAbsentDAO {


    private List<AbsentWithStudent> list = new ArrayList<>();

    public void addObjectToList(AbsentWithStudent student){
        this.list.add(student);
    }

    public createAbsentDAO() {
    }

    public List<AbsentWithStudent> getList() {
        return list;
    }

    public void setList(List<AbsentWithStudent> list) {
        this.list = list;
    }
}


When I make a request to the page, I get this error:
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates//teacher/createLesson.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates//teacher/createLesson.html]")
  at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
...
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "absentDAO.list.absentList" (template: "/teacher/createLesson" - line 69, col 21)
  at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
  at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
  at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
  ... 87 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "absentDAO.list.absentList" (template: "/teacher/createLesson" - line 69, col 21)
  at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
...
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'absentList' cannot be found on object of type 'java.util.ArrayList' - maybe not public or not valid?
  at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)


Thanks in advance and any help would be appreciated

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jacen11, 2020-02-03
@Jacen11

it is also written there, look at absentDAO.list.absentList in the
list template, it's just a list, where does it take some other field or method? list is a ready-made list that will be iterated over. It's also weird to call an element "list" when it's not a list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question