Java 速度可以';从pojo检索字段

Java 速度可以';从pojo检索字段,java,velocity,Java,Velocity,我有velocity模板,一些将模板和POJO合并到文本中的方法。我签入了调试器,所有数据都被正确地填充了。我的问题是velocity只能获得一个字段(queueName),但其余字段不能。为什么呢 模板: <html> <body> <h3>Environment: ${environment}</h3> <div> #if ($monitoredQueues.size

我有velocity模板,一些将模板和POJO合并到文本中的方法。我签入了调试器,所有数据都被正确地填充了。我的问题是velocity只能获得一个字段(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>
合并方法

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();
    }

我通过更改模板解决了我的问题。Velocity在html树深层的某个地方松散了上下文。不知道为什么

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

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

@数据
@AllArgsConstructor
福班{
私人弦杆;
私有字符串baz;
}
然后:

Foo-Foo=new-Foo();
foo.setBar(“lorem”);
foo.setBaz(“ipsum”);
Template Template=VELOCITY_ENGINE.getTemplate(“/templates/sample.vm”,UTF_8.name());
VelocityContext上下文=新的VelocityContext(ImmutableMap.of(“foo”,foo));
StringWriter编写器=新的StringWriter();
合并(上下文、编写器);
不会分别用
lorem
ipsum
替换
${foo.bar}
${foo.baz}
的值


只要我创建了
Foo
a
public
类,Velocity就可以毫无问题地呈现其属性。

Im使用lombok。数据注释生成公共获取者。你在问题中在哪里提到的?!我觉得不值得投反对票。如果你想谈论Java工具,你应该了解lombok。它在pojo课程中提到。我可以撤销这次投票,但你下次应该记住。或者删除你的答案,因为它没有帮助>“如果你想谈论Java工具,你应该知道lombok。”你的意思是说任何谈论Java工具的人都应该知道Imbok吗?我不同意。它只是众多工具中的一个工具。是什么让你认为你使用的工具应该为每个人所知?>“pojo课上提到过,”你必须告诉我在哪里。对某人不有用的答案可能对其他人有用。很抱歉,这不是您的问题,但这是一个常见的问题(人们试图直接访问字段而不使用getter)。如果你不满意,就不要接受答案。你在下面的评论中提到你找到了这个问题的解决方案。对这一说法有什么见解吗?我在这个问题上被困了好几个小时,对此我感到非常困惑。我想我已经停止使用object,而是开始使用带有所有值的map。但我记不太清楚了。我很想帮助你们,但我们已经在项目中替换了这段代码,现在很难找到它。也许velocity不是你能选择的最佳选项?我的问题是该课程不是公开的。Velocity看不到它,也没有抛出任何错误,它只是忽略了该类键入的所有字段…哦。。。下次你可以问一个关于源代码的问题。社区会立即帮助你。不管怎样,很高兴听到你已经解决了你的问题。
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
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);