Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I call getters from model passed to Thymeleaf like parameter?

I add object to ModelAndView

ModelAndView model = new ModelAndView("index");
User currentUser = getUser();
model.addObject("currentUser", currentUser);

User model:

public class User {
    private String msisdn;
    private double balance;
    private double trafficResidue;
    private Map<String, String> variables;

    public String getMsisdn() {
        return msisdn;
    }

    public void setMsisdn(String msisdn) {
        this.msisdn = msisdn;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getTrafficResidue() {
        return trafficResidue;
    }

    public void setTrafficResidue(double trafficResidue) {
        this.trafficResidue = trafficResidue;
    }

    public Map<String, String> getVariables() {
        return variables;
    }

    public void setVariables(Map<String, String> variables) {
        this.variables = variables;
    }
}

And I need call getters in Thymeleaf

I tried

<label th:text="${currentUser.getMsisdn()}"/>

But it does not work. How can I call getters from model passed to Thymeleaf like parameter?

like image 711
user5620472 Avatar asked Oct 05 '16 04:10

user5620472


People also ask

How do you pass parameters in Thymeleaf?

Request parameters can be easily accessed in Thymeleaf views. Request parameters are passed from the client to server like: https://example.com/query?q=Thymeleaf+Is+Great! In the above example if parameter q is not present, empty string will be displayed in the above paragraph otherwise the value of q will be shown.

How do you use the model attribute in Thymeleaf?

Model attributes are used inside controller classes that prepare the data for rendering inside a view. One way we can add attributes to our model is to require an instance of Model as a parameter in a controller method. Spring will then inject an instance of Model for us when /email/modelattributes is requested.

What is #{} in Thymeleaf?

#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources.

How do you use setter and name getter methods?

Getter returns the value (accessors), it returns the value of data type int, String, double, float, etc. For the program's convenience, getter starts with the word “get” followed by the variable name. While Setter sets or updates the value (mutators). It sets the value for any variable used in a class's programs.


1 Answers

If you have a standard getter method (in a format which Thymeleaf anticipates) then you can just mention objectName.fieldName instead of objectName.getFieldName(), though both will work. If your getter method has some non-standard name, then objectName.fieldName will not work, you have to use objectName.yourweirdGetterMethodName().

In your case, for the field msisdn, you have a standard getter method getMsisdn(). So, both <label th:text="${currentUser.msisdn}"/> and <label th:text="${currentUser.getMsisdn()}"/> should work just fine for you.

Again, <label th:text="${currentUser.msisdn}"/> will work just fine, you don't have to explicitly mention the getter method (since its a standard getter method).

Unfortunately, both this options doesn't work for you. So that essentially means, problem is somewhere else. I doubt the objects that you added ever made to the view. If you can post the controller code, I may be able to help you out.

like image 188
so-random-dude Avatar answered Sep 27 '22 21:09

so-random-dude