Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

In this tutorial we will create a simple web application using Spring MVC, Hibernate and JSON. We will use Maven to manage dependencies and IntelliJ IDEA to create, run and debug an application on local Tomcat application server.

...

Wiki Markup
{redirect:https://www.jetbrains.com/idea/help/spring.html

...

|delay

...

=0}

Note
iconfalse

Make sure that Spring, Maven and Tomcat plugins are enabled in IntelliJ IDEA Ultimate before you perform this tutorial.

1. Create a project

Open

Project Wizard
span
classshortcut
Wiki Markup
{span:class=shortcut}Project Wizard{span}
and select
span
classid
Spring MVC
in Spring section
span
classid
Wiki Markup
{span:class=id}Spring MVC{span}
in
Wiki Markup
{span:class=id}Spring section{span}
. If you have already configured the application server, you can select it in the Application Server
span
classid
Wiki Markup
{span:class=id}Application Server{span}
field. With IntelliJ IDEA you can deploy applications to Tomcat, TomEE, Glassfish, JBoss, WebSphere, Jetty, Geronimo, Resin, Cloud Foundry and CloudBees.

Change

span
classid
Project name
,
span
classid
Project location
and Base package
span
classid
Wiki Markup
{span:class=id}Project name{span}
,
Wiki Markup
{span:class=id}Project location{span}
and
Wiki Markup
{span:class=id}Base package{span}
if necessary. The IDE will create a "Hello world"
span
classid
Wiki Markup
{span:class=id}"Hello world"{span}
project with a simple controller and view.

...

The new project comes with Maven's

span
classid
Wiki Markup
{span:class=id}pom.xml{span}
file. You can manage project dependencies through this file or through the dedicated Maven
span
classshortcut
Wiki Markup
{span:class=shortcut}Maven{span}
tool window.

When you change Maven's dependencies, IntelliJ IDEA applies the corresponding changes to the project automatically. You can check it in the

Project Structure → Modules
span
classshortcut
Wiki Markup
{span:class=shortcut}Project Structure → Modules{span}
dialog.

Besides dependencies, IntelliJ IDEA also imports the artifacts definition from

span
classid
Wiki Markup
{span:class=id}pom.xml{span}
. You can check the artifacts settings in the
Project Structure → Artifacts
span
classshortcut
Wiki Markup
{span:class=shortcut}Project Structure → Artifacts{span}
dialog.

The artifacts define the structure of what will be deployed to the application server when you click

Run → Run 'Tomcat
span
classshortcut
Wiki Markup
{span:class=shortcut}Run → Run 'Tomcat 7.0'{span}
.

2. Create run configuration

If you haven't specified

span
classid
Application server
in Project Wizard
span
classshortcut
Wiki Markup
{span:class=id}Application server{span}
in
Wiki Markup
{span:class=shortcut}Project Wizard{span}
you can do it now via Run → Edit
span
classshortcut
Wiki Markup
{span:class=shortcut}Run → Edit Configurations...{span}
.

Don't forget to specify the artifacts to deploy for this run configuration via the

Deployment
span
classshortcut
Wiki Markup
{span:class=shortcut}Deployment{span}
tab.

If you have configured at least one run configuration for an application server, IntelliJ IDEA shows the

Application Servers
span
classshortcut
Wiki Markup
{span:class=shortcut}Application Servers{span}
tool window to manage the application state on the application server. You can see the list of application servers, start or stop servers, see deployed applications, manage artifacts to deploy, and manage the application state.

...

After the artifacts and run configurations are defined, you can deploy the application by simply running your configuration or via a shortcut right from the

Application Servers
span
classshortcut
Wiki Markup
{span:class=shortcut}Application Servers{span}
tool window.

4. Add dependencies

...

We have to define all these dependencies in our

span
classid
Wiki Markup
{span:class=id}pom.xml{span}
file. The IDE will automatically download the corresponding libraries and add to the artifact.

...

5. Create persistence.xml

Now let's define

span
classid
Wiki Markup
{span:class=id}resources/META-INF/persistence.xml{span}
file to initialize Hibernate's entity manager over JPA.

...

Now we have to register the user repository, an entity manager factory and a transaction manager in

span
classid
Wiki Markup
{span:class=id}webapp/WEB-INF/mvc-dispatcher-servlet.xml{span}
file.

Code Block
langxml
<jpa:repositories base-package="com.springapp.mvc"/>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="defaultPersistenceUnit"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

...

8. Define controller

Let's rename

span
classid
HelloController
to UserController
span
classid
Wiki Markup
{span:class=id}HelloController{span}
to
Wiki Markup
{span:class=id}UserController{span}
and add the following code:

...

9. Define view

Let's rename

hello
span
classid
Wiki Markup
{span:class=id}hello{span}
view (and corresponding
span
classid
Wiki Markup
{span:class=id}hello.jsp{span}
file) to users
span
classid
Wiki Markup
{span:class=id}users{span}
(and
span
classid
Wiki Markup
{span:class=id}users.jsp{span}
, respectively). If you rename the view name from usage, IntelliJ IDEA applies the corresponding changes to JSP files automatically.

...

If you need to debug your application, just add a breakpoint and re-run the application in debug mode via

Run → Debug 'Tomcat
span
classshortcut
Wiki Markup
{span:class=shortcut}Run → Debug 'Tomcat 7.0'...{span}
.

12. Add JSON API

Finally, let's output the created users via JSON by implementing this simple Controller's method:

...