私は速度テンプレートを使用してhtmlファイルにデータを設定する必要があります。私はテーブルを動的に作成する必要がありますまた、私はいくつかの静的なデータを表示する必要があります。私は動的にテーブルにデータを設定することができますが、私は私のリストから静的なデータを取得することはできません。私の問題は、私は単一のリストからのみ動的と静的なデータの両方を抽出する必要があるということです。私は速度テンプレートを使ってデータをhtmlファイルに設定する必要がありますか?
速度コード:
public static String prepareTemplate(HashMap<String, String> documentContentHashmap,String templateLocation) throws NamingException{
System.out.println("Document Contents :"+documentContentHashmap);
ArrayList documentList=new ArrayList();
documentList.add(documentContentHashmap);
System.out.println(documentList);
String templatePath=templateLocation.replaceAll("\\\\","/");
String[] splittedTemplatePath = templatePath.split("/");
String templateName=splittedTemplatePath[splittedTemplatePath.length-1];
VelocityEngine ve = new VelocityEngine();
Properties properties = new Properties();
properties.setProperty("resource.loader","file");
properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
properties.setProperty("file.resource.loader.path", templatePath.substring(0, templatePath.lastIndexOf("/"))+"/");
properties.setProperty("file.resource.loader.cache", "false");
properties.setProperty("file.resource.loader.modificationCheckInterval", "0");
ve.init(properties);
Template t = ve.getTemplate(templateName);
VelocityContext context = new VelocityContext();
//for (Entry<String, String> entry : documentContentHashmap.entrySet()) {
//context.put(entry.getKey(), entry.getValue());
//}
context.put("documentList", documentList);
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println("writer"+writer);
String documentBytes=generateDocument(writer);
if(documentBytes!=null){
return documentBytes;
}else{
return null;
}
}
HTMLファイル:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1"/>
<title>Payment Voucher</title>
</head>
<body>
<div id="container">
<div id="leftDiv">
<img alt="logo" src="$image_path"/>
</div>
<br/>
<div id="rightDiv" style="align:right;">
<label>Invoice Number :</label> <span>Here i have to access some value from the same documentList </span><br/>
<label>Invoice Date :</label> <span>Here i have to access some value from the same documentList </span>
</div>
<table>
#foreach($documentContent in $documentList)
<tr><td>$documentContent.invoice_no</td></tr>
#end
</table>
</div>
</body>
</html>