简体   繁体   中英

Use FreeMarker Template in Spring Boot with nested XML template

So i have the below template for 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>

And the below POJO for FreeMarker Template :

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

}

All good all sweet and happy but now i want to do the below thing do avoid duplicate because i have many templates with the same header structure , move the variables that belong to header to a new class named TemplateHeader , like this :

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

}

But i have no clue how to do that i am getting errors from FreeTemplate and can't find any tutorial on the web ....

You can use inheritance ( SscMessageVo extends TemplateHeader ). Note that both classes and the getter methods has to be public.

If you want to use composition, it's trickier if that's not transparent in the Java API (as in myComposite.getHeader().getCreator() ). But it can be transparent in the template. Updated: Simply create your data-model like TemplateModelUtils.wrapAsHashUnion(config.getObjectWrapper(), myHeader, myData) , where myHeader and myData are the object you want to "compose".

But, TemplateModelUtils.wrapAsHashUnion only exists since 2.3.29 (far from being released as of 2018-09-12). So meanwhile, check the source code ( https://github.com/apache/freemarker/blob/2.3-gae/src/main/java/freemarker/template/utility/TemplateModelUtils.java ). Yours can be much much simpler though. You just need something like var ow = cfg.getObjectWrapper(); TemplateHashModel dataModel = new HashUnionModel(ow.wrap(myHeader), ow.wrap(myData)); var ow = cfg.getObjectWrapper(); TemplateHashModel dataModel = new HashUnionModel(ow.wrap(myHeader), ow.wrap(myData)); Then use dataModel as your data-model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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