Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

Maven: Building Complex Systems


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
Listing Four: Directory layout.

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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.