One of the promises of Tapestry 5 is easy creation of components. The word component can trigger lots of different associations in people’s minds. I guess for the majority of web developers the component means a GUI thing. For someone from the middle management a component might as well be a forum system with all the bells and whistles. For old school guys the first impression when the word component is a word of discussion might mean an architectural thing.
To me, the term components, is quite a familiar for a number of years now. When I started with web application development I was a lucky man as Web Objects were my first contact in a jungle of languages and frameworks out there. You can imagine (I hope) the disappointment when I had to switch to JSP. Only now I got an opportunity to use similar technology again!
Until now, all my posts were talking about preparing the development environment and this one feels like I am jumping ahead of time. In a sense this is true, but I think creating a place to put the goodies you will create and reuse is a good start. Why would you create a component and later copy it to a library? Let’s do it right now.
The tools are: Eclipse Ganymede, m2eclipse 0.9.7, Maven 2.0.10 and Java 6.
In Eclipse click the New button then type maven in the filter box and select Maven Project Click Next and select Create a simple project. Click Next and fill-in the configuration details. All of the values I have filled-in do not have any special meaning. You can name your GroupId and ArtifactId anything you want.
Now we will skip the last step of the wizard (adding dependencies) and just press Finish. You should see a new project in the Project Explorer. Now double click the pom.xml to open it in Maven POM Editor. The content should be something like:
4.0.0 bb.webcraft.tapestry components-library bbWebCraft Tapestry 5 components library 0.0.1-SNAPSHOT A collection of Tapestry 5 components
We need to add some more elements in the pom for this project to become a Tapestry 5 components library. A bare minimum is:
4.0.0 bb.webcraft.tapestry components-library bbWebCraft Tapestry 5 components library 0.0.1-SNAPSHOT A collection of Tapestry 5 components org.apache.tapestry tapestry-core ${tapestry-release-version}
Please note that Tapestry-Module-Classes could as well be
com.acme.services.MyModule
but would be a bad practice. The java packages you will create in this library project will have the same root as the GroupId (bb.webcraft.tapestry).
Let’s create a package bb.webcraft.tapestry.library.services and then a java class named LibraryModule. This is now your library’s IoC module. In order for Tapestry to know where to search for your component’s classes you need to make a contribution to the ComponentClassResolver service configuration
public class LibraryModule { public static void contributeComponentClassResolver(Configuration<LibraryMapping> configuration) { configuration.add(new LibraryMapping("bbwcraft", "bb.webcraft.tapestry.library")); } }
The project is now ready. If it had any components, you would just put the jar on your web application’s class path and use it. You can create a jar by typing mvn package in the console or by right clicking on a project root (or pom.xml) and selecting Run As / Maven package. A jar named components-library-0.0.1-SNAPSHOT.jar should be created in the target folder. As you may know a jar has to be installed (mvn install) in your local maven repository to be used by other mavenized projects. You can then use the library by referencing it in your application’s pom like this:
Now would be the time and place to mention how to use the component from the library in some T5 application, but since we haven’t actually created one, we will leave this for the next post.
Before we end, let’s add another section in the library’s pom (under the <project> element).
This is very important, as you and Tapestry will create a good documentation about your components. I will also talk about how to write the documentation for the components in the next post where we will actually create a component – a date input component composed of three combo boxes (day, month, year) suitable for selecting dates far in the past or future.
I made a list of 36 additional UI components in the form of a survey. Please take this survey, maybe some of the components will be part of some library because of it. Maybe some of them will evolve and maybe some of them will be unique to any web framework out there.
When coding, sometimes we (at least me) forget to use all the help from an IDE we are coding in. One of techniques is context help.
I am sure you are all used to typing
so you quicker create a for loop.
To speed up developing web applications with Tapestry 5, I have created 4 templates just to show the principle. I will most likely create more as I learn the framework and find the patterns that are recurring more often.
See http://tapestry.apache.org/tapestry5/tapestry-core/ref/org/apache/tapestry5/corelib/components/ActionLink.html for a full component description. */
// Only keep the message until it has been displayed once. You must persist it // because ActionLink performs redirect after post. If you click refresh browser // button the message is lost, because it is persisted just for one request. @Persist(PersistenceConstants.FLASH) private String myMessageAfterPressingTheActionLink;
void onActionFromMyAction(String contextParameter) { myMessageAfterPressingTheActionLink = "You pressed an action link which had a literal string " + contextParameter + " in its context."; }
public String getMyMessageAfterPressingTheActionLink() { return myMessageAfterPressingTheActionLink; } }
The inserted code is more like a cheat sheet as it tells you what to insert in the appropriate template file to have a working example of the ActionLink component. It also gives a tip on why the field must be persisted.
All the templates also import the necessary classes from the framework, so you don’t even have to bother pressing Ctrl+Shift+O (Organize Imports).
Now, delete the code which has been inserted and try the second template (t5-addScriptLink).
/* The method addScriptLink adds a link to a script file, a JavaScript library. A component can inject such a script and pass one or more of assets to this method. Tapestry will ensure that the necessary <link> elements are added to the top of the document (just inside the element). The same can be achieved with the annotation
@IncludeJavaScriptLibrary("${tapestry.scriptaculous}/dragdrop.js") on a page (Index) class. */ @Inject @Path("${tapestry.scriptaculous}/dragdrop.js") private Asset dragDropLibrary;
One can now quickly modify the name of the JavaScript library that is to be added on this page. It also includes an alternative way of doing the same thing.
The third template is a real beast. It includes the code and instructions to have a working autocomplete example in minutes. Show this to your boss to demonstrate how quickly you can have this done in Tapestry 5. :-)
/* The Autocomplete mixin exists to allow a text field to query the server for completions for a partially entered phrase. It is often used in situations where the field exists to select a single value from a large set, too large to succesfully download to the client as a drop down list; for example, when the number of values to select from is numbered in the thousands.
public interface CityNames { List<String> getAll(); }
And the implementation:
public class CityNamesImpl implements CityNames { private List<String> cityNames = new ArrayList<String>();
public CityNamesImpl() { // top 7 cities by population cityNames.add("Mumbai"); cityNames.add("Shanghai"); cityNames.add("Karachi"); cityNames.add("Istanbul"); cityNames.add("Delhi"); cityNames.add("Sao Paulo"); cityNames.add("Moscow"); }
public List<String> getAll() { Collections.sort(cityNames); return Collections.unmodifiableList(cityNames); } }
Change the look of the autocomplete menu by overriding CSS classes DIV.t-autocomplete-menu UL DIV.t-autocomplete-menu LI DIV.t-autocomplete-menu LI.selected */ @Inject private CityNames cityNames;
@Property private String cityName;
List<String> onProvideCompletionsFromCityName(String partial) { List<String> matches = new ArrayList<String>();
for (String cityName : cityNames.getAll()) { if (cityName.toLowerCase().startsWith(partial.toLowerCase())) { matches.add(cityName); } } return matches; }
And the last one (t5-zone) which demonstrates the usage of AJAX in Tapestry 5.
/* A Zone can be updated via an ActionLink component. ActionLink supports a zone parameter, which is the id of the Zone's <div>. Clicking such a link will invoke an event handler method on the server as normal ... except that the return value of the event handler method is used to send a partial page response to the client, and the content of that response is used to update the Zone's <div> in place.
Put this into the Index.tml,
<t:block t:id="myBlockToBeUpdated"> The current time is: ${time} </t:block> <t:zone t:id="myZone"> <t:delegate to="myBlockToBeUpdated"/> </t:zone><br/> <a t:type="actionlink" t:id="refreshZone" href="#" t:zone="myZone">Refresh time with AJAX request.</a> */ @Inject @Property private Block myBlockToBeUpdated;
Block onActionFromRefreshZone() { // Return the zone we want rendered. return myBlockToBeUpdated; }
public Date getTime() { return new Date(); }
Again, if you follow the comments in the template, you will have a working example in under a minute.
And this is all about – getting beginners on the track fast! Including me.
As there is no initiative, at least to my knowledge, for Tapestry 5 support in Eclipse, we will have to manage with some manual tweaking.
First bring up a preference dialog (Windows/Preferences). Then type cont in the filter area to narrow down preferences which we are looking for. Click on Content Types under the General node. On the right side of the dialog find Text/HTML node and select it. All file associations for HTML content type are listed in the area just bellow.
We have to add Tapestry’s templates to be associated with HTML content type, so click on Add button and type *.tml in the input field and click OK.
You should see another entry in the File associations list - *.tml. Now confirm the addition by clicking on an OK button.
From now on a HTML editor which comes with Eclipse WTP is the default editor for tml files. This editor unfortunately does not handle formatting of the source code (tml in our case) very well.
If you are a one man band and don’t care about indenting the tags by some rules then you can safely skip this screencast bellow.
For others, who might have questioned themselves if there is a way, I will show you how you can make your templates look better in the editor and most importantly – with fewer conflicts if two or more designers are working repeatedly/in sequence with the same set of templates checked-in in some source control system like subversion and using the same formatter.
The screencast is best viewed in full screen mode.
Below are the tags that I was talking about in the screencast that should be copied into the Eclipse Tidy preferences. It is just a quick first proposition – like monkey sort (not at all tested)
It is not perfect, but good enough to eye scan the sources.
One more quick tip. Maybe you have noticed, you have to press F5 (refresh) on a running project (jetty:run) for some changes you have made to take effect. There is an option in the Preferences which can save you from pressing the button again and again.
Next time we will learn how to create java code templates to speed up Tapestry 5 web application development.
The goal of this task is to learn how Tapestry can boost your productivity by allowing you to modify (some) files and see the changes immediately. This does not mean we will be learning how to configure the servlet container of your choice to reload the context when some files were changed. Oh no, I will be talking and demonstrating live class reloading.
I will continue to work on a web application from Task 1. Seeing is believing, so click on a play button of the screencast below to witness live class reloading in action.
If you have already read the documentation at the official site, you probably know that you cannot change just any class and expect to be reloaded by the framework. A change to any loaded class inside the controlled package is noticed by the framework and acted appropriately upon. In case of our demo application the controlled packages are:
org.driving.school.pages
org.driving.school.components
org.driving.school.mixins
org.driving.school.base
We will be experimenting and exploring live class & template reloading in tasks to come.
As I “promised” in previous post I will tell you how to prepare your development environment to pick up changes in any CSS files your application will have. How can I be writing about something so basic after you have just watched a state of the art framework capability to pick up changes in class files? Well, if you are developing in Windows and using Jetty as your servlet container, then you will probably spend some time googling for a solution. I was.
But first let’s show the problem. In Eclipse (on Windows) open our school project. Add a css folder under src/main/webapp and create a new file school.css inside.
body { color: red; }
Then, modify Index.tml to include a reference to this CSS file. The head section should look like
Now start the web app if it was not already started. You should see
That is ok, red color as expected. Now change the color to black for instance in school.css in Eclipse while the web app is still running and save the changes. You will get
Now go googling. Just kidding, read on.
Stop web app by clicking on a red square in the Console view.
You have to get a copy of webdefault.xml file. Where? The safest way is to extract it from the jetty jar you are running your application with. If you look at the pom.xml file of the school application you will see a maven-jetty-plugin
<!-- Run the application using "mvn jetty:run" --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.9</version> <configuration> <!-- Log to the console. --> <requestLog implementation="org.mortbay.jetty.NCSARequestLog"> <!-- This doesn't do anything for Jetty, but is a workaround for a Maven bug that prevents the requestLog from being set. --> <append>true</append> </requestLog> </configuration> </plugin>
Notice the version 6.1.9. Navigate to %HOMEPATH%/.m2/repository/org/mortbay/jetty/jetty/6.1.9. Open jetty-6.1.9.jar and extract webdefault.xml file to src/main/resources/org/mortbay/jetty/webapp/ of the school project. Open this file in Eclipse and locate the section
Start the school web app again. Jetty started! If not, write a dirty comment on this blog.
What a detour. Do you still remember what we are trying to do? Type http://localhost:8080/school in the browser. Do you see the text in the color you have specified in the CSS file? Now change the color, save the school.css (you should not get an error dialog this time) and refresh the browser. Finally.
Let’s expand all the nodes of the project in the Package Explorer.
If you have never seen a project structure for a web application by Maven convention – this is it. Well, with Tapestry flavor. This “tree of files” is created by tapestry quickstart archetype.
Two sentences of theory before continuing.
It is WHERE the files END UP in the WAR that matters, not where they are in your development environment. The IDE and tools that you use should maximize the productivity and harness the power that is provided by Tapestry framework.
I will continue to describe one project layout with one set of tools in this post. I want to describe things simple. I will rather create another post for different set of tools and/or personal preferences of where things (files) can be.
The tools of choice are: Eclipse (3.4.0), Maven (2.0.9) and m2eclipse plugin (0.9.6).
As we are not starting from scratch – the archetype did the initial work for us – let’s have a look at the existing directories and files from top to bottom.
There are two java files in two different packages: Index.java and AppModule.java. The Index.java resides in the org.driving.school.pages package and it is in this package that you will add java sources for additional pages you will create. The AppModule.java is a Tapestry Inversion of Control Module. This is your application’s module builder class. In other words – it is where you will do most of the configuration.
The accompanying template file to the Index.java controller class is Index.tml. In the above figure it is located in the src/main/webapp/ directory. This file can also reside in src/main/resources/org/driving/school/pages directory. It is of personal/team CHOICE where the template files are located.
So, what else goes to src/main/resources? As you can observe from the figure above, the log4j.properties file is one of them. Why? Because when packaged, it ends up in the WEB-INF/classes, which means on the classpath. Message catalogs (.properties) also belong here.
You can imagine what goes to src/test/java and src/test/resources. I will not go into details here because this is not a crash course in Test Driven Development.
Next, there are two libraries: JRE System Library and Maven Dependencies. The latter includes all the jars needed by the project. Only two of them are really referenced in the project’s POM: tapestry-core and testng. The others are so called transitive dependencies (dependencies of dependencies). Maven takes care of downloading and including them in the classpath.
We will skip src/main/webapp/META-INF/MANIFEST.MF.
And then there is app.properties file under src/main/webapp/WEB-INF. This is a global message catalog. It will include a default language messages. They are called messages because you will be accessing them through the message prefix, but we will not discuss that now. If your application will be multilingual then you would create a file, for example, app_sl_SI.properties which would include Slovenian messages (translations).
Next, the only XML file you will need to have in your application – web.xml.
There is one context parameter. It has a value of org.driving.school. This tells Tapestry where to look for pages and components. That is why you have a package named org.driving.school.pages under src/main/java.
You can also see there is a filter named app which is implemented by TapestryFilter class. Every request that comes to application’s context (in our case http://localhost:8080/school) is processed by TapestryFilter.
Favicon.ico is a file which I think everyone knows what it is, so we will skip it.
Now the template file for the page – Index.tml. I have already mentioned it above. This is a XHTML file which includes elements in Tapestry namespace
The tml files are the View part of your app and those files will be the only ones developers will meet the designers. With very little practice no one will ruin the work of the other one. There is of course a lot more to tell for the templates, but as this blog is about hands-on approach to building web apps, I have to be short in words and more expressive in examples and real world cases.
One file remains – pom.xml. This is a Maven thing. It manages our project. It is here where you configure what java libraries do we need, and what version of the compiler to use, and what should be the name of the WAR (hence context name) etc.
It includes a jetty plugin which enables us to run the web app with mvn jetty:run, but unfortunately Windows users will have to do a bit more to achieve that changing something in the CSS will be visible in the browser without restarting the Jetty container. Yes, something as basic as that and it does not work out of the box by pressing F5 (refresh) in the browser. I was quite disappointed until I dug the solution on the net. I will write about this in the next post.
The quickstart archetype is really just that – quick. It does not demonstrate the users (developers) where components are, where images and css files should be, how pages can be grouped in subfolders/subpackages and a lot of other concepts. Of course this is all explained at official site, but in many cases in a technical lingo which does not suit everyone well, especially newbies. Several more archetypes should be created and made available to public via Maven repositories which would demonstrate and kick start a new project with some additional concepts.
So, to achieve the Task 2 I have to explain where to put your components. The java files for this project would go under src/main/java/org/driving/school/components and the resources (templates, message catalogs, javascript) under src/main/resources/org/driving/school/components.
Put your images in the src/main/webapp/images (or img or similar) and CSS files as a sibling to images (src/main/webapp/css).
This post ends here. It does not provide much value as we have only walked through the files of the quickstart project template, but it pointed out to some weak points which will all be addressed in the next posts. The goal is to remove the pit holes some of us have fallen into when started to use Tapestry 5.
Fire up Eclipse Ganymede and choose Java perspective. Make sure the eclipse is started up with JDK not JRE.
Select File/New/Other… from the Eclipse menu. In the dialog type maven in the Wizard's input field to reduce the number of wizards in the list. Select Maven Project.
In the New Maven Project dialog that appears next just click Next. Don’t get distracted by other options for now.
After clicking Next, you will see something similar like the image bellow.
In short - there are numerous so called archetypes (maven terminology for project templates) in the list which are identified by GroupId, ArtifactId and Version. Archetypes are “gathered” in catalogs and one of them is Nexus Indexer which includes all archetypes from the central maven repository.
If you don’t see any artifacts, then you don’t have central index downloaded or you haven’t chosen to include an optional component Maven Central repository index while installing m2eclipse plugin.
If you do see the artifacts, then you can skip the next two screenshots.
Check Software Updates and Add-ons in Help/Software updates… menu in Eclipse to confirm you have all the necessary components installed.
If you don’t have Maven Central repository index in the list of Installed Software, then select Available Software tab and install it by selecting this/these module(s) under Maven Integration for Eclipse Update Site.
To automatically download Nexus index (which includes Tapestry 5 archetypes) at Eclipse startup check Download repository index updates on startup in the Preferences.
Now let's continue. We are looking for Tapestry 5 archetype, so type tapestry in the Filter input field.
Select quickstart archetype, at present (December 2008) the version 5.0.18 is available. Click Next.
Now the New Maven Project Wizard displays a dialog which asks us about the name of the project. Name is not the right term in Maven world, but we will not go too deeply into theory for now, let’s just type org.driving for the GroupId and school for ArtifactId. Leave the Version as is.
Click Finish.
After a few moments you should see something very similar to the image below in the Package Explorer.
If you don’t have a Console View in your Java Perspective, please add it (Window/Show View/Console). Choose Maven Console.
Let’s stay focused. The task is to create a basic Tapestry 5 skeleton application and run it. Let’s do that!
Select Run Configurations… from the Run Menu Command.
This will open up the Run Configurations dialog. Locate and select Maven Build and then press the New button.
Now fill in the missing blanks in the next dialog. Name this Run Configuration with something meaningful, like Run Traffic School. The fill in the Base Directory by clicking the Browse Workspace button and selecting the school project. Type jetty:run for the Goal input field. Then click Run and wait for Maven to download all the necessary artifacts. This can take some time the first time, but the next time the Jetty will launch very quickly.
If all goes well, you should see something like this in the Maven Console:
Open your browser and type http://localhost:8080/school
You should see
You can click the refresh link and see the time changes – just to prove it works. You can stop the Jetty server by clicking on the red square in the Maven Console. This ends this task.
You learned how to create a new Tapestry 5 skeleton project by running New Maven Project wizard and how to run the Tapestry 5 web application by creating a maven run configuration.
Alternative approaches
You can create a Tapestry 5 skeleton project by running mvn archetype:create -DarchetypeGroupId=org.apache.tapestry -DarchetypeArtifactId=quickstart -DgroupId=org.driving -DartifactId=school -DpackageName=org.driving.school
You can run the application with
mvn jetty:run or with mvn tomcat:run
You can also run the web application with WTP (Web Tools Project) by right clicking on a project and selecting Run As / Run on Server.
If you haven’t defined a server yet or even not downloaded one, then you have some more work to do. Choose the server (Tomcat 6.0 for example).
In the next step choose the directory of the Tomcat you have downloaded and unzipped to.
After clicking on Next, a dialog appears which shows which projects can be run on this Tomcat server.
Our project is already configured, so just click Finish. The Tapestry 5 project will be launched and first page displayed in the embedded browser inside Eclipse.
Running with Tomcat with WTP causes the Tomcat container to reload the context (by default) on any file change in the context path. This is sometimes not desirable as it hides the jewel of Tapestry – live class reloading. To be discussed in the next task.
This is by no means the only possible stack. You can develop Tapestry 5 web applications with any IDE you can think of, even plain notepad if you wish. Of course with Java editors and refactoring capabilities of modern IDEs such as NetBeans, IDEA and Eclipse raises your productivity to a next level.
Also, Maven is not mandatory, you can use any build tool you wish.
Hopefully, as time permits, I will post Tapestry 5 releated stuff in this blog. I will follow my moto as in this latin proverb: "By learning you will teach, by teaching you will learn.".