
[英]How can I call a constructor with two parameters from thymeleaf textbox?
[英]How can I call getters from model passed to Thymeleaf like parameter?
我将对象添加到ModelAndView
ModelAndView model = new ModelAndView("index");
User currentUser = getUser();
model.addObject("currentUser", currentUser);
用户模型:
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;
}
}
我需要Thymeleaf中的呼叫获取器
我试过了
<label th:text="${currentUser.getMsisdn()}"/>
但这行不通。 如何从传递给Thymeleaf之类的参数的模型中调用吸气剂?
如果您有标准的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方法)。
不幸的是,这两个选项都不适合您。 所以从本质上讲,问题就在其他地方。 我怀疑您添加到视图中的对象。 如果您可以发布控制器代码,我也许可以为您提供帮助。
如果您只是使用getter作为值,则直接使用属性“ $ {currentUser.msisdn}”,或者如果您希望将一些逻辑添加到getter并使用它,则可以在这里参考: 如何从Thymeleaf调用对象的方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.