napisalem taki o to form:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1"></meta>
<title>Plan a visit!</title>
</head>
<body>
<form action="visit.do" th:action="@{/visit.do}" th:object="${Visit}" method="post">
<label for="date">Date:</label>
<input type="text" th:field="*{date}"/>
<label for="pet">Pet:</label>
<select th:field="*{pet}" th:remove="all-but-first">
<option th:each="pet : ${petList}" th:value="${pet}" th:text="${pet.petName}" ></option>
</select>
<label for="vet">Vet:</label>
<select th:field="*{vet}" th:remove="all-but-first">
<option th:each="vet : ${vetList}" th:value="${vet}" th:text="${vet.firstName} +' '+ ${vet.lastName}" ></option>
</select>
<input type="submit"/>
</form>
</body>
</html>
odpowiadajacy mu kontroler:
@Controller
public class VisitController {
@Autowired
ServiceDao<Pet> petService;
@Autowired
@Qualifier("vetService")
ServiceDao<Vet> vetService;
@RequestMapping("/setVisit")
public String visit(Model model) {
List<Vet> vetList = vetService.getAll();
List<Pet> petList = petService.getAll();
model.addAttribute("vetList", vetList);
model.addAttribute("petList", petList);
model.addAttribute("Visit", new Visit());
return "visitForm";
}
@RequestMapping(value = "visit.do", method = RequestMethod.POST)
public String visitDo(@ModelAttribute(value="Visit") Visit visit, BindingResult result
) {
System.out.println(visit);
return "visitForm";
}
}
w widoku wygenerowanym motoda get wszystko jest ok, tzn wartosci z DB wczytuja sie do list i mozna w <select> wybrac odpowiednie encje. Jednak do kontrolera trafiaja nulle, a output z sysout'u jest :
Visit [id=0, date=1111-11-11, pet=null, vet=null]
chcialem zoabczyc co jest w modelu tak wiec zmienilem troche kontroler:
@RequestMapping(value = "visit.do", method = RequestMethod.POST)
public String visitDo(@ModelAttribute(value="Visit") Visit visit, BindingResult result, Model model
) {
System.out.println(model.toString());
System.out.println(visit);
return "visitForm";
}
i mam z tego taki output:
Field error in object 'Visit' on field 'pet': rejected value [Pet [id=1, petName=q, owner=null]]; codes [typeMismatch.Visit.pet,typeMismatch.pet,typeMismatch.pl.kaczynski.model.Pet,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [Visit.pet,pet]; arguments []; default message [pet]]; default message [Failed to convert property value of type [java.lang.String] to required type [pl.kaczynski.model.Pet] for property 'pet'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [pl.kaczynski.model.Pet] for property 'pet': no matching editors or conversion strategy found]
Field error in object 'Visit' on field 'vet': rejected value [Vet [id=1, firstName=Abdul, lastName=Koza]]; codes [typeMismatch.Visit.vet,typeMismatch.vet,typeMismatch.pl.kaczynski.model.Vet,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [Visit.vet,vet]; arguments []; default message [vet]]; default message [Failed to convert property value of type [java.lang.String] to required type [pl.kaczynski.model.Vet] for property 'vet'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [pl.kaczynski.model.Vet] for property 'vet': no matching editors or conversion strategy found]}
Visit [id=0, date=1111-11-11, pet=null, vet=null]
Mimo tego (chyba) dosc jasnego stacktrace'u nie jestem w stanie tego ugryzc.