Good evenning i no longer have a solution ..Ive been hesitating to ask for help but Im litteraly at a dead end . Im working on a Spring boot 2.0.5 Spring MVC 5.0.9, ThymeLeaf 3.0.9 project that needs to be delievered in few weeks ..I have encountered a problem for some weeks now ...did my research and tried every possible solution and I still have the same problem . In Fact, my controller does not bind my model variables to my view ...it always renders "EL1007E: Property or field 'fieldName' cannot be found on null" .. Ive litteraly tried everything(since my code is just fine )..
Using ModelAndView instead of String for rendreding web pages
mvn clean/mvn install /mvn dependency:resolve ...every command i found helpfull
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
data-layout-decorate="~{index}">
<head>
<meta charset="UTF-8" />
<title>Page test</title>
</head>
<body>
<div data-layout-fragment="content">
<div class="container">
<div class="row">
<div class="col-sm-6" align="center">
<form th:action="@{/consulter}" method="get"
th:object="${paramSociete}">
<input type="text" name="nomSociete" class="form-control"
placeholder="AMEN BANK" />
<button type="submit">Clique moi !!</button>
</form>
<br /> <br /> <br /> <br />
<div>
<div>
<label>Nom Banque :</label><Label th:inline="text">
[[${paramSociete.nomSociete}]]</Label>
</div>
<div>
<label>Reference msg:</Label><Label th:inline="text">[[${paramSociete.initialMsg}]]</Label>
</div>
<div>
<label>chemin dacces:</label> <Label th:inline="text">[[${paramSociete.pathMsgEmis}]]</Label>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
@Controller
public class ParamSocieteController {
@Autowired
private ParamSocieteServiceImpl societeServiceImpl;
@Autowired
public void setSocieteServiceImpl(ParamSocieteServiceImpl societeServiceImpl) {
this.societeServiceImpl = societeServiceImpl;
}
@RequestMapping(value="/")
public String showTest() {
System.out.println("Here we go !!");
return "ThymeTest";
}
@RequestMapping(value = "/consulter")
public String afficherAmenBank(Model model, String nomSociete) {
ParamSociete societe = societeServiceImpl.findSociete(nomSociete);
if (societe == null) {
model.addAttribute("paramSociete", new ParamSociete());
} else {
model.addAttribute("nomSociete", nomSociete);
model.addAttribute("paramSociete", societe);
System.out.println(societe.getNomSociete());
System.out.println(societeServiceImpl.findSociete(societe.getNomSociete()).toString());
}
return "ThymeTest";
}
}
so i did nothing in my controller but did this in my view : I tested if my object existed with th:if
<div th:if="${paramSociete}">
<div>
<label>Nom Banque :</label><Label th:inline="text">
[[${paramSociete.nomSociete}]]</Label>
</div>
<div>
<label>Reference msg:</Label><Label th:inline="text">[[${paramSociete.initialMsg}]]</Label>
</div>
<div>
<label>chemin dacces:</label> <Label th:inline="text">[[${paramSociete.pathMsgEmis}]]</Label>
</div>
</div>
OK. So the problem is dead simple. Your view has this line of code:
${paramSociete.nomSociete}
So it tries to display the property nomSociete
of the model attribute paramSociete
. The error message tells you
Property or field 'nomSociete' cannot be found on null
So paramSociete
is null. Which means that there is no such model attribute. Let's check. Have you added such an attribute in the model before displaying that page? The method of your controller which is mapped to the URL in the browser location bar only has
@RequestMapping(value="/")
public String showTest() {
System.out.println("Here we go !!");
return "ThymeTest";
}
So it displays your view, but no, there is no attribute at all in the model. Which explains that paramSociete
is null.
As simple as that. If you want a page to display the name of a company, that company must exist.