■velocity/velocity-toolsのDL
http://velocity.apache.org/download.cgi
■必要なjar
* commons-beanutils-1.7.0.jar
* commons-collections-3.2.1.jar
* commons-digester-1.8.jar
* commons-lang-2.4.jar
* commons-logging-1.1.jar
* velocity-1.7.jar
* velocity-tools-view-2.0.jar
■WEB-INF/velocity.propertiesを作成
velocity-1.7/src/java/org/apache/velocity/runtime/defaults/velocity.properties
をコピーして以下を変更。
* input.encoding=UTF-8
* output.encoding=UTF-8
* file.resource.loader.path = WEB-INF/vm/page
■web.xmlに以下を追記
notFoundFilter gaeVelocityTest.servlet.NotFoundFilter notFoundFilter *.vm REQUEST velocityServlet gaeVelocityTest.servlet.MyVelocityServlet velocityServlet /velocity velocity org.apache.velocity.tools.view.servlet.VelocityViewServlet org.apache.velocity.properties /WEB-INF/velocity.properties velocity *.vm
■appengine-web.xmlに以下を追記
true
■NotFoundFilter(vmファイルへの直アクセスを防ぐ)
package gaeVelocityTest.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class NotFoundFilter implements Filter { @Override public void destroy() { } @Override public void doFilter ( ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { ((HttpServletResponse)res).sendError(404, ((HttpServletRequest)req).getRequestURI()); } @Override public void init(FilterConfig conf) throws ServletException { } }
■MyVelocityServlet
package gaeVelocityTest.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.velocity.tools.view.VelocityViewServlet; public class MyVelocityServlet extends VelocityViewServlet { private static final long serialVersionUID = -2215018323215813266L; public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { try { req.setAttribute("val", "テスト"); req.getRequestDispatcher("/test.vm").forward(req, res); } catch (ServletException e) { e.printStackTrace(); } } }
■WEB-INF/vm/page/test.vm
--
$val
--
■確認
http://127.0.0.1:8888/velocity にアクセス。
デプロイして同様にアクセス。
■参考
GAE/JでVelocity
http://blog.suz-lab.com/2009/12/gaejvelocity.html
GAE/Javaでウェブアプリ開発: Velocityテンプレートエンジン(2)
http://blog.livedoor.jp/ykohat/archives/50788389.html
vmファイルへの直アクセスを防ぐ
http://d.hatena.ne.jp/paulownia/20090604/1244085243
No comments:
Post a Comment