Hello, World with Spring MVC on Tomcat with IntelliJ

IntelliJ provides checkbox Spring MVC project creation and easy to configure deployment to Tomcat (with one caveat).

Download and unpack Tomcat from http://tomcat.apache.org/index.html and put it wherever you keep your servers in your filesystem.

To begin select the Spring heading in the lefthand panel and check the box for Spring MVC. I’ll be using WebFlow later so I’ve checked that one too.

setup

Go to next and name your project and click Finish and the dependencies will be downloaded to the lib directory.

name

IntelliJ will create the project structure including an index.jsp file in the web folder.

structure

To deploy the project you will need to do 2 things:

  1. Set up a run configuration
  2. Add the dependencies to the build artefact

Set up the run configuration

Go to the Run menu and click Run…

run

and choose Edit Configurations…

edit_conf

In the dialog click the + sign at the top of the left hand pane and in the drop down scroll to the Tomcat entry and click the Local sub menu.

conf

(Note: if Tomcat Server isn’t in the list, scroll to the end and click the more items…)

Choose a name for your server (I’ve called mine Tomcat) and then click the Configure button at the end of the Application server: line.

.server_name

You’ll be presented with a dialog to fill out – browse to where you put your Tomcat download from earlier and select the top level directory from the download and click OK.

conf_empty conf_complete

Once you have the application server configured, select the Deployment tab and click the + sign at the bottom of the artifact box.

deploy

In the context menu choose Artefact… and the default exploded war file should be added to the deployment list.

exploded

This should finish the Run Configuration so click Apply to store the changes and then close the dialog. It won’t deploy successfully until you’ve done the next section.

Add the dependencies to the artefact

To Run the project in Tomcat you’ll need to add in the dependencies to the artefact so open the Project Structure dialog from the menu as shown below or with the keyboard shortcut (Cmd + 😉

project_structure

In the dialog, go to the Artefacts heading in the panel on the left and then find the Fix button in the bottom right hand corner. Fix each problem (there should be 2 or 3 depending on what dependencies you selected in the first screen) by adding the missing library to the artefact. Click OK when all the problems have been fixed.

fix

Once you have fixed all the missing dependencies then you should be able to deploy the artefact directly to your Tomcat server, with the IDE starting the server on first run and then giving you the option of restarting or updating on subsequent runs.

To run go to the Run menu, click the Run… item and then choose the Tomcat run you setup earlier.

runtomcat

You’ll see the default index.jsp file loaded to your browser:

deployed_file

For subsequent runs use the button on the menu bar.

run_shortcut

 

 

Leave a Reply

Your email address will not be published / Required fields are marked *