简体   繁体   English

Java:使用2个或更多类进行Freemarker数据模型处理

[英]Java: Using 2 or more Classes for Freemarker Data Model processing

I've got a problem with understanding the Freemarker Data Model and how to work with it correctly. 我在理解Freemarker数据模型以及如何正确使用它方面遇到了问题。 I currently have 1 Core Data Class. 我目前有1个核心数据类。 It is stuffed as hell (a lot of variables with getters and setters) and I want to split it into different Classes. 它塞满了地狱(很多带有getter和setter的变量),我想将其拆分为不同的类。

But my Core Class is fueling a Freemarker processing... And I just don't get, how to get this working with several classes. 但是我的核心课程正在推动Freemarker的处理……而我不明白如何在多个课程中使用它。

Currently I use this: 目前我使用这个:

templateValues.process(config_generator.CDATA, stringWriter);

CDATA is my Core Data Class... I checked the Freemarker guide several times, but I just don't get it. CDATA是我的核心数据类...我检查了Freemarker指南几次,但我不明白。

If I would implement 2 Classes (CDATA1 & CDATA2 ie) how would I set up the processing? 如果我要实现2个类(即CDATA1和CDATA2),我将如何设置处理? And would this work with even more Classes as well? 这是否也可以用于更多类? (CDATA3 to X). (CDATA3至X)。

There's no such feature out of the box, but it's just the question of the TemplateHashModel implementation you pass in as the data model. 没有开箱即用的此类功能,但这只是您作为数据模型传递的TemplateHashModel实现的问题。 (The data model is always a TemplateHashModel , though if you pass in a POJO FreeMarker will adapt it automatically via the ObjectWrapper in the Configuration .) You could easily write a TemplateHashModel implementation that encloses a list of other TemplateHashModel -s, and whose get(String) returns the requested variable from the first enclosed TemplateHashModel whose get(String) returns non- null . (数据模型始终是TemplateHashModel ,尽管如果传入POJO FreeMarker则会通过ConfigurationObjectWrapper自动对其进行调整。)您可以轻松地编写一个TemplateHashModel实现,其中包含其他TemplateHashModel -s以及其get(String)从第一个包含的TemplateHashModel返回其请求的变量,该TemplateHashModelget(String)返回non- null To adapt your POJO ( CDATA1 etc.) to TemplateHashModel call cfg.getObjectWrapper().wrap(myObject) , where cfg is the freemarker.template.Configuration object. 要使POJO( CDATA1等)适应TemplateHashModel请调用cfg.getObjectWrapper().wrap(myObject) ,其中cfgfreemarker.template.Configuration对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM