You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Alfonso Perez R." <ap...@kynesoft.com> on 2004/02/16 15:00:31 UTC
JSP using Velocity
Hi all....
I'm new in Velocity.
I've a very extend set of JSP's actually, and I'm trying to migrate a
part of them to use Velocity.
What I'd like to do is to rebuild those JSP by replacing all HTML by the
use of one or more templates.
I build this example:
---------------------------------------
<%@ page import="java.io.StringWriter, java.io.PrintWriter,
org.apache.velocity.VelocityContext,
org.apache.velocity.Template,
org.apache.velocity.app.Velocity" %><%
try {
Velocity.init("alf.properties");
out.println(Velocity.getProperty(Velocity.RESOURCE_LOADER));
} catch(Exception e){
out.println("In INIT...<br>");
e.printStackTrace();
}
VelocityContext context = new VelocityContext();
context.put("name", new String("Velocity"));
Template template = null;
StringWriter sw = new StringWriter();
try {
template = Velocity.getTemplate("mytemplate.vm");
template.merge(context, sw);
} catch(Exception e){
out.println("In getTemplate...<br><pre>");
StringWriter swx = new StringWriter();
e.printStackTrace(new PrintWriter(swx));
out.println(swx.toString()+"\n</pre>");
}
out.println(sw.toString());
%>
---------------------------------------
Using the following alf.properties
---------------------------------------
resource.loader = alfonso
alfonso.resource.loader.class =
org.apache.velocity.runtime.resource.loader.FileResourceLoader
alfonso.resource.loader.path = /home/webroot/desarrolloAlf
alfonso.resource.loader.cache = true
alfonso.resource.loader.modificationCheckInterval = 2
---------------------------------------
The response I get is
---------------------------------------
[file] En getTemplate...
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'mytemplate.vm'
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:501)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:384)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:814)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:796)
at org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.java:337)
at org.apache.velocity.app.Velocity.getTemplate(Velocity.java:512)
at org.apache.jsp.test_jsp._jspService(test_jsp.java:60)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2416)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:263)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:360)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:604)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:562)
at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:679)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
at java.lang.Thread.run(Thread.java:534)
---------------------------------------
The question is Where to put the properties file.
I know this is not the way you recommend to use Velocity with Tomcat,
but I also want to know if this is possible and which are disadvantage
to use it that way. I don't like to use servlets (now) because to deploy
any servlet I need to do an especial configuration in the tomcat.
Thanks a lot.
alfonso
Re: JSP using Velocity
Posted by Nathan Bubna <na...@esha.com>.
Alfonso Perez R said:
> I'm new in Velocity.
> I've a very extend set of JSP's actually, and I'm trying to migrate a
> part of them to use Velocity.
>
> What I'd like to do is to rebuild those JSP by replacing all HTML by the
> use of one or more templates.
why not just use the Veltag?
http://jakarta.apache.org/velocity/veltag.html
> I build this example:
> ---------------------------------------
> <%@ page import="java.io.StringWriter, java.io.PrintWriter,
> org.apache.velocity.VelocityContext,
> org.apache.velocity.Template,
> org.apache.velocity.app.Velocity" %><%
>
> try {
> Velocity.init("alf.properties");
> out.println(Velocity.getProperty(Velocity.RESOURCE_LOADER));
> } catch(Exception e){
> out.println("In INIT...<br>");
> e.printStackTrace();
> }
> VelocityContext context = new VelocityContext();
> context.put("name", new String("Velocity"));
> Template template = null;
> StringWriter sw = new StringWriter();
> try {
> template = Velocity.getTemplate("mytemplate.vm");
> template.merge(context, sw);
> } catch(Exception e){
> out.println("In getTemplate...<br><pre>");
> StringWriter swx = new StringWriter();
> e.printStackTrace(new PrintWriter(swx));
> out.println(swx.toString()+"\n</pre>");
> }
> out.println(sw.toString());
>
> %>
> ---------------------------------------
>
> Using the following alf.properties
>
> ---------------------------------------
> resource.loader = alfonso
> alfonso.resource.loader.class =
> org.apache.velocity.runtime.resource.loader.FileResourceLoader
> alfonso.resource.loader.path = /home/webroot/desarrolloAlf
> alfonso.resource.loader.cache = true
> alfonso.resource.loader.modificationCheckInterval = 2
> ---------------------------------------
>
> The response I get is
>
> ---------------------------------------
>
> [file] En getTemplate...
>
> org.apache.velocity.exception.ResourceNotFoundException: Unable to find
resource 'mytemplate.vm'
> at
org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(Resource
ManagerImpl.java:501)
> at
org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceM
anagerImpl.java:384)
> at
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:8
14)
> at
org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:7
96)
> at
org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.java
:337)
> at org.apache.velocity.app.Velocity.getTemplate(Velocity.java:512)
> at org.apache.jsp.test_jsp._jspService(test_jsp.java:60)
> at
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>
> ---------------------------------------
>
> The question is Where to put the properties file.
>
> I know this is not the way you recommend to use Velocity with Tomcat,
not even close.
> but I also want to know if this is possible and which are disadvantage
> to use it that way.
it's ugly, complicated, and nobody else is doing it, so not many people will
be able to help you.
> I don't like to use servlets (now) because to deploy
> any servlet I need to do an especial configuration in the tomcat.
IMHO, writing up a simple little web.xml (or copying one from a VelocityTools
example app) is a lot easier, simpler, and cleaner than what you're trying to
do above.
Nathan Bubna
nathan@esha.com
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org