設定手順
[1] 以下のダウンロード先から以下を落としてきて、解凍する * 「velocity-tools-X.X.zip」(今回「velocity-tools-2.0.zip」)http://velocity.apache.org/download.cgi
[2] ダウンロードしたファイルのLibのJarを、Eclipseの対象プロジェクトに追加する * 「velocity-X.X.X.jar」 * 「velocity-tools-X.X.jar」 * 「commons-digester-X.X.jar」(今回「commons-digester-1.8.jar」) など [3] web.xml に以下の記述を追記する(以下のサンプルを参照のこと。) ~~~~~~ <servlet> <servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class> <init-param> <param-name>org.apache.velocity.toolbox</param-name> <param-value>/WEB-INF/velocity-toolbox.xml</param-value> </init-param> <init-param> <param-name>org.apache.velocity.properties</param-name> <param-value>/WEB-INF/velocity.properties</param-value> </init-param> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> <url-pattern>*.vm</url-pattern> </servlet-mapping> ~~~~~~ [4] WEB-INF配下にファイル「velocity.properties」「velocity-toolbox.xml」を作成し、内容は、以下の様にする ■velocity.properties ~~~~~~ resource.loader = webapp webapp.resource.loader.class = org.apache.velocity.tools.view.servlet.WebappLoader webapp.resource.loader.path=/WEB-INF/templates webapp.resource.loader.cache = true runtime.log=/WEB-INF/velocity.log input.encoding=UTF-8 output.encoding=UTF-8 default.contentType=text/html; charset=UTF-8 ~~~~~~ ■velocity-toolbox.xml ~~~~~~ <?xml version="1.0" encoding="UTF-8" ?> <toolbox> </toolbox> ~~~~~~
ファイル構成
SampleService(Project) +- lib | + velocity-1.6.2.jar | + velocity-tools-2.0.jar | + commons-digester-1.8.jar | + ・・・略・・・ +- src | + com.sample.servlets.SampleVelocityServlet.java | + com.sample.servlets.SampleMessageBean.java +- WebContent + WEB-INF + velocity-toolbox.xml + velocity.properties + templates + SampleTemplete.vm
SampleVelocityServlet.java
package com.sample.servlets; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.velocity.Template; import org.apache.velocity.context.Context; import org.apache.velocity.tools.view.VelocityViewServlet; /** * Servlet implementation class SampleApacheServlet */ public class SampleVelocityServlet extends VelocityViewServlet { private static final long serialVersionUID = 1L; public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) { SampleMessageBean MessageBean = new SampleMessageBean("Mike", "2015-01-08", "111-222", "Java Program Book", 1000); context.put("msgBean", MessageBean); Template template = null; try { template = getTemplate("SampleTemplete.vm"); } catch (Exception e) { System.out.println("Error " + e); } return template; } }
SampleMessageBean.java
package com.sample.servlets; public class SampleMessageBean { private String userName; private String date; private String productId; private String productName; private int price; public SampleMessageBean( String userName, String date, String productId, String productName, int price) { this.userName = userName; this.date = date; this.productId = productId; this.productName = productName; this.price = price; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
SampleTemplete.vm
* テンプレートファイル<html> <body> <h1>$msgBean.userName様の購入履歴</h1> <table border="3"> <caption>日付:$msgBean.date</caption> <tr> <th>商品ID</th> <th>商品名</th> <th>値段</th> </tr> <tr> <td>$msgBean.productId</td> <td>$msgBean.productName</td> <td>$msgBean.price</td> </tr> </table> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SampleService</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>SampleVelocityServlet</servlet-name> <servlet-class>com.sample.servlets.SampleVelocityServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SampleVelocityServlet</servlet-name> <url-pattern>/SampleVelocityServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class> <init-param> <param-name>org.apache.velocity.toolbox</param-name> <param-value>/WEB-INF/velocity-toolbox.xml</param-value> </init-param> <init-param> <param-name>org.apache.velocity.properties</param-name> <param-value>/WEB-INF/velocity.properties</param-value> </init-param> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> <url-pattern>*.vm</url-pattern> </servlet-mapping> </web-app>
参考文献
http://www.jajakarta.org/velocity/velocity-1.2/docs-ja/developer-guide.htmlhttp://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/view/
http://pgadekar.blogspot.jp/2011/03/using-apache-velocity-for-mvc-sample.html
* 以下は後で役立ちそうなサイト