Starting with Wicket on IntelliJ IDEA

I’ve changed my job recently and went to another company to do more or less the same I did for 10 years (Java based information systems typically delivered through web browser) – but with completely different stack. I did JBoss Seam with JSF/Facelets, later we switched to GWT (still on top of the Seam), previously we used Struts (many years ago, but really hard to forget ;-)) and among these things we used EJB 2.1 (ouch!) and 3.0 (finally usable).

Now I’m learning Spring and Wicket – and it’s a lot of stuff to learn. Couple of cunning colleagues came up with interesting modeling framework on top of this stack (based on EMF) – so it’s even more stuff to learn now. But here the whole thing does make sense and I’m trying my best to beat the learning curve. And yes, I had to switch from Idea to Eclipse as well (soon rant #2 will follow ;-)).

But to test Wicket hands-on from the basics at home I decided to use Idea. Because one guide is just too old and others are hardly guides, I decided to write this one. It’s pretty simple actually because to start with Wicket is really easy (which is a big compliment to the framework). A few years ago I had to appreciate how JBoss Seam examples were written – they really worked even though the technology stack there is quite crazy. But then you forgot seam.properties in your first Hello World test and went crazy for a few days. Wicket is simpler. Of course, Seam is more than just the web framework – that’s why we used it under GWT although I would probably not use it if I hadn’t known it before really.

But back to the topic. Every recipe has to start with things we need:

  • Wicket 1.5 (or maybe 1.4, but we’re just so eager to use 1.5 that we’re testing that one) (link is for RC1, check if there is newer/more stable release!)
  • IntelliJ IDEA 10.0.2 Community Edition (or whatever similar, full edition, …)
  • Jetty 8.x (I mean, it works with M2 release, but you’ll probably download some stable 7.x :-))
  • Velocity 1.7 (needed for Wicket, and not bundled as Wicket kinda expects you to use Maven)
  • SLF4J 1.6.1 (the same reason)
  • Idea plugin WicketForge (install it directly from Idea’s available plugins)

Unzip Wicket, SLF4J, Velocit and Jetty to your lib directory, create libs for them in Idea (or one lib with all relevant JARs – your choice) and add these libs to your project. I have one testing project for various trials and experiments so I just added these libraries there. You can’t go wrong if you add all JARs from Wicket’s lib directory, Jetty’s lib directory, from SLF4J just add slf4j-api-1.6.1.jar and slf4j-jdk14-1.6.1.jar and finally velocity-1.7-dep.jar.

Technically we will just copy and paste this Hello World example. You can find nearly the same example in Wicket’s directory
...\src\wicket-examples\src\main\java\org\apache\wicket\examples\helloworld, but this one’s classes are derived from some common Example application classes. That said – you can go and inspire yourself in that directory, but when you drag classes from there you have to change their extends (at least for HelloWorld nothing more was required).

When you write it all, you have to build it, pack it or somehow prepare extended war and start your favourite server (as said on the example page), but we don’t want that. That’s where Jetty comes in. First check if you can compile it all. If yes, add this main method into the application class (actually it can be any other class, but why? :-)):

public static void main(String[] args) throws Exception {
      Server server = new Server();
      Connector connector = new SelectChannelConnector();
      connector.setPort(8080);
      connector.setHost("127.0.0.1");
      server.addConnector(connector);

      WebAppContext wac = new WebAppContext();
      wac.setContextPath("/");
      wac.setWar(".");
//    wac.setParentLoaderPriority(true);
      server.setHandler(wac);
      server.setStopAtShutdown(true);

      server.start();
}

Now you should have project that fits on one screen and looks like this:

Now compile the package (or the whole project, whatever suits you) and run the class with main method. Setup the runner so that the working directory points to the helloworld package (or fix the path to your WAR in code – up to you). You should have no problem with classes – but if you do, try to uncomment that line with setParentLoaderPriority. I think it saved my life on a different project, but funny enough – it doesn’t seem to do anything (be it true or false) in this case. It is somehow documented here – but it’s quite vague if you ask me.

But you will probably face another problem related to Velocity:

Caused by: org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'VM_global_library.vm'

While it’s easy to find many references where they say it’s rather no problem, if you go to http://localhost:8080 (program keeps running) you will probably get HTTP 503. I’m no Wicket/Velocity expert and I think it’s quite a shame this resource is wanted so hard – yet it’s not in the JAR or whenever. But you can copy it from Velocity’s ...\test\templates\VM_global_library.vm to your default package and “compile” it from there. To be able to “compile” it, go to Settings (Ctrl+Alt+S), Compiler and add “;?*.vm” (without quotes) at the end of Resource patterns.

Alternatively you just copy the VM file to the output directory yourself – find out where it is via Project Structure (Ctrl+Alt+Shift+S), Project, Project compiler output. Under that out directory you have to go on to production\all and there are classes from the default package – and that’s where should the VM file go too.

Now you should be ready to run the program again (Shift+F10 if you didn’t change the settings) and this time you should get Hello World!

OK – I know it’s nothing great and you can get there with Maven much faster, but right now you have Idea ready for more Wicket testing, you can extend your application and you don’t rely on Ant or Maven – just compile and run. And if you successfully installed WicketForge you have also basic tool support for switching between HTML template and component class (Alt+Shift+W) or if you are on the component ID, just press F4 (quite naturally). Orange colour in this case is good. It’s not red. 🙂

Happy Wicket testing to you all. Comment if you feel so and if you have anything to add. I’m still just a Wicket newbie and maybe you can shed some light on that Velocity problem or explain Jetty classloading better. Cheers.

I nearly forgot! Download my Idea project here – but you still have to setup those libraries. After that it’s just Shift+F10.

Advertisements

About virgo47
Java Developer by profession in the first place. Gamer and amateur musician. And father too. Naive believer in brighter future. Step by step.

One Response to Starting with Wicket on IntelliJ IDEA

  1. ji says:

    thank you, it solved my ‘VM_global_library.vm’ problem

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s