0

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";
    }
}
}
Jim Archer
  • 1,337
  • 5
  • 30
  • 43
  • Please see http://stackoverflow.com/questions/8688135/modelattribute-annotation-when-to-use-it/26916920#26916920 and http://stackoverflow.com/questions/29039116/spring-mvc-validation-post-redirect-get-partial-updates-optimistic-concurren/29039117#29039117 . Looks like you're over-complicating things – Neil McGuigan Jul 17 '15 at 19:40
  • I'm sorry Neil, I don't see what you're pointing me at. I read the links but don't see how they answer my question. – Jim Archer Jul 17 '15 at 20:43
  • The first link explains when to use @ModelAttribute. The second provides an example for doing gets and posts. Perhaps you could explain (in words) what you're trying to do, and we can go from there – Neil McGuigan Jul 17 '15 at 20:57
  • Thanks Neil, well what I am trying to do seems straightforward. I have some text in a database I want to render on an html page. There is no form, its just text to display. The html page is a Thymeleaf template, and I used th:utext to make it render the html. I was just using a String object to hold the text. So, I add the String object to the model, and then go and fetch the text from the database, and assign it to the String. But the model does not update, so the text does not get rendered. More to come... – Jim Archer Jul 18 '15 at 00:40
  • I have worked around it by adding the String to the model after I populate with the text from the database, but it seems that I should be able to update the data in the model. – Jim Archer Jul 18 '15 at 00:40

0 Answers0