簡體   English   中英

ActiveWeb:在模板中調用時,模型設置器/獲取器不起作用

[英]ActiveWeb: model setter/getter does not work when calling in the template

使用舊版數據庫時,有時無法使用belongs_to注釋正確設置關系。 在這種情況下,我嘗試使用以下訪問器方法定義指向另一個Model類的屬性:

@Table("INTERVENTION")
@IdName("ITV_ID")
public class Intervention extends Model {
  private InterventionModel interventionModel;

    public InterventionModel getInterventionModel() {
        return interventionModel;
    }

    public void setInterventionModel(InterventionModel interventionModel) {
        this.interventionModel = interventionModel;
    }
}

我在服務類中加載並設置InterventionModel時沒有出現如下問題(存在intervention實例):

private void loadInterventionModel(final Intervention intervention) {
        final InterventionModel model = InterventionModel.findById(intervention.getLongId());
        intervention.setInterventionModel(model);
    }

問題是,當我嘗試評估FreeMarker模板中的InterventionModel屬性時,它不起作用:

"item_code:": ${intervention.intervention_model.imo_internal_code}

這是刷新的錯誤:

FreeMarker template error:
An error has occurred when reading existing sub-variable "intervention_model"; see cause exception! The type of the containing value was: extended_hash+string (app.models.Intervention wrapped into f.e.b.StringModel)

Caused by: java.lang.IllegalArgumentException: Attribute: 'intervention_model' is not defined in model: 'class app.models.Intervention.

我在這里缺少什么,為什么它不能按預期工作? 通常,如果我在模型中聲明一個帶有其訪問器(getter和setter)的屬性,則可以在模板中通過以下方式訪問它:

mymodel.my_attribute

我想,我找到了原因。 似乎在類模型中聲明實例變量且此變量不屬於模型可用屬性(表列)時,應使用snake_case而不是camelCase編寫,即不要聲明:

public class Intervention extends Model {
...
  private InterventionModel interventionModel;

  public InterventionModel getInterventionModel() {
        return interventionModel;
    }

    public void setInterventionModel(InterventionModel interventionModel) {
        this.interventionModel = interventionModel;
    }

}

使用snake_case版本:

private InterventionModel intervention_model;

public InterventionModel getIntervention_model() {
    return intervention_model;
}

public void setIntervention_model(InterventionModel intervention_model) {
    this.intervention_model = intervention_model;
}

這樣,您將能夠在模板中作為模型中可用的其他變量來訪問它:

"label": "${intervention.intervention_model.imo_product_label}"

一開始,我認為轉換是自動完成的。 您可以保留camlCase版本,在這種情況下,您也應該在模板中使用它:

"label": "${intervention.interventionModel.imo_product_label}"

希望這可以幫助。

如果您的InterventionModel類指向舊表,則可以執行以下操作:

@Table("INTERVENTION")
@IdName("ITV_ID")
public class Intervention extends Model {
  private InterventionModel interventionModel;

    public InterventionModel getInterventionModel() {
        return interventionModel;
    }

    public void setInterventionModel(InterventionModel interventionModel) {
        this.interventionModel = interventionModel;
    }

    Object getImoProductLabel(){
        return interventionModel.imo_product_label();
    }
}

然后在模板中:

"label": "${intervention.imoProductLabel}"

這樣,您不僅可以為模板構建方法,還可以為代碼中的其他位置構建方法,並提取掉舊代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM