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)