这是一个复杂的xml模板的一部分,我在这里将其放在这个问题中:
<?xml version="1.0" encoding="UTF-8"?>
<Document>
<CstmrDrctDbtInitn>
<GrpHdr>
<MsgId>${MsgId}</MsgId>
<CreDtTm>${CreDtTm}</CreDtTm>
<NbOfTxs>${NbOfTxs}</NbOfTxs>
<a> ${val1}
<b>
${val2}
</b>
</a>
<CtrlSum>${CtrlSum}</CtrlSum>
</GrpHdr>
<PmtInf>
<PmtInfId>${PmtInfId}</PmtInfId>
<PmtMtd>${PmtMtd}</PmtMtd>
</PmtInf>
<#list persons as person>
</#list>
</CstmrDrctDbtInitn>
</Document>
上个月我一直在使用FreeMarker,直到现在xml模型都很简单
在网络上搜索如何使用此模板,是否应该创建匹配的Java类(其中有100个?)...我应该使用Map吗?就像显示的here一样。
我不知道该怎么做...如何在此模板上应用FreeMarker?
答案 0 :(得分:0)
因此,这是将来任何用户的解决方案。首先,我创建了3个模型,如下所示:
Map<String, Object> data = new HashMap<>();
//================= Example Creating HeaderVo //=================
HeaderVo header = new HeaderVo("la","la",5,5,"la","la");
data.put("header", header);
//================= Example Creating MiddleVo //=================
MiddleVo middleVo = new MiddleVo("la","la",5,"la","la","la","la");
data.put("middle", middleVo);
//================= Example Creating internal items =================
InternalVo v1 = new InternalVo(5, 5, 5, "s", true, "zz", "zzzz", "ll","EUR");
InternalVo v2 = new InternalVo(5, 5, 5, "s", true, "zz", "zzzz", "ll","DOLLARS");
InternalVo v3 = new InternalVo(5, 5, 5, "s", true, "zz", "zzzz", "ll","LAT");
//List parsing
List<InternalVo> internalVos = new ArrayList<>();
internalVos.add(v1);
internalVos.add(v2);
internalVos.add(v3);
data.put("vos", internalVos);
//================= //================= //================= //=================
final String message = this.templateManager.composeStringFromTemplate(data, "bankfile.ftl");
所以下面的bankfile.ftl
代表了复杂的xml:
```
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02">
<CstmrDrctDbtInitn>
<GrpHdr>
<MsgId>${header.msgId}</MsgId>
<CreDtTm>${header.creDtTm}</CreDtTm>
<NbOfTxs>${header.nbOfTxs}</NbOfTxs>
<CtrlSum>${header.ctrlSum}</CtrlSum>
<InitgPty>
<Nm>${header.nm}</Nm>
<Id>
<OrgId>
<Othr>
<Id>${header.id}</Id>
</Othr>
</OrgId>
</Id>
</InitgPty>
</GrpHdr>
<PmtInf>
<PmtTpInf>
<SvcLvl>
<Cd>${middle.svcLvlCD}</Cd>
</SvcLvl>
<LclInstrm>
<Cd>${middle.lclInstrmCD}</Cd>
</LclInstrm>
<SeqTp>${middle.seqTp}</SeqTp>
</PmtTpInf>
<ReqdColltnDt>${middle.reqdColltnDt}</ReqdColltnDt>
<Cdtr>
<Nm>${middle.nm}</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>${middle.iBAN}</IBAN>
</Id>
</CdtrAcct>
<CdtrAgt>
<FinInstnId>
<BIC>${middle.bIC}</BIC>
</FinInstnId>
</CdtrAgt>
<#list vos as vo>
<DrctDbtTxInf> <PmtId> <EndToEndId>${vo.endToEndId}</EndToEndId> </PmtId> <InstdAmt Ccy="${vo.ccy}">${vo.instdAmt}</InstdAmt> <DrctDbtTx> <MndtRltdInf> <MndtId>${vo.mndtId}</MndtId> <DtOfSgntr>${vo.dtOfSgntr}</DtOfSgntr> <AmdmntInd>${vo.amdmntInd?c}</AmdmntInd> </MndtRltdInf> </DrctDbtTx> <DbtrAgt> <FinInstnId> <BIC>${vo.bIC}</BIC> </FinInstnId> </DbtrAgt> <Dbtr> <Nm>${vo.nm}</Nm> </Dbtr> <DbtrAcct> <Id> <IBAN>${vo.iBAN}</IBAN> </Id> </DbtrAcct> </DrctDbtTxInf>
</#list>
</PmtInf>
</CstmrDrctDbtInitn>
</Document>
```
最后是用于FTL的3种模型(抱歉,它们太大了,我只是想知道它有多复杂:)):
HeaderVo
public class HeaderVo {
private String msgId;
private String creDtTm;
private int nbOfTxs;
private int ctrlSum;
private String nm;
private String id;
public HeaderVo() {
super();
}
public HeaderVo(String msgId, String creDtTm, int nbOfTxs, int ctrlSum, String nm, String id) {
super();
this.msgId = msgId;
this.creDtTm = creDtTm;
this.nbOfTxs = nbOfTxs;
this.ctrlSum = ctrlSum;
this.nm = nm;
this.id = id;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getCreDtTm() {
return creDtTm;
}
public void setCreDtTm(String creDtTm) {
this.creDtTm = creDtTm;
}
public int getNbOfTxs() {
return nbOfTxs;
}
public void setNbOfTxs(int nbOfTxs) {
this.nbOfTxs = nbOfTxs;
}
public int getCtrlSum() {
return ctrlSum;
}
public void setCtrlSum(int ctrlSum) {
this.ctrlSum = ctrlSum;
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
this.nm = nm;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
MiddleVo
public class MiddleVo {
private String svcLvlCD;
private String lclInstrmCD;
private int seqTp;
private String reqdColltnDt;
private String nm;
private String iBAN;
private String bIC;
public MiddleVo() {
super();
}
public MiddleVo(String svcLvlCD, String lclInstrmCD, int seqTp, String reqdColltnDt, String nm, String iBAN,
String bIC) {
super();
this.svcLvlCD = svcLvlCD;
this.lclInstrmCD = lclInstrmCD;
this.seqTp = seqTp;
this.reqdColltnDt = reqdColltnDt;
this.nm = nm;
this.iBAN = iBAN;
this.bIC = bIC;
}
public String getSvcLvlCD() {
return svcLvlCD;
}
public void setSvcLvlCD(String svcLvlCD) {
this.svcLvlCD = svcLvlCD;
}
public String getLclInstrmCD() {
return lclInstrmCD;
}
public void setLclInstrmCD(String lclInstrmCD) {
this.lclInstrmCD = lclInstrmCD;
}
public int getSeqTp() {
return seqTp;
}
public void setSeqTp(int seqTp) {
this.seqTp = seqTp;
}
public String getReqdColltnDt() {
return reqdColltnDt;
}
public void setReqdColltnDt(String reqdColltnDt) {
this.reqdColltnDt = reqdColltnDt;
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
this.nm = nm;
}
public String getiBAN() {
return iBAN;
}
public void setiBAN(String iBAN) {
this.iBAN = iBAN;
}
public String getbIC() {
return bIC;
}
public void setbIC(String bIC) {
this.bIC = bIC;
}
}
InternalVo
public class InternalVo {
private int endToEndId;
private int instdAmt;
private int mndtId;
private String dtOfSgntr;
private boolean amdmntInd;
private String bIC;
private String nm;
private String iBAN;
private String ccy;
public InternalVo() {
super();
}
public InternalVo(int endToEndId, int instdAmt, int mndtId, String dtOfSgntr, boolean amdmntInd, String bIC, String nm,
String iBAN,String ccy) {
super();
this.endToEndId = endToEndId;
this.instdAmt = instdAmt;
this.mndtId = mndtId;
this.dtOfSgntr = dtOfSgntr;
this.amdmntInd = amdmntInd;
this.bIC = bIC;
this.nm = nm;
this.iBAN = iBAN;
this.ccy = ccy;
}
public int getEndToEndId() {
return endToEndId;
}
public void setEndToEndId(int endToEndId) {
this.endToEndId = endToEndId;
}
public int getInstdAmt() {
return instdAmt;
}
public void setInstdAmt(int instdAmt) {
this.instdAmt = instdAmt;
}
public int getMndtId() {
return mndtId;
}
public void setMndtId(int mndtId) {
this.mndtId = mndtId;
}
public String getDtOfSgntr() {
return dtOfSgntr;
}
public void setDtOfSgntr(String dtOfSgntr) {
this.dtOfSgntr = dtOfSgntr;
}
public boolean isAmdmntInd() {
return amdmntInd;
}
public void setAmdmntInd(boolean amdmntInd) {
this.amdmntInd = amdmntInd;
}
public String getbIC() {
return bIC;
}
public void setbIC(String bIC) {
this.bIC = bIC;
}
public String getNm() {
return nm;
}
public void setNm(String nm) {
this.nm = nm;
}
public String getiBAN() {
return iBAN;
}
public void setiBAN(String iBAN) {
this.iBAN = iBAN;
}
public String getCcy() {
return ccy;
}
public void setCcy(String ccy) {
this.ccy = ccy;
}