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.
Go to next and name your project and click Finish and the dependencies will be downloaded to the lib directory.
IntelliJ will create the project structure including an index.jsp file in the web folder.
To deploy the project you will need to do 2 things:
- Set up a run configuration
- Add the dependencies to the build artefact
Set up the run configuration
Go to the Run menu and click Run…
and choose Edit Configurations…
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.
(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.
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.
Once you have the application server configured, select the Deployment tab and click the + sign at the bottom of the artifact box.
In the context menu choose Artefact… and the default exploded war file should be added to the deployment list.
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 + 😉
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.
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.
You’ll see the default index.jsp file loaded to your browser:
For subsequent runs use the button on the menu bar.