如何从传递给Thymeleaf之类的参数的模型中调用吸气剂?

use*_*472 5 java spring spring-mvc thymeleaf

我将对象添加到ModelAndView

ModelAndView model = new ModelAndView("index");
User currentUser = getUser();
model.addObject("currentUser", currentUser);
Run Code Online (Sandbox Code Playgroud)

用户模型:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要Thymeleaf中的呼叫获取器

我试过了

<label th:text="${currentUser.getMsisdn()}"/>
Run Code Online (Sandbox Code Playgroud)

但这行不通。如何从传递给Thymeleaf之类的参数的模型中调用吸气剂?

so-*_*ude 5

如果您有标准的getter方法(Thymeleaf期望采用的格式),则可以只提及objectName.fieldName而不是objectName.getFieldName(),尽管两者都可以。如果您的getter方法具有某些非标准名称,则objectName.fieldName将不起作用,您必须使用objectName.yourweirdGetterMethodName()。

对于您的情况,对于字段msisdn,您有一个标准的getter方法getMsisdn()。所以,无论是<label th:text="${currentUser.msisdn}"/><label th:text="${currentUser.getMsisdn()}"/>应该只是罚款你。

再说一次, <label th:text="${currentUser.msisdn}"/>就可以了,您不必明确提到getter方法(因为它是标准的getter方法)。

不幸的是,这两个选项都不适合您。所以从本质上讲,问题就在其他地方。我怀疑您添加到视图中的对象。如果您可以发布控制器代码,我也许可以为您提供帮助。