SudokuServer: A Sample Web Application
The sample application I present is a minimalist MVC web application that consists of a simple model component (SudokuSolver) and a view/controller web component (SudokuServer). The application serves a hard-coded Sudoku puzzle and allows the user to check for partial solutions. I will not dwell on the implementation much since there is nothing spectacular there, it just serves a as vehicle to demonstrate Maven.
I used Spring the web/infrastructure framework for the web. The spring-webmvc module provides various features such dispatcher servlet handler mappings, view resolution, JSP tag lib, and the like. It is very configurable and flexible. There is no connection or any support between Maven and Spring. Any web framework would do for this application.
The application components include:
- SudokuSolver. Contains the Board class. This is a POJO that wraps a Sudoku board. It is used to serve the initial riddle to the user as well as to carry back the user's partial/full solution. The Board can check itself and determine if it is complete, partial or contains an error.
- SudokuServer: Contains the SudokuServerController and SudokuServerValidator. These classes control the workflow of requests and determine the response sent back to the user (via jsp+css). There is no significant JavaScript and it's definitely not an AJAX application. (There. I did it. Now, a search for AJAX will turn up this article.)
The directory structure is the standard Maven directory structure. The web component (SudokuServer) has some web-specific sub directories like WEB-INF and META-INF. The model component (SudokuSolver) has a test sub-directory. Listing Four contains the exact directory layout with all the input files. These files include: Java source files, various XML descriptors, JSP templates and Maven's build files. It seems like a lot of structure and metadata for such a simple project and it really is. However, keep in mind that the same structure is supposed to support huge large-scale projects with hundreds or thousands of modules.
SudokuServer-o | src-o | | | main-o | | | java-o | | | | | com-o | | | | | GigiZone-o | | | | | SudokuServer-o | | | | | SudokuServerController.java | | SudokuServerValidator.java | | | webapp-o | | | META-INF-o | | | | | context.xml | | | WEB-INF-o | | | | | jspf-o | | | | | | | include.jspf | | | sudoku.jspf | | | | | SudokuServer-servlet.xml | | spring.tld | | web.xml | | | index.jsp | sudoku.css | pom.xml SudokuSolver-o | src-o | | | main-o | | | | | java-o | | | | | com-o | | | | | GigiZone-o | | | | | Sudoko-o | | | | | Board.java | | | test-o | | | java-o | | | com-o | | | GigiZone-o | | | Sudoku-o | | | Test-o | | | BoardTest.java | pom.xml pom.xml
The interesting thing about the build files is that they don't reflect the complexity of the directory layout. The build files contain exactly the information necessary to build the project, which is more or less identify the artifact, configure the build with various plugins and state the dependencies of each module. This is a nice demonstration of the "convention over configuration" approach. The only thing I would add is for the test project to depend by default on junit since it is so prevalent and the de-facto standard for Java unit testing.
Building, Deploying, and Running the SudokuServer
SudokuServer requires Apache Tomcat be installed on your system. Building and deploying is as simple as typing 'mvn deploy' at the command line. All the preparation work on the build files now pays off. Maven will compile the sources, package the various artifacts in jars, run the tests and finally will deploy it into Tomcat. If you wish to extend this project with additional modules or more files in existing modules the build infrastructure is ready for you. Running is as simple as pointing your browser to http://localhost:8080/SudokuServer-1.0-SNAPSHOT/. SudokuServer always serves the same hard-coded riddle at the moment for simplicity and ease of testing. Future versions will generate new riddles automatically and/or scrape the web for new ones. Have fun.
DDJ