JVC - Java View Controller

What is JVC?

JVC is a tool for creating Java web applications.

Most well designed web applications separate their components into models, views and controllers. JVC creates the view and controller part of your web application (you are free to use any model implementation you like).

JVC is simple and the web applications it creates are fast because the requests are processed by pre-compiled Java code. For even better performance we recommend configuring your web server so that the static content is handled by an Apache server instead of by your JVC app.

Writing a JVC web app is simple because you just write POJO's and you don't need any xml configuration files.

JVC uses standard open source tools like Tomcat, Ant and Log4j.

JVC applications share many similarities with Ruby on Rails applications, but since JVC apps are written in Java many errors can be caught while compiling instead of during runtime.

What does JVC do?

  1. Creates web application project skeletons (see Hello JVC section)
  2. Converts view templates into page generator and controller classes (as part of the build process)
  3. Dispatches page requests to the appropriate page generator and controller and returns their response (at run time).

What do you do?

View Templates

View templates are just regular html files that can also contain JVC tags. There are only 5 JVC tag types:

[[ ... ]]Java-code-tag These tags contain Java code which will be executed as part of the page generation process. This code has access to the JVCRequestContext via the variable rc and the companion Controller object via the variable controller.
[[= ... ]]Java-expression-tag These tags contain Java code which evaluates to a String. They have access to the same variables as the Java-code-tags.
[[== ... ]]HTML escaped Java-expression-tag These tags are exactly the same as Java-expression-tags except their output is HTML escaped.
[[+ ... ]]Include-tag These tags contain a path to a file to be included. The included file's name must begin with an underscore character (ie. _myfile.jhtml) to distinguish it from a regular view template.
[[! ... ]]Comment-tag Anything inside of comment-tags is ignored (including other JVC tags).

And one meta tag pair:

[[begin_cache]] ... [[end_cache]]Cache-block Anything inside a cache-block will only be evaluated once, subsequent page requests will use the cached result.

During the build process view templates get turned into page generators and controllers.

Page Generators

A page generator is an automatically generated Java class corresponding to a view template. The JVCDispatcher calls the appropriate page generator when it receives a request for a dynamic web page. You can think of a page generator as a view template turned inside out. The inside of the Java-code-tags in the template becomes the page generator method code and the html outside of the tags becomes the page generator output. The contents of Java-expression-tags are converted to Strings and also become part of the page generator output. The comment-tags are discarded. The files in the include-tags are loaded and processed just like the rest of the view template.

You can look at the page generator source code, but don't modify it (your changes will be overwritten the next time the project is rebuilt). The page generators for the view templates in the view directory become corresponding Java classes located in the src/<package_prefix>/generators/ directory (you specified the package_prefix when you create your project or it defaults to no package, see Hello JVC section). Note: if you build your project with the -Dline.numbers=true command line argument then the each line of the page generator code will contain a comment indicating which part of which template it came from.

Controllers

Each view template also spawns a corresponding page controller class. The page controller is located in a directory under src/<package prefix>/controllers/ mirroring the path to the view template relative to the view/ directory.

All the page controllers corresponding to the view templates in a particular directory extend a 'local' base controller, and all these 'local' base controllers in turn extend a single 'global' base controller. See the Hello JVC section for an example.

The generated controller classes don't do anything other than provide access to the JVCRequestContext, but they give you a place to wire in your web application code while keeping it separate from your views. Page specific code can be added to in the page controllers and common code can be put in the 'local' or 'global' base controllers.

If you look at the source code for a PageGenerator.java class you will see that on the first line of each page generator method the corresponding page controller object is instantiated and stored in the variable controller. This is how your page view and controller can interact.

Request Dispatcher and JVCRequestContext

The request dispatcher's job is examine the path part of each request, invoke the appropriate page generator (and controller) and return the result. It also serves static resources too, if necessary.

In the Tomcat server the JVC request dispatcher is a simple servlet.

The only JVC class you, as a developer, really need to know about is the JVCRequestContext. When the request dispatcher receives a request it decides if it is for static or dynamic content. If its for dynamic content then it creates a JVCRequestContext and instantiates the appropriate controller and PageGenerator and passes them the JVCRequestContext.

For more details please see the javadocs

Hello JVC

Once you've installed installed JVC and its dependencies (Java, Tomcat and Ant) you can create, build, test and install a new web application in seconds.

Lets assume you installed JVC in your ~/dev/jvc/ directory then you can create a hello_jvc app by typing:

~/dev/jvc$ ant new_project -Dname=hello_jvc
~/dev/jvc$ cd ../hello_jvc
~/dev/hello_jvc$ ant test # run unit tests (optional)
~/dev/hello_jvc$ $CATALINA_HOME/bin/shutdown.sh -force
~/dev/hello_jvc$ ant install
~/dev/hello_jvc$ $CATALINA_HOME/bin/startup.sh

or instead of the last 3 commands you can just type:

~/dev/hello_jvc$ ant rev

At this point you should be able to go to http://localhost:8080/hello_jvc and see your new web app (assuming your Tomcat server is in its default configuration).
(Note: you may have to start/stop Tomcat and install your app as root if you installed Tomcat as root).

Now lets look at what was created.

hello_jvc/
  build.xml
  conf/
    ...
  dist/
    ...
  docs/
    ...
  lib/
    ...
  src/
    Application.java
    controllers/
      BaseController.java
      docs/
        BaseDocsController.java
        examples/
          BaseExamplesController.java
          FormTestController.java
          RequestController.java
          BouncerController.java
          RedirectController.java
          TagController.java
      ...
    generators/
      BaseGenerator.java
      docs/
        examples/
          BouncerGenerator.java
          FormTestGenerator.java
          RedirectGenerator.java
          RequestGenerator.java
          TagGenerator.java
      ...
    test/
      ...
    utils/
      Helpers.java
  view/
    docs/
      examples/
        bouncer.jhtml
        formTest.jhtml
        redirect.jhtml
        _request.jhtml
        request.jhtml
        tag.jhtml
      ...
    shared/
      ...
    static/
      ...
    tests/
      ...

Notice how, for example, the tag.jhtml view template spawns the TagGenerator.java page generator and the TagController.java, BaseExamplesController.java and BaseController.java controllers.

Use the -Dpkg.prefix option when creating your application to cause the generated code to be put into your own java package. For example: if you are creating a web app called my_app and want your code to be in the package com.acme then type: ant new_project -Dname=my_app -Dpkg.prefix=com.acme

Tips and Tricks

Installation

  1. Download and un-tar Java (a JDK not just a JRE)
  2. Set the JAVA_HOME environment variable
  3. Add $JAVA_HOME/bin to your PATH
  4. to test type: java -version

  5. Download and un-tar Tomcat
  6. Set the CATALINA_HOME environment variable
  7. Set the CATALINA_PID environment variable to something like $CATALINA_HOME/catalina.pid (otherwise 'shutdown.sh -force' don't work correctly)
  8. to test type: $CATALINA_HOME/bin/startup.sh and go to http://localhost:8080/ you should see the Tomcat start page.

  9. Download and un-tar Ant
  10. Add ant to your PATH
  11. to test type: ant -version

  12. Download and un-tar JVC from sourceforge.net
  13. to test create a 'hello jvc' web app

SourceForge.net Logo