【问题标题】:Velocity can't retreive field from pojoVelocity 无法从 pojo 中检索字段
【发布时间】:2019-07-31 13:54:14
【问题描述】:

我有速度模板,一些将模板和 POJO 合并为文本的方法。我签入调试器,所有数据都正确填充。我的问题是速度只能获得一个字段(queueName),但其余的不能。这是为什么呢?

模板:

<html>
    <body>
        <h3>Environment: ${environment}</h3>

        <div>
            #if ($monitoredQueues.size() > 0)
            <table>
                #foreach( $monitoredQueue in $monitoredQueues )
                #set( $queueName = ${monitoredQueue.QueueName})
                #set( $crMsgCount = ${monitoredQueue.CurrentMessagesCount})
                #set( $prMsgCount = ${monitoredQueue.PreviousMessagesCount})
                #set( $prCheckTime = ${monitoredQueue.PreviousCheckTime})
                #set( $prEmailTime = ${monitoredQueue.PreviousEmailSentTime})
                <tr>
                    <td>
                        <table>
                            <tr>
                                <td>
                                    <p>Queue name: ${queueName}</p>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <p>Current messages count: ${crMsgCount}</p>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <p>Previous messages count: $!{prMsgCount}</p>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <p>Previous check time: $!{prCheckTime}</p>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <p>Previous email sent time: $!{prEmailTime}</p>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                #end
            </table>
            #end
        </div>
    </body>
</html>

这是我的 POJO

@Data //lombok annotation generates getters, setters, hash and equals
@Builder //lombok annotation generates builder
@NoArgsConstructor
@AllArgsConstructor
public class MonitoredQueue {

    private Integer id;
    private String queueName;
    private Integer currentMessageCount;
    private Integer previousMessageCount;
    private LocalDateTime previousCheckDate;
    private LocalDateTime previousEmailSentDate;
    private String createdBy;
    private LocalDateTime creationDate;
    private String modifiedBy;
    private LocalDateTime modificationDate;
}

合并方法

public String mergeTemplateIntoEmailText(List<MonitoredQueue> monitoredQueues, String environment) {
        velocityEngine.init();
        Template mailTemplate = velocityEngine.getTemplate(EMAIL_TEMPLATE);
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put(ENVIRONMENT, environment);
        velocityContext.put(MONITORED_QUEUE, monitoredQueues);
        StringWriter writer = new StringWriter();
        mailTemplate.merge(velocityContext, writer);
        return writer.toString();
    }

结果如下:

Environment: test

Queue name: XXXX Current messages count: $crMsgCount Previous messages count: $prMsgCount Previous check time: $prCheckTime Previous email sent time: $prEmailTime

我通过更改模板解决了我的问题。速度以某种方式在 html 树的深层某处松散了上下文。不知道为什么。

【问题讨论】:

  • 您在下面的 cmets 中提到您找到了解决此问题的方法。对这种说法有任何见解吗?我被困了几个小时,对此我感到非常困惑。
  • 我想我已经停止使用 object 并且我已经开始使用 map 来代替所有值。但我记不太清了。我想帮助你,但是我们在项目后期替换了这个代码,现在真的很难找到它。也许速度不是您可以选择的最佳选择?
  • 我的问题是该课程不公开。 Velocity 看不到它,也没有抛出任何错误,而是忽略了使用该类键入的所有字段...
  • 哦...下次可以用源代码提问。社区会立即为您提供帮助。无论如何,很高兴听到您解决了您的问题。

标签: java velocity


【解决方案1】:

确保您的 POJO 类是 public。如果您的 POJO 不公开:

@Data
@AllArgsConstructor
class Foo {
    private String bar;
    private String baz;
}

然后:

Foo foo = new Foo();
foo.setBar("lorem");
foo.setBaz("ipsum");

Template template = VELOCITY_ENGINE.getTemplate("/templates/sample.vm", UTF_8.name());
VelocityContext context = new VelocityContext(ImmutableMap.<String, Object>of("foo", foo));
StringWriter writer = new StringWriter();
template.merge(context, writer);

不会将${foo.bar}${foo.baz} 的值分别替换为loremipsum

只要我将Foo 设为public 类,Velocity 就可以毫无问题地渲染其属性。

【讨论】:

    【解决方案2】:

    Velocity 无法读取私有字段。您需要添加公共 getter。

    【讨论】:

    • 我正在使用龙目岛。数据注释生成公共 getter。
    • 你在哪里提到你的问题?!我觉得不值得投反对票。
    • 如果你想谈论Java工具,你应该知道lombok。它在 pojo 类中提到。我可以撤消此投票,但您下次应该记住它。
    • 或者只是删除你的答案,因为它没有帮助
    • > "如果你想谈论 Java 工具,你应该知道 lombok。"你的意思是说任何人都应该知道Iombok吗?我不同意。它只是其他海洋中的一个工具。是什么让您认为使用的工具应该为所有人所熟知? > “pojo 课上提到过。”你必须告诉我在哪里。对某人无用的答案可能对其他人有用。很抱歉,这不是您的问题,但它很常见(人们试图直接访问没有 getter 的字段)。如果您不满意,请不要接受答案。
    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多