【问题标题】:Thymeleaf not recognized my field get methodThymeleaf 无法识别我的字段获取方法
【发布时间】:2018-06-15 10:02:50
【问题描述】:

我有一个百里香叶形式和弹簧靴后端。我有一个模型类,它定义了它的 getter 和 setter 有点不同的名称。因此,当我要采用该模型并将其字段作为表单输入字段时,thymeleaf 无法将它们识别为字段。

这是我的模态,

public class scDto {
    private Integer region;
    private boolean isAmt;

    public scDto() {
    }

    public Integer getRegion() {
        return this.region;
    }

    public void setRegion(Integer region) {
        this.region = region;
    }

    public boolean isAmt() {
        return this.isAmt;
    }

    public void setAmt(boolean amt) {
        this.isAmt = amt;
    }

这是我的表单输入字段,

 <input type="text" th:field="*{sc.isAmt}"/>

这是错误,

 Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (price:331)

表单在区域字段中运行良好。但它不适用于 Amt 字段。 如果我将 isAmt() get 方法更改为 getIsAmt(),也许我可以解决这个问题。但是我无法更改模态类的任何方法名称,因为该类已经编译并且我通过 jar 文件使用它。有没有办法解决这个问题。

【问题讨论】:

  • 我猜你可以尝试使用 {sc.amt} 来引用这个变量。有关 javabeans 表示法的更多信息,您可以在此处阅读:stackoverflow.com/a/17066599/7629196
  • @Rulsan K. 谢谢它对我有用。你为什么不把它作为一个答案。
  • 我刚刚做到了 =)

标签: java spring-boot thymeleaf


【解决方案1】:

(抄自问题下的cmets)

我猜你可以尝试使用 {sc.amt} 来引用这个变量。 有关 javabeans 表示法的更多信息,您可以在此处阅读:stackoverflow.com/a/17066599/7629196

【讨论】:

    【解决方案2】:

    看到你的 DTO 它只有 2 个字段

    public class scDto {
        private Integer region;
        private boolean isAmt;
    
        public boolean isAmt() {
            return this.isAmt;
        }
    
    ...
    
    }
    

    按照惯例

    对于这样的方法名称

    boolean isXyz()
    

    你会像xyz一样阅读它

    所以这一行

     <input type="text" th:field="*{sc.isAmt}"/>
    

    应该是

      <input type="text" th:field="*{sc.amt}"/>
    

    感谢 Ruslan K 在评论中提到这一点。 只是添加以增加清晰度。

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 2019-12-17
      • 2014-06-21
      • 1970-01-01
      • 2016-04-13
      • 2016-01-26
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多