On Mon, 22 Sep 2014 05:32:24 -0300, Stephen Nutbrown <steves...@gmail.com> wrote:

Hello,

Hi!

I am wondering the best way to structure this within Tapestry 5 - I
can see lots of ways, but i'm trying to consider the best way.

I have the requirement to make approximately 125 pages (or rather, 1
page with a possible 125 different sets of data). Each page is a short
tutorial which has on it:
- A description
- An example of source code of something done badly
- An example of the same piece of code improved.
- A link to some documentation.
All of the information for each one is to be stored in text files,
probably stored in src/main/webapp/tutorials, but I am not quite sure
where exactly to put them yet.

Create some kind of database (SQL, NoSQL, files, ...) and one class to represent the tutorials. Write a Tapestry-IoC service that retrieves the tutorial from the database.

The next thing is that I would like to read the argument from the URL
- this isn't a problem (e.g
something.com/tutorials/?tutorial=ifstatements ). This means I don't
need to make the 125 pages, but can use the passed param to find the
correct data to show on the page. I've done this before and i'm fine
with this part.

Why not something.com/tutorials/ifstatements? If you have a Tutorials page class, it'll receive the tutorial id in onActivate(String id). That's what we call the page activation context and it has better SEO than query parameters. More details on http://tapestry.apache.org/page-navigation.html.

So my main questions are:
- How do I make it so that Tapestry will cache these text files to
avoid reading them each and every time the page is loaded? One option
I think is to use @Cached and to load all of the data in to some kind
of list of tutorial objects. I can then pick the correct object from
the list to show on the page. Or should I be doing this as a service?
(http://tapestry.apache.org/defining-tapestry-ioc-services.html)

@Cached doesn't do what you think. It just caches the return value of a page, component or mixin class (not services) inside one thread (request). Yes, you should create a Tapestry-IoC service for it and put your caching logic yet. From Tapestry-IoC 5.4 on, which has just released betas so far, you can use tapestry-ioc-jcache to provide caching to your services easily using annotations JCache (JSR 107) annotations.

- Is this worth doing? I would think the pages will be viewed fairly regularly.

I'd wait to do the caching until it's proven necessary.

- is src/main/webapp/tutorials a sensible place to store them?

I don't think so. I'd put inside the classpath (src/main/resources, probably src/main/resources/tutorials. This way, you can use the Asset getClasspathAsset(String path, Locale locale) method of AssetSource to get an Asset pointing to one of your tutorial files. Asset.getResource().openStream() will give you an InputStream to read the file. This whole method leverages the caching Tapestry already does, so you don't need to worry about caching at all.

You can pass null to the second parameter of getClasspathAsset() without problems. On the other hand, if you translate your tutorials to other languages, you can use this to choose the language. @Inject private Locale locale; will give you the locale from the user browser.

- Should I be doing this totally differently? It's going to take me a
while to write these pages, so I really don't want to have to do it
again.

You'll write exactly one Tapestry page class which will handle n tutorials, so you'll spend way more time writing the tutorials themselves than writing the Tapestry code to show them.

I'd use some SQL no NoSQL database to store the data instead of files. MongoDB is dead simple to install (at least in Linux) and use (any OS), with or without tapestry-mongodb, only available in the Tapestry 5.4 betas.

--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to