【问题标题】:How to translate a DTO in Struts 2如何在 Struts 2 中翻译 DTO
【发布时间】:2015-07-03 12:52:38
【问题描述】:

我们正在努力使带有一些脏代码的旧应用程序国际化。例如,我们有一个对象 DTO InstrumentDto:

private String label;
private Quotation quotation;
private ExprQuote quoteExp;

public String getTypeCouponCouru() {
    if (this.quoteExp.getCode().equals(Constants.INS_QUOTE_EXPR_PCT_NOMINAL_CPN_INCLUS)
     || this.quoteExp.getCode().equals(Constants.INS_QUOTE_EXPR_PCT_NOMINAL_INTERET)) {
        return "Coupon attaché";
    } else if(this.quoteExp.getCode().equals(Constants.INS_QUOTE_EXPR_PCT_NOMINAL_PIED_CPN)){
        return "Coupon détaché";
    } else {
        return "";
    }
}       

public String getFormattedLabel() {
    StringBuilder formattedLabel = new StringBuilder(this.label);

    Quotation quote = this.quotation;
    if (this.quotation != null) {
        formattedLabel.append(" ");
        formattedLabel.append(FormatUtil.formatDecimal(this.quotation.getCryQuote());

        if (this.quoteExp.getType().equals("PERCENT")) {
            formattedLabel.append(" %");
        } else {
            formattedLabel.append(" ");
            formattedLabel.append(this.quotation.getCurrency().getCode());
        }
        formattedLabel.append(" le ");
        formattedLabel.append(DateUtil.formatDate(this.quotation.getValoDate()));
    }
    return formattedLabel.toString();
}

然后,这些方法用于 JSP。例如getFormattedLabel(),我们有:

<s:select name = "orderUnitaryForm.fieldInstrument" 
            id = "fieldInstrument"
          list = "orderUnitaryForm.instrumentList" 
       listKey = "id" 
     listValue = "formattedLabel" />    

IMO,第一种方法在 DTO 上没有它的位置。我们期望视图管理要打印的标签。而在这个视图(JSP)中,翻译那些话是没有问题的。 另外,这个方法只用在 2 JSP 中。 “重复”条件测试不是问题。

但是getFormattedLabel()比较难:这种方法在很多JSP中都用到了,格式化标签的构建比较“复杂”。而且在 DTO 中不可能有 i18n 服务。

那该怎么做呢?

【问题讨论】:

  • 我觉得问题比仅仅使用 textgetText() 更复杂(或者我错过了什么?)
  • 不,这根本不是问题,S2 i18n 多年来一直是该框架的关键特性。但是使用它需要一些技巧。

标签: java jsp struts2 internationalization dto


【解决方案1】:

getFormattedLabel() 中的代码似乎是业务逻辑。

DTO 是一个简单的对象,没有任何复杂的测试/行为(请参阅wiki definition)。

IMO,您应该将这段代码移动到您的 Action 并像这样拆分您的 *.properties 文件:

您的 *.properties:

message1= {0} % le {1}
message2= {0} {1} le {2}

你的行动:

public MyAction extends ActionSupport { 
    public String execute(){
        //useful code here
        InstrumentDto dto = new InstrumentDto();
        StringBuilder formattedLabel = new StringBuilder(label);

        if (this.quotation != null) {
            String cryQuote = FormatUtil.formatDecimal(this.quotation.getCryQuote());
            String date = DateUtil.formatDate(this.quotation.getValoDate());

            if (this.quoteExp.getType().equals("PERCENT")) {
                formattedLabel.append(getText("message1", new String[] { cryQuote, date }));
            } else {
                String cryCode = this.quotation.getCurrency().getCode();
                formattedLabel.append(getText("message2", new String[] { cryQuote, cryCode, date }));
            }   
        }
        dto.setFormattedLabel(formattedLabel);
    }
}

希望这会有所帮助;)

【讨论】:

  • 感谢您的解决方案。是的,它完成了这项工作。但这是最好的解决方案吗?如果我必须在服务中将这个逻辑相互化(不扩展 ActionSupport,因此无法访问 getText()),我会被阻止。也许服务中的方法可以返回“message1”或“message2”,这是用 .properties 翻译它的操作:但是如何注入参数?我已经想到 taglib 来解决这个问题,但有可能吗?
猜你喜欢
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多