i start to use Spring MVC and i have a trouble. I want to get the value of a hashmap with the key. The object ProtoStatus contains a hashmap who i want to get value. I have this error :
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "protoStatus.status.get(30000)" (template: "protoStatusPage" - line 18, col 21)
public class ProtoStatus
{
public HashMap<String, String> status;
public void computeStatus()
{
this.status = new HashMap();
for (int i=30000; i<30032; i++)
{
this.status.put(String.valueOf(i), String.valueOf(ServerChecker.Check("192.168.0.1", i)));
}
}
public void setStatus(HashMap status)
{
this.status = status;
}
public HashMap getStatus()
{
return this.status;
}
public String getStatus(int key)
{
return (String) this.status.get(key);
}
}
The Spring MVC part :
@PostMapping("/")
public String submit(@ModelAttribute User user, @ModelAttribute ProtoStatus protoStatus)
{
protoStatus = new ProtoStatus();
protoStatus.computeStatus();
return "protoStatusPage";
}
And Finaly, in the template protoStatusPage.html, i want to get the value for key 30000:
<p th:text="${protoStatus.status.get(30000)}" />
Your status attribute is null by default. It only becomes non-null when you call computeStatus()
. But you're never calling it on the ProtoStatus used by the view:
protoStatus = new ProtoStatus();
protoStatus.computeStatus();
Instead, you create a different one, and call computeStatus() on that one, that is then immediately eligible to garbage collection.
Also, you have a Map<String, String>
, but your getStatus()
method, and the expression in your view, tries to get something out of it usig a key of type Integer. That will always return null.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments