Thymeleaf Model attribute not present












1














Situation



I am using SpringBoot application with the Thymeleaf templating engine. I posted a simplified version of the code that is being involved. A controller is adding an attribute to the Model object and then returning DeferredResult. At the end the attributes form the model are used in the Thymeleaf template to render them on the page.



Problem



I know if the object would not be added to the model it would throw an exception in the Thymeleaf template, because of the missing guard block or th:if. But I am setting initialized object before any code is being executed. I am also logging the value of the model for debugging purposes and its always present after the calculation. So model with appropriate key is set but at the point of rendering thymeleaf the key in model returns null value. This only happens under a heavy load. In the production system or when simulating load with more than 30 concurrent requests.



It happens 1~2 times in a 100 requests.



I store multiple objects, in the model. They are randomly null.



I also tried logging all the values with following code. All other data seems to be fine and present, only single object in the case below missing one is "model_key".



<table id="model-attributes">
<tr th:each="var : ${#vars}">
<td th:text="${var.key}"></td>
<td th:text="${var.value}"></td>
</tr>
</table>


Question



Did anyone else experienced similar issues. Can it be the Thymeleaf caching issue?



Code



Controller



@GetMapping(value = "/path") 
public DeferredResult<String> processRequest(Model model) throws Exception {

// To make sure the model is not null add it already before any computation
model.addAttribute("model_key", new ModelValue());
ModelValue modelValue = calculateModel();

// For debugging purposes
log("Model Value " + modelValue);

// Add the actual model
if(modelValue != null) {
model.addAttribute("model_key", modelValue);
} else {
log.error("Model was empty");
}
deferredResult.setResult("template_name");
}


Thymeleaf template



<p th:text="${model_key.id}">


Exception log



2018-11-12T07:45:28.691Z ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] [http-nio-8080-exec-6] : Servlet.service() for servlet dispatcherServlet threw exception
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:220)
org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)









share|improve this question






















  • I don't think you should be using DeferredResult<String> with a model attribute like you are. What is the reason you're doing it like that?
    – Metroids
    Nov 12 at 19:29
















1














Situation



I am using SpringBoot application with the Thymeleaf templating engine. I posted a simplified version of the code that is being involved. A controller is adding an attribute to the Model object and then returning DeferredResult. At the end the attributes form the model are used in the Thymeleaf template to render them on the page.



Problem



I know if the object would not be added to the model it would throw an exception in the Thymeleaf template, because of the missing guard block or th:if. But I am setting initialized object before any code is being executed. I am also logging the value of the model for debugging purposes and its always present after the calculation. So model with appropriate key is set but at the point of rendering thymeleaf the key in model returns null value. This only happens under a heavy load. In the production system or when simulating load with more than 30 concurrent requests.



It happens 1~2 times in a 100 requests.



I store multiple objects, in the model. They are randomly null.



I also tried logging all the values with following code. All other data seems to be fine and present, only single object in the case below missing one is "model_key".



<table id="model-attributes">
<tr th:each="var : ${#vars}">
<td th:text="${var.key}"></td>
<td th:text="${var.value}"></td>
</tr>
</table>


Question



Did anyone else experienced similar issues. Can it be the Thymeleaf caching issue?



Code



Controller



@GetMapping(value = "/path") 
public DeferredResult<String> processRequest(Model model) throws Exception {

// To make sure the model is not null add it already before any computation
model.addAttribute("model_key", new ModelValue());
ModelValue modelValue = calculateModel();

// For debugging purposes
log("Model Value " + modelValue);

// Add the actual model
if(modelValue != null) {
model.addAttribute("model_key", modelValue);
} else {
log.error("Model was empty");
}
deferredResult.setResult("template_name");
}


Thymeleaf template



<p th:text="${model_key.id}">


Exception log



2018-11-12T07:45:28.691Z ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] [http-nio-8080-exec-6] : Servlet.service() for servlet dispatcherServlet threw exception
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:220)
org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)









share|improve this question






















  • I don't think you should be using DeferredResult<String> with a model attribute like you are. What is the reason you're doing it like that?
    – Metroids
    Nov 12 at 19:29














1












1








1







Situation



I am using SpringBoot application with the Thymeleaf templating engine. I posted a simplified version of the code that is being involved. A controller is adding an attribute to the Model object and then returning DeferredResult. At the end the attributes form the model are used in the Thymeleaf template to render them on the page.



Problem



I know if the object would not be added to the model it would throw an exception in the Thymeleaf template, because of the missing guard block or th:if. But I am setting initialized object before any code is being executed. I am also logging the value of the model for debugging purposes and its always present after the calculation. So model with appropriate key is set but at the point of rendering thymeleaf the key in model returns null value. This only happens under a heavy load. In the production system or when simulating load with more than 30 concurrent requests.



It happens 1~2 times in a 100 requests.



I store multiple objects, in the model. They are randomly null.



I also tried logging all the values with following code. All other data seems to be fine and present, only single object in the case below missing one is "model_key".



<table id="model-attributes">
<tr th:each="var : ${#vars}">
<td th:text="${var.key}"></td>
<td th:text="${var.value}"></td>
</tr>
</table>


Question



Did anyone else experienced similar issues. Can it be the Thymeleaf caching issue?



Code



Controller



@GetMapping(value = "/path") 
public DeferredResult<String> processRequest(Model model) throws Exception {

// To make sure the model is not null add it already before any computation
model.addAttribute("model_key", new ModelValue());
ModelValue modelValue = calculateModel();

// For debugging purposes
log("Model Value " + modelValue);

// Add the actual model
if(modelValue != null) {
model.addAttribute("model_key", modelValue);
} else {
log.error("Model was empty");
}
deferredResult.setResult("template_name");
}


Thymeleaf template



<p th:text="${model_key.id}">


Exception log



2018-11-12T07:45:28.691Z ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] [http-nio-8080-exec-6] : Servlet.service() for servlet dispatcherServlet threw exception
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:220)
org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)









share|improve this question













Situation



I am using SpringBoot application with the Thymeleaf templating engine. I posted a simplified version of the code that is being involved. A controller is adding an attribute to the Model object and then returning DeferredResult. At the end the attributes form the model are used in the Thymeleaf template to render them on the page.



Problem



I know if the object would not be added to the model it would throw an exception in the Thymeleaf template, because of the missing guard block or th:if. But I am setting initialized object before any code is being executed. I am also logging the value of the model for debugging purposes and its always present after the calculation. So model with appropriate key is set but at the point of rendering thymeleaf the key in model returns null value. This only happens under a heavy load. In the production system or when simulating load with more than 30 concurrent requests.



It happens 1~2 times in a 100 requests.



I store multiple objects, in the model. They are randomly null.



I also tried logging all the values with following code. All other data seems to be fine and present, only single object in the case below missing one is "model_key".



<table id="model-attributes">
<tr th:each="var : ${#vars}">
<td th:text="${var.key}"></td>
<td th:text="${var.value}"></td>
</tr>
</table>


Question



Did anyone else experienced similar issues. Can it be the Thymeleaf caching issue?



Code



Controller



@GetMapping(value = "/path") 
public DeferredResult<String> processRequest(Model model) throws Exception {

// To make sure the model is not null add it already before any computation
model.addAttribute("model_key", new ModelValue());
ModelValue modelValue = calculateModel();

// For debugging purposes
log("Model Value " + modelValue);

// Add the actual model
if(modelValue != null) {
model.addAttribute("model_key", modelValue);
} else {
log.error("Model was empty");
}
deferredResult.setResult("template_name");
}


Thymeleaf template



<p th:text="${model_key.id}">


Exception log



2018-11-12T07:45:28.691Z ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] [http-nio-8080-exec-6] : Servlet.service() for servlet dispatcherServlet threw exception
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'id' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:220)
org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)






spring-mvc spring-boot thymeleaf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 10:55









RenatoIvancic

5041024




5041024












  • I don't think you should be using DeferredResult<String> with a model attribute like you are. What is the reason you're doing it like that?
    – Metroids
    Nov 12 at 19:29


















  • I don't think you should be using DeferredResult<String> with a model attribute like you are. What is the reason you're doing it like that?
    – Metroids
    Nov 12 at 19:29
















I don't think you should be using DeferredResult<String> with a model attribute like you are. What is the reason you're doing it like that?
– Metroids
Nov 12 at 19:29




I don't think you should be using DeferredResult<String> with a model attribute like you are. What is the reason you're doing it like that?
– Metroids
Nov 12 at 19:29

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260644%2fthymeleaf-model-attribute-not-present%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260644%2fthymeleaf-model-attribute-not-present%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues