I am writing a Spring MVC app (latest Spring) with the Thymeleaf template engine. All I need to do is display a page of text, but which text of course depends upon some factor.
Because its just text I am thinking I can use a Java String object as my model. I tried this, and through debugging I learned that apparently, the data is saved to the model and if I attempt to modify it, well, the model does not seem to notice.
Also, it seems that my getDecisionText() method (pasted below) is called right up front, before the web page hits the code. I was expecting that the getDecisionText() method would be called after I returned my view (in this case a Thymeleaf template name) when the data was needed.
Oh, I do have plans to fix my database schema but for now its not causing my issue.
I am thinking there must be a way to tell the model that the data is transient and to reload it each time it is needed? Or perhaps my design is just 100% wrong from the start?
Among several other things, here is what I tried for setting the model attribute:
@ModelAttribute("decisionText")
private String getDecisionText()
{
Logger.getGlobal().info(() -> "Returning Dec Text: " + decisionText);
return decisionText;
}
Here is the method that sets the private decisionText variable:
private void setDecisionText(String dec)
{
AdminData ad = adRep.findById(1L);
if(dec.equals("DEC_A"))
{
decisionText = ad.getTextA();
}
if(dec.equals("DEC_B"))
{
decisionText = ad.getTextB();
}
if(dec.equals("DEC_C"))
{
decisionText = ad.getTextC();
}
Logger.getGlobal().info(() -> "Dec Text Set: " + decisionText);
}
And the relevant portion of my controller class:
@RequestMapping(value="/", method=RequestMethod.POST)
public String loginSumbit(@Valid
@ModelAttribute("loginForm") ApplicantCredentialsData
applicantCredentialsData,
BindingResult br)
{
if(br.hasErrors())
{
return "loginForm";
}
else
{
ApplicantCredentialsData acd = acRep.findByLoginName(applicantCredentialsData.getLoginName());
if(acd == null)
{
Logger.getGlobal().info(() -> "Loginname not found");
return "loginForm";
}
String pw = applicantCredentialsData.getPassword().trim();
if(pw.equals(acd.getPassword().trim()))
{
Logger.getGlobal().info(() -> "Successfull Login");
Logger.getGlobal().info(() -> "Decision: " + acd.getDecision());
setDecisionText(acd.getDecision());
return "decisionPage";
}
else
{
Logger.getGlobal().info(() -> "Invalid password - Login failed");
return "loginForm";
}
}
}