0

I want to make an application with 3 controllers. I need to pass the model object between controllers in the application. But when I try to do this, [@ModelAttribute("ticketObj") Ticket ticket] object in the 3rd controller returns a null object.

Ticket Class

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Ticket {
    int id;
    private InvoiceType invoiceType;
    private Company company;
    private Individual individual;
    private ParticipantDTO participantDTO;
    int numberOfParticipants;
}

First Class

@GetMapping("/firstPage")
public String firstPage(Model model) {
    Ticket ticket = new Ticket();

    ticket.setCompany(new Company());
    ticket.setIndividual(new Individual());

    model.addAttribute("ticketObj", ticket);
    return "firstpage";
}

firstpage.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<head>
    <link rel="stylesheet" type="text/css" th:href="@{/assets/css/form-styles.css}" />
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form id="form" action="#" th:action="@{/secondPage}" method="post" th:object="${ticketObj}">
    <!-- Some inputs -->
    </form>
</body>
</html>

Second Controller

@PostMapping("/secondPage")
public String secondPage(@ModelAttribute("ticketObj") Ticket ticket, Model model) {

    System.out.println(ticket);

    ParticipantDTO participantDTO = new ParticipantDTO(new ArrayList<>());

    for(int i = 0; i < ticket.getNumberOfParticipants(); i++) {
        participantDTO.addParticipant(new Participant());
    }
    ticket.setParticipantDTO(participantDTO);

    model.addAttribute("ticketObj", ticket);
    return "secondpage";
}

System.out.println(ticket) result (This part works as it should.):

Ticket(id=0, invoiceType=null, company=Company(fname=, lname=, companyName=, invoiceAddress=, city=, taxId=, taxOffice=, kdv=false), individual=Individual(fname=sv, lname=sd, nationalId=132123123, [email protected], phone=555-666-7777, address=s dsdvsdv sdfnlasn asdhaousdhouas), participantDTO=null, numberOfParticipants=2)

secondpage.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<head>
    <link rel="stylesheet" type="text/css" th:href="@{/assets/css/form-styles.css}" />
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form id="form" action="#" th:action="@{/thirdPage}" method="post" th:object="${ticketObj.participantDTO}">
    <!-- Some inputs -->
    </form>
</body>
</html>

Third Controller

@PostMapping("/thirdPage")
public String thirdPage(@ModelAttribute("ticketObj") Ticket ticket) {
    System.out.println(ticket);
    return "thirdpage";
}

sout(ticket) (That part is not working.):

I couldn't get inputs I passed in the html section here.

Ticket(id=0, invoiceType=null, company=null, individual=null, participantDTO=null, numberOfParticipants=0)

1 Answer 1

1

The only thing model.addAttribute("ticketObj", ticket); does in the given example code is add a ticketObj when rendering the page using thymeleaf. After that there is nothing in spring or thymeleaf that keeps said object in memory somewhere. As such the field you define in form, which you ommited, are the only fields that wil be filled in your next controller.

With the supplied code and what it's meant to do it's hard to suggest a solution though, in general I would suggest keeping your form inside 1 post and potentially filling dropdown and such using ajax if you need serverside information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.