所以我有以下用于FreeMarker的模板:
<?xml version="1.0" encoding="ISO-8859-1"?>
<master>
<header>
<version>${version}</version>
<created>${created}</created>
<creator>${creator}</creator>
<draw>${draw}</draw>
<game>${game}</game>
<type>${type}</type>
<sender>${sender}</sender>
<recipient>${recipient}</recipient>
<revision>${revision}</revision>
</header>
<data>
<stake>
${stake}
</stake>
<checksum_combifile>
${checksumFile}
</checksum_combifile>
</data>
</master>
以下是FreeMarker模板的POJO:
public class SscMessageVo {
/* Header */
private String version = "1.00";
private String created;
private String creator;
private String draw;
private String game;
private String type;
private String sender;
private String recipient;
private String revision;
/* Data */
private String stake;
private String checksumFile;
public SscMessageVo() {
super();
}
public SscMessageVo(final String version, final String created, final String creator, final String draw, final String game, final String type, final String sender, final String recipient,
final String revision, final String stake, final String checksumFile) {
super();
this.version = version;
this.created = created;
this.creator = creator;
this.draw = draw;
this.game = game;
this.type = type;
this.sender = sender;
this.recipient = recipient;
this.revision = revision;
this.stake = stake;
this.checksumFile = checksumFile;
}
public String getStake() {
return stake;
}
public void setStake(final String stake) {
this.stake = stake;
}
public String getChecksumFile() {
return checksumFile;
}
public void setChecksumFile(final String checksumFile) {
this.checksumFile = checksumFile;
}
public String getVersion() {
return version;
}
public void setVersion(final String version) {
this.version = version;
}
public String getCreated() {
return created;
}
public void setCreated(final String created) {
this.created = created;
}
public String getCreator() {
return creator;
}
public void setCreator(final String creator) {
this.creator = creator;
}
public String getDraw() {
return draw;
}
public void setDraw(final String draw) {
this.draw = draw;
}
public String getGame() {
return game;
}
public void setGame(final String game) {
this.game = game;
}
public String getType() {
return type;
}
public void setType(final String type) {
this.type = type;
}
public String getSender() {
return sender;
}
public void setSender(final String sender) {
this.sender = sender;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(final String recipient) {
this.recipient = recipient;
}
public String getRevision() {
return revision;
}
public void setRevision(final String revision) {
this.revision = revision;
}
}
一切顺利,但也很高兴,但现在我想做以下事情,避免重复,因为我有许多具有相同标头结构的模板,将属于标头的变量移动到名为 TemplateHeader的新类 strong>,像这样:
public class TemplateHeader {
/* Header */
private String version;
private String created;
private String creator;
private String draw;
private String game;
private String type;
private String sender;
private String recipient;
private String revision;
public TemplateHeader() {
super();
}
public TemplateHeader(String version, String created, String creator, String draw, String game, String type, String sender, String recipient, String revision) {
super();
this.version = version;
this.created = created;
this.creator = creator;
this.draw = draw;
this.game = game;
this.type = type;
this.sender = sender;
this.recipient = recipient;
this.revision = revision;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getDraw() {
return draw;
}
public void setDraw(String draw) {
this.draw = draw;
}
public String getGame() {
return game;
}
public void setGame(String game) {
this.game = game;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getRevision() {
return revision;
}
public void setRevision(String revision) {
this.revision = revision;
}
}
但是我不知道该怎么做,因为我正在从FreeTemplate中获取错误,并且在网络上找不到任何教程...。
答案 0 :(得分:1)
您可以使用继承(SscMessageVo
扩展了TemplateHeader
)。请注意,类和getter方法都必须是公共的。
如果要使用合成,如果Java API中的合成不透明(如myComposite.getHeader().getCreator()
),则比较麻烦。但是它在模板中可以是透明的。 更新:只需创建您的数据模型,例如
TemplateModelUtils.wrapAsHashUnion(config.getObjectWrapper(), myHeader, myData)
,其中myHeader
和myData
是您要“组成”的对象。
但是,TemplateModelUtils.wrapAsHashUnion
仅存在于2.3.29之后(距离2018年9月12日尚未发布)。因此,同时检查源代码(https://github.com/apache/freemarker/blob/2.3-gae/src/main/java/freemarker/template/utility/TemplateModelUtils.java)。您的可能会简单得多。您只需要类似var ow = cfg.getObjectWrapper(); TemplateHashModel dataModel = new HashUnionModel(ow.wrap(myHeader), ow.wrap(myData));
之类的东西,然后使用dataModel
作为您的数据模型。