velocity(覚書)
try { // Velocityの初期化 Velocity.init(getServlet().getServletContext().getRealPath( "velocity.properties")); // Velocityコンテキストに値を設定 VelocityContext context = new VelocityContext(); context.put("csvDataList", csvDataList); StringWriter sw = new StringWriter(); // テンプレートの作成 Template template = Velocity.getTemplate("sample.vm"); // テンプレートとマージ template.merge(context, sw); // マージしたデータはWriterオブジェクトであるswが持っているのでそれを文字列として出力 System.out.println(sw); request.setAttribute("outputData", sw.toString()); sw.flush(); // エラー処理 } catch (ResourceNotFoundException e) { e.printStackTrace(); throw e; // テンプレートが見つからないときの処理 } catch (ParseErrorException e) { // 構文にエラーがあるときの処理 e.printStackTrace(); throw e; } catch (MethodInvocationException e) { // テンプレートのどこかにエラーがあるときの処理 e.printStackTrace(); throw e; } catch (Exception e) { // その他のエラー時の処理 e.printStackTrace(); throw e; }
テンプレートファイル
<table border="1"> <tr> <td>No</td> <td>名前</td> <td>生まれた年</td> <td>死んだ年</td> </tr> #foreach($list in $csvDataList) <tr> <td>$list.line</td> <td>$list.name</td> <td>$list.birthYear</td> <td>$list.deathYear</td> </tr> #end </table>
vmファイルの場所を指定
file.resource.loader.path =