Trying to pass Custom made objects using to th:value from View to Controller but only able to pass Java's primitive Data types through th:value. Is there any way I can send complex objects through th:value
I am new to spring and trying out some random things. It is a simple web application.
I have a form for creating a team. The form includes a text field for a team name and a bunch of checkboxes to select the team members. For each selected checkbox the corresponding member should be added into the team list.
I tried using thymeleaf's th:value attribute to pass objects from View to Controller. But I can only pass String values using th:value
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Create your Team</title>
</head>
<body>
<h1>Create your dream team</h1>
<form method="post" th:object="${team}">
<span>Name of the team</span>
<input type="text" th:field="*{name}">
<div th:each="player : ${playersList}">
<input type="checkbox" name="players" th:value="${player}" th:valueType="com.example.kkvamshee.Cricket.Player">
<span th:text="${player.name}">Player Name</span>
</div>
<button type="submit">Submit your team</button>
</form>
</body>
</html>
CreateTeamController.java
package com.example.kkvamshee.Cricket.web;
import com.example.kkvamshee.Cricket.Player;
import com.example.kkvamshee.Cricket.Team;
import com.example.kkvamshee.Cricket.data.JdbcPlayerRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/create-your-team")
public class CreateTeamController {
....
@GetMapping
public String showSelectTeamPage(Model model) {
List<Player> playersList = playerRepo.findAll();
model.addAttribute("playersList", playersList);
model.addAttribute("team", new Team());
return "teamCreationForm";
}
.....
}
Team.java
package com.example.kkvamshee.Cricket;
import lombok.Data;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
@Data
public class Team {
@NotNull private String name;
@Size(min = 3, max = 3, message = "You can only have 3 members on the team")
private List<Player> players;
}
Field error in object 'team' on field 'players': rejected value [Player(name=vamshee, age=20, exp=1),Player(name=messi, age=32, exp=18),Player(name=ronaldo, age=34, exp=18)]; codes [typeMismatch.team.players,typeMismatch.players,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [team.players,players]; arguments []; default message [players]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.List' for property 'players'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.example.kkvamshee.Cricket.Player' for property 'players[0]': no matching editors or conversion strategy found]
At the end of thid error log, there is "no matching editors or conversion strategy found".
I tried changing the th:valueType to Integer and it successfully casted corresponding text into Integer values. But i get this casting error doing the same thing with Team object or any other custom made object for instance.
Thymeleaf can know which object you are working with especially if you have it mapped correctly and accepting it appropriately. Look at the test I did with your code.
Please watch how the action is introduced in the form.



If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With