You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Steve Cohen <sc...@javactivity.org> on 2008/12/26 23:53:24 UTC

Which flavor of Velocity to use?

I want to try to port an existing application to Velocity.

This application is in some ways quite like a Web App, yet it is not 
one.  It's user-interface is delivered through the AOL Instant Messenger 
SDK.  In AIM terms, it is a "bot".  Users chat with it as they would 
with any other AIM buddy and it performs various tasks for them, during 
which it presents them with various bits of information.

AIM delivers instant messages to our bot, which is implemented using 
AIM's SDK.  An Instant Message contains of course the text of the IM, 
which our bot processes much like a command-line parser of a sort.  A 
properly entered command leads to some sort of action being performed 
which might be data retrieval or modification, or something else.  An 
improperly entered command leads to an "error page".  No matter what it 
is, the feedback to the user is an IM sent back to him via AIM.  Our 
bot's interface to AIM going in this direction is again the AIM SDK.  
This is analogous to a servlet response.

Additionally, although there are alternatives, AOL's preferred mechanism 
for receiving inputs from a "bot" is that it be a limited form of xhtml 
- that is, only a restricted subset of xhtml is allowed.  Things like 
<b>, <br/>, and <font> are allowed, <table> is not.  The AIM SDK 
provides a way of converting strings to this format, but it is not 
required to use this API.

Up until now, the relatively simple nature of this interface led me down 
the primrose path of simply taking strings as input, parsing them, 
running them through whatever process was required and spitting back out 
the required output.  I gave no thoughts to MVC.  Possible outputs were 
stored in a properties file, often with various replaceable parameters.  
This was easily handled in java, but it's become more cumbersome as the 
size of this properties file increased.  It got way more cumbersome when 
I started needing to format the text.  I developed a clever (too clever 
by half) mechanism for formatting this text using java, but I really 
need to get away from that.  Java is doing way too much.  I would rather 
now have a bunch of little "html pages" that have the formatting on 
them, with the content to be popped in via templating.

What I want is to have the various boilerplate outputs stored as 
velocity templates, in an intelligently organized directory tree 
structure, and use the velocity mechanism to merge in the live data.  
Fortunately, even though this application isn't a WebApp, I long ago 
made the decision to run it inside of Tomcat.  Although it isn't a Web 
application, there are a couple of minor interfaces that do access the 
application through servlets, hence Tomcat.  Thus the architecture is 
all already there to put all the templates under the Web Context root.

However, as I said, it ISN'T actually a web app.  It doesn't deliver 
content to users via the http request/response paradigm.  One idea for 
using velocity in this situation would be to make http://localhost:8080 
calls to the Velocity Servlet, get the merged output, read it into a 
StringWriter, and send that on to the user.  But that doesn't feel 
right.  It feels like needless overhead.

I would like to invoke Velocity like a non-webapp, through java, but 
read the templates from the Web Context as though it were a web app.  
Has anyone done anything like that, or otherwise have helpful advice for me?

Thanks.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Which flavor of Velocity to use?

Posted by Steve Cohen <sc...@javactivity.org>.
Actually, I didn't need a whitespace stripper at all.  I just needed to 
stop using </br> tags in my template and let AOL do its thing with the 
physical presence of a linefeed, which makes the whole thing more 
WYSIWIG anyway, which is the aim.
Nathan Bubna wrote:
> No whitespace stripper in Velocity.  Last i checked, JTidy was the
> best option.  I'm not aware of another Apache one at the moment.
>
> On Sun, Dec 28, 2008 at 7:46 PM, Steve Cohen <sc...@javactivity.org> wrote:
>   
>> Thanks.  I didn't understand that both properties were needed.   (And I now
>> understand what the configuration documentation was trying to tell me.)
>>
>> This is now working for me, except for one thing:
>>
>> The AOL converter seems to strip out all whitespace from the html it
>> generates.  Since I'm not using the AOL converter here, (the velocity
>> templates are serving the function of providing the html), whitespace is not
>> getting stripped and the output ends up looking not so hot.  The whole point
>> of doing this is to be able to code the templates more or less visually as
>> simple html pages.  I don't want unreadable templates.  So I need an html
>> whitespace stripper.  I suppose I could use JTidy but I'd like to avoid
>> adding yet another jar.  Is there anything in Velocity itself (or perhaps in
>> the Apache toolset) that takes html and strips out the whitespace?
>>
>> Nathan Bubna wrote:
>>     
...

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: The loading of such a template cost 16000ms :(

Posted by Byron Foster <by...@base2.cc>.
There is something definitely wrong with your setup, getTemplate()  
should be much faster, even when the template hasn't been compiled  
yet.  I'm not sure what you mean by "This template is created  
automatically by another Velocity client class".

The problem you are experiencing is NOT due to the below velocity  
template, but is some issue with your implementation.

Without knowing what you are doing it is hard to judge, but I suggest  
you begin with the simplest possible code to load the below template,  
and then work backward to find out where the problem lies.

On Dec 29, 2008, at 2:11 , bluejoe wrote:

> Hi, all. My template file is shown below, getTemplate() cost more  
> than 16000ms to load it. I don't know what's wrong with it. This  
> template is created automatically by another Velocity client class  
> (I named it as TemplatePublisher), so it is too ugly and long. Who  
> can tell me how to optimize this template to make the loading time  
> shorter?
>
> thanks a lot!
>
> ============== my poor template ==============
>
> <!--created by VdbPublisher at 2008-12-27 00:14:31-->
> #if ($pageViewer.beans.size() == 0)
> <center>
>  <div style="width:100%" align="left"> <br>
>    <p><font color="#cccccc">no records...</font></p>
>  </div>
> </center>
> #else
> <table align="center" width="100%" border="1" cellpadding="6"  
> class="table1 dataset book">
>  <tr class="header">
>       <td align="center" fieldId="8a8080841c833108011c8331087c0002"  
> orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)"  
> style="cursor:hand" title="order by number" ><b><img src="/images/ 
> key.gif" align="absbottom"> number</b>#if  
> ('8a8080841c833108011c8331087c0002' == $jsoQuery.orderId)<img src="/ 
> images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</ 
> td>
>          <td align="center"  
> fieldId="8a8080841c833108011c8331087c0003" orderAsc="$ 
> {jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand"  
> title="order by title" ><b> title</b>#if  
> ('8a8080841c833108011c8331087c0003' == $jsoQuery.orderId)<img src="/ 
> images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</ 
> td>
>          <td align="center"  
> fieldId="1f62836e1c8dd5da011c8dd909b00001" orderAsc="$ 
> {jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand"  
> title="order by price" ><b> price</b>#if  
> ('1f62836e1c8dd5da011c8dd909b00001' == $jsoQuery.orderId)<img src="/ 
> images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</ 
> td>
>          <td align="center"  
> fieldId="1f62836e1c8dd5da011c8dd925d30003" orderAsc="$ 
> {jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand"  
> title="order by modification time" ><b> modification time</b>#if  
> ('1f62836e1c8dd5da011c8dd925d30003' == $jsoQuery.orderId)<img src="/ 
> images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</ 
> td>
>          <td align="center"  
> fieldId="1f62836e1c8de132011c8e09666d0001" orderAsc="$ 
> {jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand"  
> title="order by description" ><b> description</b>#if  
> ('1f62836e1c8de132011c8e09666d0001' == $jsoQuery.orderId)<img src="/ 
> images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</ 
> td>
>          <td align="center"  
> fieldId="1f62836e1c8de132011c8e0984550003" orderAsc="$ 
> {jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b>  
> images</b>#if ('1f62836e1c8de132011c8e0984550003' ==  
> $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif"  
> align="absbottom" width="13">#end</td>
>          <td align="center"  
> fieldId="1f62836e1c8de132011c8e09b4000005" orderAsc="$ 
> {jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand"  
> title="order by publish date" ><b> publish date</b>#if  
> ('1f62836e1c8de132011c8e09b4000005' == $jsoQuery.orderId)<img src="/ 
> images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</ 
> td>
>          <td align="center"  
> fieldId="ff8080811db4d587011db4e3fab5000a" orderAsc="$ 
> {jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand"  
> title="order by isNew" ><b> isNew</b>#if  
> ('ff8080811db4d587011db4e3fab5000a' == $jsoQuery.orderId)<img src="/ 
> images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</ 
> td>
>          <td align="center"  
> fieldId="ff8080811db4d587011db4e811f3000d" orderAsc="$ 
> {jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b>  
> vfield</b>#if ('ff8080811db4d587011db4e811f3000d' ==  
> $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif"  
> align="absbottom" width="13">#end</td>
>          <td align="center"  
> fieldId="ff8080811db4ec19011db4ec61c50001" orderAsc="$ 
> {jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b>  
> location</b>#if ('ff8080811db4ec19011db4ec61c50001' ==  
> $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif"  
> align="absbottom" width="13">#end</td>
>          <td align="center"  
> fieldId="ff8080811e5245cc011e524aaa1a0005" orderAsc="$ 
> {jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b>  
> authors</b>#if ('ff8080811e5245cc011e524aaa1a0005' ==  
> $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif"  
> align="absbottom" width="13">#end</td>
>        <td class="header"></td>
>  </tr>
> #foreach ($bean in $pageViewer.beans)
>  #set ($id = $bean.id.jdbcObject)
>  #set ($flag = $velocityCount%2)
>    <tr id="DATAGRID_ROW" class="bg${flag}" checked="false"  
> originalClass="bg$flag" rowID="${id}" onmouseout="hiliteRecord(this,  
> false)" style="cursor:hand" onclick="toggleRecord(this);"  
> title="click here to select this row">
>        <td>$bean.id.format('%d')</td>
>        <td>$bean.title.format('%s')
> </td>
>        <td>$bean.price.format('%.2f')</td>
>        <td>$bean.posttime.dateFormat('')</td>
>        <td>$bean.desc.format('%s')
> </td>
>        <td>#foreach ($file in $bean.photo.files)
> <a href="${file.filePath}">${file.title}[${file.fileSize}]</a><br>
> #end</td>
>        <td>$bean.publishdate.dateFormat('')</td>
>        <td>$bean.ispop.format('%s')
> </td>
>        <td>$bean.vfield.value</td>
>        <td>$bean.location.lon, $bean.location.lat</td>
>        <td><div id="13" class="DataTableView"></div>
> <script>
> var jsoQuery_13 = new SlaveQuery('cn.csdb.paperdb.author', '$ 
> {bean.id.jdbcObject}', 'cn.csdb.paperdb.book.authors');
> jsoQuery_13.pageSize = 20;
> var jsoDataTableView_13 = new  
> DataTableView('cn.csdb.paperdb.author', jsoQuery_13, $ 
> ('#13.DataTableView')[0]);
> jsoDataTableView_13.refresh();
> </script></td>
>        <td align="center" valign="top" class="bg2"><span id="TITLE_ 
> $velocityCount" style="display:none">$bean.title.format('%s')
> </span>
>  #if($!authenticator)
>  <a href=# onclick="updateRecord('8a8080841c833108011c8331087c000a',  
> '${id}');">update</a> <a href=#  
> onclick="deleteRecord('8a8080841c833108011c8331087c000a', '$ 
> {id}');">delete</a>
>  #end
>      </td>
>  </tr>
> #end
> </table>
>  <p> <input type="checkbox" onclick="checkRecords(this);">select all
>  &nbsp;
> #if($!authenticator)
> <button onclick='deleteRecords();'><img src='/images/submit_n.gif'  
> border=0 align=absbottom>delete all</button>
> #end
>  </p>
> #if ($pageViewer.pageCount > 1)
> <br>
> <center>
>    <div style="width:100%">
>      <div align="right">${pageViewer.footerBar1}</div>
>    </div>
>    <div style="width:100%">
>      <div align="right">${pageViewer.footerBar2}</div>
>    </div>
> </center>
> #end
> #end


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: The loading of such a template cost 16000ms :(

Posted by bluejoe <bl...@gmail.com>.
Thank you, It seems that there is something wrong in my eclipse. The simplest TestCase runs very very slowly in it. When I replace the eclipse with a new version, the cost turns to be 300ms and even more quickly than that, it's so nice, thank you!

----- Original Message ----- 
From: "Nathan Bubna" <nb...@gmail.com>
To: "Velocity Users List" <us...@velocity.apache.org>
Sent: Tuesday, December 30, 2008 1:33 AM
Subject: Re: The loading of such a template cost 16000ms :(


> 16 seconds?   I'm with Byron on this.  Something is wrong, and it's
> not with your template or Velocity's parsing.  This template is
> neither long enough nor complicated enough to get anywhere close to
> that time for parsing on any sort of modern system.  What system is
> this on?  Are you using a custom resource loader?  URLResourceLoader
> over a slow connection?  What profiler are you getting the 16000ms
> time from?  It would be best to know exactly where in your system this
> time is being spent.
> 
> On Mon, Dec 29, 2008 at 1:11 AM, bluejoe <bl...@gmail.com> wrote:
>> Hi, all. My template file is shown below, getTemplate() cost more than 16000ms to load it. I don't know what's wrong with it. This template is created automatically by another Velocity client class (I named it as TemplatePublisher), so it is too ugly and long. Who can tell me how to optimize this template to make the loading time shorter?
>>
>> thanks a lot!
>>
>> ============== my poor template ==============
>>
>> <!--created by VdbPublisher at 2008-12-27 00:14:31-->
>> #if ($pageViewer.beans.size() == 0)
>> <center>
>>  <div style="width:100%" align="left"> <br>
>>    <p><font color="#cccccc">no records...</font></p>
>>  </div>
>> </center>
>> #else
>> <table align="center" width="100%" border="1" cellpadding="6" class="table1 dataset book">
>>  <tr class="header">
>>       <td align="center" fieldId="8a8080841c833108011c8331087c0002" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by number" ><b><img src="/images/key.gif" align="absbottom"> number</b>#if ('8a8080841c833108011c8331087c0002' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="8a8080841c833108011c8331087c0003" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by title" ><b> title</b>#if ('8a8080841c833108011c8331087c0003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="1f62836e1c8dd5da011c8dd909b00001" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by price" ><b> price</b>#if ('1f62836e1c8dd5da011c8dd909b00001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="1f62836e1c8dd5da011c8dd925d30003" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by modification time" ><b> modification time</b>#if ('1f62836e1c8dd5da011c8dd925d30003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="1f62836e1c8de132011c8e09666d0001" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by description" ><b> description</b>#if ('1f62836e1c8de132011c8e09666d0001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="1f62836e1c8de132011c8e0984550003" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> images</b>#if ('1f62836e1c8de132011c8e0984550003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="1f62836e1c8de132011c8e09b4000005" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by publish date" ><b> publish date</b>#if ('1f62836e1c8de132011c8e09b4000005' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="ff8080811db4d587011db4e3fab5000a" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by isNew" ><b> isNew</b>#if ('ff8080811db4d587011db4e3fab5000a' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="ff8080811db4d587011db4e811f3000d" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> vfield</b>#if ('ff8080811db4d587011db4e811f3000d' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="ff8080811db4ec19011db4ec61c50001" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> location</b>#if ('ff8080811db4ec19011db4ec61c50001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>          <td align="center" fieldId="ff8080811e5245cc011e524aaa1a0005" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> authors</b>#if ('ff8080811e5245cc011e524aaa1a0005' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>>        <td class="header"></td>
>>  </tr>
>>  #foreach ($bean in $pageViewer.beans)
>>  #set ($id = $bean.id.jdbcObject)
>>  #set ($flag = $velocityCount%2)
>>    <tr id="DATAGRID_ROW" class="bg${flag}" checked="false" originalClass="bg$flag" rowID="${id}" onmouseout="hiliteRecord(this, false)" style="cursor:hand" onclick="toggleRecord(this);" title="click here to select this row">
>>        <td>$bean.id.format('%d')</td>
>>        <td>$bean.title.format('%s')
>> </td>
>>        <td>$bean.price.format('%.2f')</td>
>>        <td>$bean.posttime.dateFormat('')</td>
>>        <td>$bean.desc.format('%s')
>> </td>
>>        <td>#foreach ($file in $bean.photo.files)
>>  <a href="${file.filePath}">${file.title}[${file.fileSize}]</a><br>
>> #end</td>
>>        <td>$bean.publishdate.dateFormat('')</td>
>>        <td>$bean.ispop.format('%s')
>> </td>
>>        <td>$bean.vfield.value</td>
>>        <td>$bean.location.lon, $bean.location.lat</td>
>>        <td><div id="13" class="DataTableView"></div>
>> <script>
>>  var jsoQuery_13 = new SlaveQuery('cn.csdb.paperdb.author', '${bean.id.jdbcObject}', 'cn.csdb.paperdb.book.authors');
>>  jsoQuery_13.pageSize = 20;
>>  var jsoDataTableView_13 = new DataTableView('cn.csdb.paperdb.author', jsoQuery_13, $('#13.DataTableView')[0]);
>>  jsoDataTableView_13.refresh();
>> </script></td>
>>        <td align="center" valign="top" class="bg2"><span id="TITLE_$velocityCount" style="display:none">$bean.title.format('%s')
>> </span>
>>  #if($!authenticator)
>>  <a href=# onclick="updateRecord('8a8080841c833108011c8331087c000a', '${id}');">update</a> <a href=# onclick="deleteRecord('8a8080841c833108011c8331087c000a', '${id}');">delete</a>
>>  #end
>>      </td>
>>  </tr>
>>  #end
>> </table>
>>  <p> <input type="checkbox" onclick="checkRecords(this);">select all
>>  &nbsp;
>>  #if($!authenticator)
>>  <button onclick='deleteRecords();'><img src='/images/submit_n.gif' border=0 align=absbottom>delete all</button>
>>  #end
>>  </p>
>>  #if ($pageViewer.pageCount > 1)
>> <br>
>> <center>
>>    <div style="width:100%">
>>      <div align="right">${pageViewer.footerBar1}</div>
>>    </div>
>>    <div style="width:100%">
>>      <div align="right">${pageViewer.footerBar2}</div>
>>    </div>
>> </center>
>>  #end
>> #end
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

Re: The loading of such a template cost 16000ms :(

Posted by Nathan Bubna <nb...@gmail.com>.
16 seconds?   I'm with Byron on this.  Something is wrong, and it's
not with your template or Velocity's parsing.  This template is
neither long enough nor complicated enough to get anywhere close to
that time for parsing on any sort of modern system.  What system is
this on?  Are you using a custom resource loader?  URLResourceLoader
over a slow connection?  What profiler are you getting the 16000ms
time from?  It would be best to know exactly where in your system this
time is being spent.

On Mon, Dec 29, 2008 at 1:11 AM, bluejoe <bl...@gmail.com> wrote:
> Hi, all. My template file is shown below, getTemplate() cost more than 16000ms to load it. I don't know what's wrong with it. This template is created automatically by another Velocity client class (I named it as TemplatePublisher), so it is too ugly and long. Who can tell me how to optimize this template to make the loading time shorter?
>
> thanks a lot!
>
> ============== my poor template ==============
>
> <!--created by VdbPublisher at 2008-12-27 00:14:31-->
> #if ($pageViewer.beans.size() == 0)
> <center>
>  <div style="width:100%" align="left"> <br>
>    <p><font color="#cccccc">no records...</font></p>
>  </div>
> </center>
> #else
> <table align="center" width="100%" border="1" cellpadding="6" class="table1 dataset book">
>  <tr class="header">
>       <td align="center" fieldId="8a8080841c833108011c8331087c0002" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by number" ><b><img src="/images/key.gif" align="absbottom"> number</b>#if ('8a8080841c833108011c8331087c0002' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="8a8080841c833108011c8331087c0003" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by title" ><b> title</b>#if ('8a8080841c833108011c8331087c0003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="1f62836e1c8dd5da011c8dd909b00001" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by price" ><b> price</b>#if ('1f62836e1c8dd5da011c8dd909b00001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="1f62836e1c8dd5da011c8dd925d30003" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by modification time" ><b> modification time</b>#if ('1f62836e1c8dd5da011c8dd925d30003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="1f62836e1c8de132011c8e09666d0001" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by description" ><b> description</b>#if ('1f62836e1c8de132011c8e09666d0001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="1f62836e1c8de132011c8e0984550003" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> images</b>#if ('1f62836e1c8de132011c8e0984550003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="1f62836e1c8de132011c8e09b4000005" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by publish date" ><b> publish date</b>#if ('1f62836e1c8de132011c8e09b4000005' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="ff8080811db4d587011db4e3fab5000a" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by isNew" ><b> isNew</b>#if ('ff8080811db4d587011db4e3fab5000a' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="ff8080811db4d587011db4e811f3000d" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> vfield</b>#if ('ff8080811db4d587011db4e811f3000d' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="ff8080811db4ec19011db4ec61c50001" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> location</b>#if ('ff8080811db4ec19011db4ec61c50001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>          <td align="center" fieldId="ff8080811e5245cc011e524aaa1a0005" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> authors</b>#if ('ff8080811e5245cc011e524aaa1a0005' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
>        <td class="header"></td>
>  </tr>
>  #foreach ($bean in $pageViewer.beans)
>  #set ($id = $bean.id.jdbcObject)
>  #set ($flag = $velocityCount%2)
>    <tr id="DATAGRID_ROW" class="bg${flag}" checked="false" originalClass="bg$flag" rowID="${id}" onmouseout="hiliteRecord(this, false)" style="cursor:hand" onclick="toggleRecord(this);" title="click here to select this row">
>        <td>$bean.id.format('%d')</td>
>        <td>$bean.title.format('%s')
> </td>
>        <td>$bean.price.format('%.2f')</td>
>        <td>$bean.posttime.dateFormat('')</td>
>        <td>$bean.desc.format('%s')
> </td>
>        <td>#foreach ($file in $bean.photo.files)
>  <a href="${file.filePath}">${file.title}[${file.fileSize}]</a><br>
> #end</td>
>        <td>$bean.publishdate.dateFormat('')</td>
>        <td>$bean.ispop.format('%s')
> </td>
>        <td>$bean.vfield.value</td>
>        <td>$bean.location.lon, $bean.location.lat</td>
>        <td><div id="13" class="DataTableView"></div>
> <script>
>  var jsoQuery_13 = new SlaveQuery('cn.csdb.paperdb.author', '${bean.id.jdbcObject}', 'cn.csdb.paperdb.book.authors');
>  jsoQuery_13.pageSize = 20;
>  var jsoDataTableView_13 = new DataTableView('cn.csdb.paperdb.author', jsoQuery_13, $('#13.DataTableView')[0]);
>  jsoDataTableView_13.refresh();
> </script></td>
>        <td align="center" valign="top" class="bg2"><span id="TITLE_$velocityCount" style="display:none">$bean.title.format('%s')
> </span>
>  #if($!authenticator)
>  <a href=# onclick="updateRecord('8a8080841c833108011c8331087c000a', '${id}');">update</a> <a href=# onclick="deleteRecord('8a8080841c833108011c8331087c000a', '${id}');">delete</a>
>  #end
>      </td>
>  </tr>
>  #end
> </table>
>  <p> <input type="checkbox" onclick="checkRecords(this);">select all
>  &nbsp;
>  #if($!authenticator)
>  <button onclick='deleteRecords();'><img src='/images/submit_n.gif' border=0 align=absbottom>delete all</button>
>  #end
>  </p>
>  #if ($pageViewer.pageCount > 1)
> <br>
> <center>
>    <div style="width:100%">
>      <div align="right">${pageViewer.footerBar1}</div>
>    </div>
>    <div style="width:100%">
>      <div align="right">${pageViewer.footerBar2}</div>
>    </div>
> </center>
>  #end
> #end

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


The loading of such a template cost 16000ms :(

Posted by bluejoe <bl...@gmail.com>.
Hi, all. My template file is shown below, getTemplate() cost more than 16000ms to load it. I don't know what's wrong with it. This template is created automatically by another Velocity client class (I named it as TemplatePublisher), so it is too ugly and long. Who can tell me how to optimize this template to make the loading time shorter?

thanks a lot! 

============== my poor template ==============

<!--created by VdbPublisher at 2008-12-27 00:14:31-->
#if ($pageViewer.beans.size() == 0)
<center>
  <div style="width:100%" align="left"> <br>
    <p><font color="#cccccc">no records...</font></p>
  </div>
</center>
#else
<table align="center" width="100%" border="1" cellpadding="6" class="table1 dataset book">
  <tr class="header">
       <td align="center" fieldId="8a8080841c833108011c8331087c0002" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by number" ><b><img src="/images/key.gif" align="absbottom"> number</b>#if ('8a8080841c833108011c8331087c0002' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="8a8080841c833108011c8331087c0003" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by title" ><b> title</b>#if ('8a8080841c833108011c8331087c0003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="1f62836e1c8dd5da011c8dd909b00001" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by price" ><b> price</b>#if ('1f62836e1c8dd5da011c8dd909b00001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="1f62836e1c8dd5da011c8dd925d30003" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by modification time" ><b> modification time</b>#if ('1f62836e1c8dd5da011c8dd925d30003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="1f62836e1c8de132011c8e09666d0001" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by description" ><b> description</b>#if ('1f62836e1c8de132011c8e09666d0001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="1f62836e1c8de132011c8e0984550003" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> images</b>#if ('1f62836e1c8de132011c8e0984550003' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="1f62836e1c8de132011c8e09b4000005" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by publish date" ><b> publish date</b>#if ('1f62836e1c8de132011c8e09b4000005' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="ff8080811db4d587011db4e3fab5000a" orderAsc="${jsoQuery.orderAsc}"  onclick="setOrderBy(this)" style="cursor:hand" title="order by isNew" ><b> isNew</b>#if ('ff8080811db4d587011db4e3fab5000a' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="ff8080811db4d587011db4e811f3000d" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> vfield</b>#if ('ff8080811db4d587011db4e811f3000d' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="ff8080811db4ec19011db4ec61c50001" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> location</b>#if ('ff8080811db4ec19011db4ec61c50001' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
          <td align="center" fieldId="ff8080811e5245cc011e524aaa1a0005" orderAsc="${jsoQuery.orderAsc}"  style="color:#888888" title="unsortable" ><b> authors</b>#if ('ff8080811e5245cc011e524aaa1a0005' == $jsoQuery.orderId)<img src="/images/${jsoQuery.orderAsc}.gif" align="absbottom" width="13">#end</td>
        <td class="header"></td>
  </tr>
 #foreach ($bean in $pageViewer.beans)
  #set ($id = $bean.id.jdbcObject)
  #set ($flag = $velocityCount%2)
    <tr id="DATAGRID_ROW" class="bg${flag}" checked="false" originalClass="bg$flag" rowID="${id}" onmouseout="hiliteRecord(this, false)" style="cursor:hand" onclick="toggleRecord(this);" title="click here to select this row">
        <td>$bean.id.format('%d')</td>
        <td>$bean.title.format('%s')
</td>
        <td>$bean.price.format('%.2f')</td>
        <td>$bean.posttime.dateFormat('')</td>
        <td>$bean.desc.format('%s')
</td>
        <td>#foreach ($file in $bean.photo.files)
 <a href="${file.filePath}">${file.title}[${file.fileSize}]</a><br>
#end</td>
        <td>$bean.publishdate.dateFormat('')</td>
        <td>$bean.ispop.format('%s')
</td>
        <td>$bean.vfield.value</td>
        <td>$bean.location.lon, $bean.location.lat</td>
        <td><div id="13" class="DataTableView"></div>     
<script>
 var jsoQuery_13 = new SlaveQuery('cn.csdb.paperdb.author', '${bean.id.jdbcObject}', 'cn.csdb.paperdb.book.authors');
 jsoQuery_13.pageSize = 20;
 var jsoDataTableView_13 = new DataTableView('cn.csdb.paperdb.author', jsoQuery_13, $('#13.DataTableView')[0]);
 jsoDataTableView_13.refresh();
</script></td>
        <td align="center" valign="top" class="bg2"><span id="TITLE_$velocityCount" style="display:none">$bean.title.format('%s')
</span>
  #if($!authenticator)
  <a href=# onclick="updateRecord('8a8080841c833108011c8331087c000a', '${id}');">update</a> <a href=# onclick="deleteRecord('8a8080841c833108011c8331087c000a', '${id}');">delete</a>
  #end
      </td>
  </tr>
 #end
</table>
  <p> <input type="checkbox" onclick="checkRecords(this);">select all
  &nbsp;
 #if($!authenticator)
 <button onclick='deleteRecords();'><img src='/images/submit_n.gif' border=0 align=absbottom>delete all</button>
 #end
  </p>
 #if ($pageViewer.pageCount > 1)
<br>
<center>
    <div style="width:100%">
      <div align="right">${pageViewer.footerBar1}</div>
    </div>
    <div style="width:100%">
      <div align="right">${pageViewer.footerBar2}</div>
    </div>
</center>
 #end
#end

Re: Which flavor of Velocity to use?

Posted by Nathan Bubna <nb...@gmail.com>.
No whitespace stripper in Velocity.  Last i checked, JTidy was the
best option.  I'm not aware of another Apache one at the moment.

On Sun, Dec 28, 2008 at 7:46 PM, Steve Cohen <sc...@javactivity.org> wrote:
> Thanks.  I didn't understand that both properties were needed.   (And I now
> understand what the configuration documentation was trying to tell me.)
>
> This is now working for me, except for one thing:
>
> The AOL converter seems to strip out all whitespace from the html it
> generates.  Since I'm not using the AOL converter here, (the velocity
> templates are serving the function of providing the html), whitespace is not
> getting stripped and the output ends up looking not so hot.  The whole point
> of doing this is to be able to code the templates more or less visually as
> simple html pages.  I don't want unreadable templates.  So I need an html
> whitespace stripper.  I suppose I could use JTidy but I'd like to avoid
> adding yet another jar.  Is there anything in Velocity itself (or perhaps in
> the Apache toolset) that takes html and strips out the whitespace?
>
> Nathan Bubna wrote:
>>
>> yeah, the WebappLoader was renamed to the WebappResourceLoader for
>> VelocityTools 2
>>
>> and your properties need to be:
>>
>> resource.loader = webapp
>> webapp.resource.loader.class =
>> org.apache.velocity.tools.view.servlet.WebappLoader
>>
>> the resource.loader property names the resource loaders you want to
>> use (in the order desired), then the other properties must be prefixed
>> with the proper names.
>>
>> and yeah, singleton should work as well as the engine, though i
>> recommend using the engine unless you have a good reason for using the
>> singleton.
>>
>> On Sun, Dec 28, 2008 at 5:23 PM, Steve Cohen <sc...@javactivity.org>
>> wrote:
>>
>>>
>>> Thanks, Nathan.  This sounds like the right approach.
>>>
>>> Do you mean org.apache.velocity.tools.view.servlet.WebappLoader?
>>> I don't find a WebappResourceLoader.  Assuming you do mean the
>>> WebappLoader,
>>> then I'm having a little trouble getting it to initialize properly.  Will
>>> your method work with the Singleton approach?
>>>
>>> My initialization code is as follows:
>>>
>>>  public static void initVelocity(ServletContext ctx)
>>>  throws VelocityInitializationException
>>>  {
>>>            Velocity.setProperty(
>>> RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
>>>        "org.apache.velocity.runtime.log.Log4JLogChute" );
>>>
>>>      Velocity.setProperty("runtime.log.logsystem.log4j.logger",
>>>                      "Velocity");
>>>            Velocity.setProperty("resource.loader.class",
>>>              "org.apache.velocity.tools.view.servlet.WebappLoader");
>>>
>>>  Velocity.setApplicationAttribute("javax.servlet.ServletContext",
>>> ctx);
>>>
>>>
>>>      try {
>>>          Velocity.init();
>>>      } catch (Exception e) {
>>>          throw new VelocityInitializationException(e);
>>>      }
>>>  }
>>>
>>> Yet when I run it I see the following in the log:
>>>
>>> 2008-12-28 19:12:08,166 [main] DEBUG Velocity  - ResourceLoader
>>> instantiated:
>>> org.apache.velocity.runtime.resource.loader.FileResourceLoader
>>>
>>> and when I go to access my templates they, of course, can't be found.
>>>
>>> What am I doing wrong?
>>>
>>> Steve
>>>
>>> Nathan Bubna wrote:
>>>
>>>>
>>>> ...
>>>>
>>>> Yeah, i don't think you really need the VelocityViewServlet.  (Note
>>>> that the VelocityServlet class is deprecated).   But so long as you
>>>> are running in a webapp container like Tomcat and have access to a
>>>> ServletContext, you can use the WebappResourceLoader (from
>>>> VelocityTools 2) to load templates from your web context.  Just
>>>> remember to put the ServletContext object in your VelocityEngine (or
>>>> singleton) application attributes before you initialize it:
>>>>
>>>> VelocityEngine engine = new VelocityEngine();
>>>> engine.setApplicationAttribute("javax.servlet.ServletContext",
>>>> theServletContext);
>>>> engine.init(properties);
>>>> ...
>>>>
>>>>
>>>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Which flavor of Velocity to use?

Posted by Steve Cohen <sc...@javactivity.org>.
Thanks.  I didn't understand that both properties were needed.   (And I 
now understand what the configuration documentation was trying to tell me.)

This is now working for me, except for one thing:

The AOL converter seems to strip out all whitespace from the html it 
generates.  Since I'm not using the AOL converter here, (the velocity 
templates are serving the function of providing the html), whitespace is 
not getting stripped and the output ends up looking not so hot.  The 
whole point of doing this is to be able to code the templates more or 
less visually as simple html pages.  I don't want unreadable templates.  
So I need an html whitespace stripper.  I suppose I could use JTidy but 
I'd like to avoid adding yet another jar.  Is there anything in Velocity 
itself (or perhaps in the Apache toolset) that takes html and strips out 
the whitespace?

Nathan Bubna wrote:
> yeah, the WebappLoader was renamed to the WebappResourceLoader for
> VelocityTools 2
>
> and your properties need to be:
>
> resource.loader = webapp
> webapp.resource.loader.class =
> org.apache.velocity.tools.view.servlet.WebappLoader
>
> the resource.loader property names the resource loaders you want to
> use (in the order desired), then the other properties must be prefixed
> with the proper names.
>
> and yeah, singleton should work as well as the engine, though i
> recommend using the engine unless you have a good reason for using the
> singleton.
>
> On Sun, Dec 28, 2008 at 5:23 PM, Steve Cohen <sc...@javactivity.org> wrote:
>   
>> Thanks, Nathan.  This sounds like the right approach.
>>
>> Do you mean org.apache.velocity.tools.view.servlet.WebappLoader?
>> I don't find a WebappResourceLoader.  Assuming you do mean the WebappLoader,
>> then I'm having a little trouble getting it to initialize properly.  Will
>> your method work with the Singleton approach?
>>
>> My initialization code is as follows:
>>
>>   public static void initVelocity(ServletContext ctx)
>>   throws VelocityInitializationException
>>   {
>>             Velocity.setProperty(
>> RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
>>         "org.apache.velocity.runtime.log.Log4JLogChute" );
>>
>>       Velocity.setProperty("runtime.log.logsystem.log4j.logger",
>>                       "Velocity");
>>             Velocity.setProperty("resource.loader.class",
>>               "org.apache.velocity.tools.view.servlet.WebappLoader");
>>             Velocity.setApplicationAttribute("javax.servlet.ServletContext",
>> ctx);
>>
>>
>>       try {
>>           Velocity.init();
>>       } catch (Exception e) {
>>           throw new VelocityInitializationException(e);
>>       }
>>   }
>>
>> Yet when I run it I see the following in the log:
>>
>> 2008-12-28 19:12:08,166 [main] DEBUG Velocity  - ResourceLoader
>> instantiated: org.apache.velocity.runtime.resource.loader.FileResourceLoader
>>
>> and when I go to access my templates they, of course, can't be found.
>>
>> What am I doing wrong?
>>
>> Steve
>>
>> Nathan Bubna wrote:
>>     
>>> ...
>>>
>>> Yeah, i don't think you really need the VelocityViewServlet.  (Note
>>> that the VelocityServlet class is deprecated).   But so long as you
>>> are running in a webapp container like Tomcat and have access to a
>>> ServletContext, you can use the WebappResourceLoader (from
>>> VelocityTools 2) to load templates from your web context.  Just
>>> remember to put the ServletContext object in your VelocityEngine (or
>>> singleton) application attributes before you initialize it:
>>>
>>> VelocityEngine engine = new VelocityEngine();
>>> engine.setApplicationAttribute("javax.servlet.ServletContext",
>>> theServletContext);
>>> engine.init(properties);
>>> ...
>>>
>>>
>>>       


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Which flavor of Velocity to use?

Posted by Nathan Bubna <nb...@gmail.com>.
yeah, the WebappLoader was renamed to the WebappResourceLoader for
VelocityTools 2

and your properties need to be:

resource.loader = webapp
webapp.resource.loader.class =
org.apache.velocity.tools.view.servlet.WebappLoader

the resource.loader property names the resource loaders you want to
use (in the order desired), then the other properties must be prefixed
with the proper names.

and yeah, singleton should work as well as the engine, though i
recommend using the engine unless you have a good reason for using the
singleton.

On Sun, Dec 28, 2008 at 5:23 PM, Steve Cohen <sc...@javactivity.org> wrote:
> Thanks, Nathan.  This sounds like the right approach.
>
> Do you mean org.apache.velocity.tools.view.servlet.WebappLoader?
> I don't find a WebappResourceLoader.  Assuming you do mean the WebappLoader,
> then I'm having a little trouble getting it to initialize properly.  Will
> your method work with the Singleton approach?
>
> My initialization code is as follows:
>
>   public static void initVelocity(ServletContext ctx)
>   throws VelocityInitializationException
>   {
>             Velocity.setProperty(
> RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
>         "org.apache.velocity.runtime.log.Log4JLogChute" );
>
>       Velocity.setProperty("runtime.log.logsystem.log4j.logger",
>                       "Velocity");
>             Velocity.setProperty("resource.loader.class",
>               "org.apache.velocity.tools.view.servlet.WebappLoader");
>             Velocity.setApplicationAttribute("javax.servlet.ServletContext",
> ctx);
>
>
>       try {
>           Velocity.init();
>       } catch (Exception e) {
>           throw new VelocityInitializationException(e);
>       }
>   }
>
> Yet when I run it I see the following in the log:
>
> 2008-12-28 19:12:08,166 [main] DEBUG Velocity  - ResourceLoader
> instantiated: org.apache.velocity.runtime.resource.loader.FileResourceLoader
>
> and when I go to access my templates they, of course, can't be found.
>
> What am I doing wrong?
>
> Steve
>
> Nathan Bubna wrote:
>>
>> ...
>>
>> Yeah, i don't think you really need the VelocityViewServlet.  (Note
>> that the VelocityServlet class is deprecated).   But so long as you
>> are running in a webapp container like Tomcat and have access to a
>> ServletContext, you can use the WebappResourceLoader (from
>> VelocityTools 2) to load templates from your web context.  Just
>> remember to put the ServletContext object in your VelocityEngine (or
>> singleton) application attributes before you initialize it:
>>
>> VelocityEngine engine = new VelocityEngine();
>> engine.setApplicationAttribute("javax.servlet.ServletContext",
>> theServletContext);
>> engine.init(properties);
>> ...
>>
>>
>>>
>>> Thanks.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>> For additional commands, e-mail: user-help@velocity.apache.org
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Which flavor of Velocity to use?

Posted by Steve Cohen <sc...@javactivity.org>.
Thanks, Nathan.  This sounds like the right approach.

Do you mean org.apache.velocity.tools.view.servlet.WebappLoader?
I don't find a WebappResourceLoader.  Assuming you do mean the 
WebappLoader, then I'm having a little trouble getting it to initialize 
properly.  Will your method work with the Singleton approach?

My initialization code is as follows:

    public static void initVelocity(ServletContext ctx)
    throws VelocityInitializationException
    {
       
        Velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
          "org.apache.velocity.runtime.log.Log4JLogChute" );

        Velocity.setProperty("runtime.log.logsystem.log4j.logger",
                        "Velocity");
       
        Velocity.setProperty("resource.loader.class",
                "org.apache.velocity.tools.view.servlet.WebappLoader");
       
        Velocity.setApplicationAttribute("javax.servlet.ServletContext", 
ctx);


        try {
            Velocity.init();
        } catch (Exception e) {
            throw new VelocityInitializationException(e);
        }
    }

Yet when I run it I see the following in the log:

2008-12-28 19:12:08,166 [main] DEBUG Velocity  - ResourceLoader 
instantiated: org.apache.velocity.runtime.resource.loader.FileResourceLoader

and when I go to access my templates they, of course, can't be found.

What am I doing wrong?

Steve

Nathan Bubna wrote:
> ...
>
> Yeah, i don't think you really need the VelocityViewServlet.  (Note
> that the VelocityServlet class is deprecated).   But so long as you
> are running in a webapp container like Tomcat and have access to a
> ServletContext, you can use the WebappResourceLoader (from
> VelocityTools 2) to load templates from your web context.  Just
> remember to put the ServletContext object in your VelocityEngine (or
> singleton) application attributes before you initialize it:
>
> VelocityEngine engine = new VelocityEngine();
> engine.setApplicationAttribute("javax.servlet.ServletContext",
> theServletContext);
> engine.init(properties);
> ...
>
>   
>> Thanks.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>>     
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Which flavor of Velocity to use?

Posted by Nathan Bubna <nb...@gmail.com>.
On Fri, Dec 26, 2008 at 2:53 PM, Steve Cohen <sc...@javactivity.org> wrote:
> I want to try to port an existing application to Velocity.
>
> This application is in some ways quite like a Web App, yet it is not one.
>  It's user-interface is delivered through the AOL Instant Messenger SDK.  In
> AIM terms, it is a "bot".  Users chat with it as they would with any other
> AIM buddy and it performs various tasks for them, during which it presents
> them with various bits of information.
>
> AIM delivers instant messages to our bot, which is implemented using AIM's
> SDK.  An Instant Message contains of course the text of the IM, which our
> bot processes much like a command-line parser of a sort.  A properly entered
> command leads to some sort of action being performed which might be data
> retrieval or modification, or something else.  An improperly entered command
> leads to an "error page".  No matter what it is, the feedback to the user is
> an IM sent back to him via AIM.  Our bot's interface to AIM going in this
> direction is again the AIM SDK.  This is analogous to a servlet response.
>
> Additionally, although there are alternatives, AOL's preferred mechanism for
> receiving inputs from a "bot" is that it be a limited form of xhtml - that
> is, only a restricted subset of xhtml is allowed.  Things like <b>, <br/>,
> and <font> are allowed, <table> is not.  The AIM SDK provides a way of
> converting strings to this format, but it is not required to use this API.
>
> Up until now, the relatively simple nature of this interface led me down the
> primrose path of simply taking strings as input, parsing them, running them
> through whatever process was required and spitting back out the required
> output.  I gave no thoughts to MVC.  Possible outputs were stored in a
> properties file, often with various replaceable parameters.  This was easily
> handled in java, but it's become more cumbersome as the size of this
> properties file increased.  It got way more cumbersome when I started
> needing to format the text.  I developed a clever (too clever by half)
> mechanism for formatting this text using java, but I really need to get away
> from that.  Java is doing way too much.  I would rather now have a bunch of
> little "html pages" that have the formatting on them, with the content to be
> popped in via templating.
>
> What I want is to have the various boilerplate outputs stored as velocity
> templates, in an intelligently organized directory tree structure, and use
> the velocity mechanism to merge in the live data.  Fortunately, even though
> this application isn't a WebApp, I long ago made the decision to run it
> inside of Tomcat.  Although it isn't a Web application, there are a couple
> of minor interfaces that do access the application through servlets, hence
> Tomcat.  Thus the architecture is all already there to put all the templates
> under the Web Context root.
>
> However, as I said, it ISN'T actually a web app.  It doesn't deliver content
> to users via the http request/response paradigm.  One idea for using
> velocity in this situation would be to make http://localhost:8080 calls to
> the Velocity Servlet, get the merged output, read it into a StringWriter,
> and send that on to the user.  But that doesn't feel right.  It feels like
> needless overhead.
>
> I would like to invoke Velocity like a non-webapp, through java, but read
> the templates from the Web Context as though it were a web app.  Has anyone
> done anything like that, or otherwise have helpful advice for me?

Yeah, i don't think you really need the VelocityViewServlet.  (Note
that the VelocityServlet class is deprecated).   But so long as you
are running in a webapp container like Tomcat and have access to a
ServletContext, you can use the WebappResourceLoader (from
VelocityTools 2) to load templates from your web context.  Just
remember to put the ServletContext object in your VelocityEngine (or
singleton) application attributes before you initialize it:

VelocityEngine engine = new VelocityEngine();
engine.setApplicationAttribute("javax.servlet.ServletContext",
theServletContext);
engine.init(properties);
...

> Thanks.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Which flavor of Velocity to use?

Posted by Byron Foster <by...@base2.cc>.
On Dec 26, 2008, at 15:53 , Steve Cohen wrote:

> AIM delivers instant messages to our bot, which is implemented using  
> AIM's SDK.  An Instant Message contains of course the text of the  
> IM, which our bot processes much like a command-line parser of a  
> sort.  A properly entered command leads to some sort of action being  
> performed which might be data retrieval or modification, or  
> something else.  An improperly entered command leads to an "error  
> page".  No matter what it is, the feedback to the user is an IM sent  
> back to him via AIM.  Our bot's interface to AIM going in this  
> direction is again the AIM SDK.  This is analogous to a servlet  
> response.
>
> Additionally, although there are alternatives, AOL's preferred  
> mechanism for receiving inputs from a "bot" is that it be a limited  
> form of xhtml - that is, only a restricted subset of xhtml is  
> allowed.  Things like <b>, <br/>, and <font> are allowed, <table> is  
> not.  The AIM SDK provides a way of converting strings to this  
> format, but it is not required to use this API.
>
> Up until now, the relatively simple nature of this interface led me  
> down the primrose path of simply taking strings as input, parsing  
> them, running them through whatever process was required and  
> spitting back out the required output.  I gave no thoughts to MVC.   
> Possible outputs were stored in a properties file, often with  
> various replaceable parameters.  This was easily handled in java,  
> but it's become more cumbersome as the size of this properties file  
> increased.  It got way more cumbersome when I started needing to  
> format the text.  I developed a clever (too clever by half)  
> mechanism for formatting this text using java, but I really need to  
> get away from that.  Java is doing way too much.  I would rather now  
> have a bunch of little "html pages" that have the formatting on  
> them, with the content to be popped in via templating.
>
> What I want is to have the various boilerplate outputs stored as  
> velocity templates, in an intelligently organized directory tree  
> structure, and use the velocity mechanism to merge in the live  
> data.  Fortunately, even though this application isn't a WebApp, I  
> long ago made the decision to run it inside of Tomcat.  Although it  
> isn't a Web application, there are a couple of minor interfaces that  
> do access the application through servlets, hence Tomcat.  Thus the  
> architecture is all already there to put all the templates under the  
> Web Context root.
>
> However, as I said, it ISN'T actually a web app.  It doesn't deliver  
> content to users via the http request/response paradigm.  One idea  
> for using velocity in this situation would be to make http://localhost:8080 
>  calls to the Velocity Servlet, get the merged output, read it into  
> a StringWriter, and send that on to the user.  But that doesn't feel  
> right.  It feels like needless overhead.
>
> I would like to invoke Velocity like a non-webapp, through java, but  
> read the templates from the Web Context as though it were a web  
> app.  Has anyone done anything like that, or otherwise have helpful  
> advice for me?


It's fairly straight forward to call Velocity outside of any type of  
web-app environment:

http://velocity.apache.org/engine/releases/velocity-1.6.1/developer-guide.html#thevelocityhelperclass

Since you want to go in the MVC direction you may not want to invoke a  
velocity template directly but maybe reference some sort of logical  
name that refers to a method or class that contains the controller  
logic.  The controller logic would then initialize the context and  
specify the velocity template to render. For example:

public String dispatch(String cntrlName) {
   VelocityContext ctx = new VelocityContext();
   String template;
   if (cntrlName.equals("foo") {
     ctx.put("msg", "this is foo");
     template = "foo.vm";
   }
   else if (cntrName.equals("bar") {
     ctx.put("msg", "this is bar");
     template = "bar.vm";
   }
   else { // Some error}
  	
   StringWriter writer = new StringWriter();
   Velocity.merge(template, "utf-8", ctx, writer);
   return writer.toString();
}

The above won't compile of course, and the setup is primitive, but you  
get the idea.  If you wanted to hit a velocity page directly you can  
always add the logic for it. So, this is independent of any mechanism  
of providing a request.

You could then create a simple servlet that would map an URL to a  
dispatch cntrlName, call something like above, then stream the result  
to the response.

The above dispatch setup can also be accessed by your AOL SDK front End.

Your templates would generate your XHTML, text or whatever flavor.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org