Hi Tim,
Velocity has a way to check if a template/resource exists through this static
method[1]:
Velocity.resourceExists(name);
I'm not sure how efficient this check is though as it might touch the file
system. You could ask on
the Velocity mailing list just to make sure. If it does touch the file system
it should be easy to
cache it in your app through some utility class:
VelocityUtils {
private static Map<String, Integer> CACHE = new ConcurrentHashMap();
public boolean resourceExists(String name) {
int val = CACHE.get(name);
if (val == 1) {
return true;
} else if (val == 0) {
return false;
} else {
if(Velocity.resourceExists(name)) {
CACHE.put(name, 1);
} else {
CACHE.put(name, 0);
}
}
}
or something along those lines.
Id you are using GAE you should probably use 2.3.0-M1 which provides a
workaround[2] for a GAE bug
which breaks Page automapping.
Hope this helps
Kind regards
Bob
[1]:
http://velocity.apache.org/engine/releases/velocity-1.7/apidocs/org/apache/velocity/app/Velocity.html#resourceExists(java.lang.String)
[2]:
http://click.apache.org/docs/extras-api/org/apache/click/extras/gae/GoogleAppEngineListener.html
(See the Limitations section in the Javadoc)
On 27/12/2010 10:34, Tim Christensen wrote:
> I am building a web application using Click on the Google App Engine. One of
> the 'features' I would
> like to have is the ability to try setting a template -- if it is available.
> Like the Click example
> we have a BasePage > BorderPage (with a template of border.htm) and our Page
> classes extend BorderPage.
>
> One of those Page classes is BrandPage -- for some consumer brands. The
> BrandPage will have some
> common methods - search for products matching this brand for example - think
> ecommerce.
> Additionally, there is a brand.htm file for the BrandPage.
>
> However, I would like some brands, not all, to have a template available to
> parse - we would like to
> build out some custom .htm pages to parse in the brand.htm. For example we
> may have three brands ,
> A, B, C and A.htm, C.htm but not a B.htm in a directory say ... /brand/A.htm
>
> Normally in the BrandPage I would put something like this:
>
> public String getTemplate()
> {
> return A.htm (or C.htm, or even B.htm)
> }
>
> From the example above, while A.htm or C.htm exist, B.htm does not.
>
> How this would have happened is that when the BrandPage was requested, with a
> brand id for example,
> the database search would return the brand (and product etc...). I would then
> 'dynamically' set the
> getTemplate() return to [Normalized Brand Name].htm
>
> My question is this -- how can I test to see if B.htm exists before I even
> set the getTemplate
> return value? My focus has been using something utility-wise in Click - am I
> missing a simple way to
> do this? Wrap a try catch somewhere? As you would expect, I currently get the
> org.apache.velocity.exception.ResourceNotFoundException -- but I cannot catch
> it in the Page in
> order to do something else.
>
> I also want it to be a quick process. I have pondered using File to look in a
> specific directory
> ("/brand/file.htm"). My biggest concerns are: 1. Speed for this 'function',
> 2. Google App Engine
> restrictions - would looking at a file in the /war cause an issue, and
> finally if Click has
> something I missed as a convenience for this idea. Solving this gives us
> tremendous flexibility on
> the presentation and is a critical feature to our application. I am also
> concerned that the way I am
> thinking about this is flawed so any alternative design pattern suggestions
> are welcome.
>
> Thank you for a great framework - I have tried nearly all of them and Click
> nailed it.
>