Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

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 application on local Tomcat application server.

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 and select Spring MVC in Spring section. If you have already configured application server you can select it in Application Server field. With IntelliJ IDEA you can deploy applications to Tomcat, TomEE, Glassfish, JBoss, WebSphere, Jetty, Geronimo, Resin, Cloud Foundry and CloudBees.

Change Project name, Project location and Base package if necessary. The IDE will create a "Hello world" project with simple controller and view.

The new project comes with Maven's build.xml file. You can manage project dependencies through this file or through the dedicated Maven tool window.

When you change Maven's dependencies IntelliJ IDEA applies corresponding changes to the project automatically. You can check it in Project Structure → Modules dialog.

Just like dependencies IntelliJ IDEA also imports the artifacts definition from build.xml file. You can check the artifacts settings in Project Structure → Artifacts dialog.

The artifacts define the structure of what is going to be deployed to the application server when you click Run.

2. Create run configuration

If you haven't specified Application server in Project Wizard you can do it now via Run → Edit Configurations....

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

If you have configured at least one run configuration for an application server IntelliJ IDEA shows the Application Servers tool window to manage state of application 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 application state.

3. Run the application

When 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 tool window.

4. Add dependencies

Since we are going to create a database for our application, we need Spring Data, Hibernate and HSQLDB libraries. In order to implement JSON API for our application we need JSON library. And finally we will need JSTL library to use in application's view.

We have to add define all these dependencies in our build.xml file. The IDE will automatically download the corresponding libraries and add to the artifact.

5. Create persistence.xml

Now let's define resources/META-INF/persistence.xml file to initialise Hibernate's entity manager over JPA.

6. Define model classes

Define a model class for user entity using JPA annotations.

Define a Spring repository for the user entity.

7. Register repository, entity manager factory and transaction manager

Now we have to register the user repository, an entity manager factory and a transaction manager in webapp/WEB-INF/mvc-dispatcher-servlet.xml file.

The model for our application is ready so we can implement the controller.

8. Define controller

Let's rename HelloController to UserController and add the following code:

As you see we have defined three methods for listing, adding and deletion user entities. The methods are mapped to the corresponding URLs.

9. Define view

Let's rename hello view (and corresponding hello.jsp file) to users (and users.jsp correspondingly). If you rename the view name from usage IntelliJ IDEA applies corresponding changes to JSP files automatically.

10. Run the application

The application should be ready now.

11. Debug application

If you need to debug your application just add a breakpoint and re-run the application in debug mode via Run → Debug....

12. Add JSON API

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

Run the application and open http://localhost:8080/api/users.

  • No labels