Friday, January 20, 2012

Spring MVC 3.1 - Implement CRUD with Spring Data MongoDB (Part 6)

Review

We have just completed our application! In the previous sections, we have discussed the functional specs, created the Java classes, declared the configuration files, and wrote the HTMl files. In this section, we will build and run the application using Maven, and show how to import the project in Eclipse.


Running the Application

Access the source code

To download the source code, please visit the project's Github repository (click here)

Preparing the data source

  1. Run MongoDB (see Part 2 for instructions)
  2. There's no need to create any collections because Spring will create them automatically
  3. There's no need to populate the database with sample data because our InitMongoService will insert our sample data automatically

Building with Maven

  1. Ensure Maven is installed
  2. Open a command window (Windows) or a terminal (Linux/Mac)
  3. Run the following command:
    mvn tomcat:run
  4. You should see the following output:
    [INFO] Scanning for projects...
    [INFO] Searching repository for plugin with prefix: 'tomcat'.
    [INFO] artifact org.codehaus.mojo:tomcat-maven-plugin: checking for updates from central
    [INFO] artifact org.codehaus.mojo:tomcat-maven-plugin: checking for updates from snapshots
    [INFO] ------------------------------------------
    [INFO] Building spring-mongodb-tutorial Maven Webapp
    [INFO]    task-segment: [tomcat:run]
    [INFO] ------------------------------------------
    [INFO] Preparing tomcat:run
    [INFO] [apt:process {execution: default}]
    [INFO] [resources:resources {execution: default-resources}]
    [INFO] [tomcat:run {execution: default-cli}]
    [INFO] Running war on http://localhost:8080/spring-mongodb-tutorial
    Jan 20, 2012 8:45:19 PM org.apache.catalina.startup.Embedded start
    INFO: Starting tomcat server
    Jan 20, 2012 8:45:19 PM org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
    Jan 20, 2012 8:45:19 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    Jan 20, 2012 8:45:22 PM org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-8080
    Jan 20, 2012 8:45:22 PM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    
  5. Note: If the project will not build due to missing repositories, please enable the repositories section in the pom.xml!

Access the Entry page

  1. Follow the steps with Building with Maven
  2. Open a browser
  3. Enter the following URL (8080 is the default port for Tomcat):
    http://localhost:8080/spring-mongodb-tutorial/

Import the project in Eclipse

  1. Ensure Maven is installed
  2. Open a command window (Windows) or a terminal (Linux/Mac)
  3. Run the following command:
    mvn eclipse:eclipse -Dwtpversion=2.0
  4. You should see the following output:
    [INFO] Scanning for projects...
    [INFO] Searching repository for plugin with prefix: 'eclipse'.
    [INFO] org.apache.maven.plugins: checking for updates from central
    [INFO] org.apache.maven.plugins: checking for updates from snapshots
    [INFO] org.codehaus.mojo: checking for updates from central
    [INFO] org.codehaus.mojo: checking for updates from snapshots
    [INFO] artifact org.apache.maven.plugins:maven-eclipse-plugin: checking for updates from central
    [INFO] artifact org.apache.maven.plugins:maven-eclipse-plugin: checking for updates from snapshots
    [INFO] -----------------------------------------
    [INFO] Building spring-mongodb-tutorial Maven Webapp
    [INFO]    task-segment: [eclipse:eclipse]
    [INFO] -----------------------------------------
    [INFO] Preparing eclipse:eclipse
    [INFO] No goals needed for project - skipping
    [INFO] [eclipse:eclipse {execution: default-cli}]
    [INFO] Adding support for WTP version 2.0.
    [INFO] -----------------------------------------
    [INFO] BUILD SUCCESSFUL
    [INFO] -----------------------------------------
    
    This command will add the following files to your project:
    .classpath
    .project
    .settings
    target
    You may have to enable "show hidden files" in your file explorer to view them
  5. Open Eclipse and import the project

Conclusion

That's it! We've have successfully completed our Spring MVC 3.1 web application. We've learned how to setup MongoDB and access it through Spring Data MongoDB. Furthermore, we used AJAX to make the application responsive.

I hope you've enjoyed this tutorial. Don't forget to check my other tutorials at the Tutorials section.

Revision History


Revision Date Description
1 Jan 20 2012 Uploaded tutorial and Github repository
2 Jan 22 2012 Updated description of InitMongoService
3 Jan 23 2012 Added class, use case, and activity diagrams

StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring MVC 3.1 - Implement CRUD with Spring Data MongoDB (Part 6) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

33 comments:

  1. Thank you for this tutorial! I will try this. I'm always learn new things because of your blogs. More Power!

    ReplyDelete
  2. Thanks. I always like your tutorials for the following reasons.
    1) good explanation
    2) source code available either on googlecode or github
    3) usage of latest versions and new tools

    ReplyDelete
  3. Thanks also for the tutorial.
    Adding or editing users isn't working for me however.
    I wanted to get the project running on Eclipse to do some debugging but it is not seen as a web app. Do you know why and how to fix it?

    ReplyDelete
    Replies
    1. I have the same bug, Any new??

      Delete
  4. @Joe, did you import the project using the mvn eclipse:eclipse -Dwtpversion=2.0 command?

    Kindly open your browser's debug option to see if the AJAX requests are being sent. Also, if the request is successfully sent, you should see it on the Eclipse console.

    ReplyDelete
  5. I can't read English well. But I read your tutorial , i knew my problems, thanks you so much!

    ReplyDelete
  6. Hi Krams,
    Thanks for the great tutorial. May I know what changes needed to be made if I want a xml response instead of json ?

    ReplyDelete
  7. @Anonymous, if my memory serves me right, you don't need to change anything in the web application. You just need the client application to have an XML content-type request. In this tutorial, the HTML page acts as the client and uses JSON for the content-type.

    ReplyDelete
    Replies
    1. Hi Krams,
      Thanks for the prompt reply, I tried to capture the ajax, the request header's content-type showed "application/x-www-form-urlencoded" instead of "application/json".

      Best Regards,
      John

      Delete
    2. Hi Krams,
      After some googling, I've found the solution, annotate the domain object with XmlRootElement will get a xml response.

      Best Regards,
      John

      Delete
    3. Tried it, program worked somewhat, nothing into database, disappointing.

      Steve

      Delete
  8. I have encountered the same problem as "Steve" (Anonymous) above. The program basically works and displays, but nothing is written to the database. All changes are apparently discarded. Any assistance would be greatly appreciated, since the tutorial itself is outstanding. I just wish I could get it to work! :-)

    ReplyDelete
    Replies
    1. When you restart the application, it will delete the existing data and re-initialize them with the default records. If you want to prevent this behavior, you must uncomment the InitMongoService.

      Delete
  9. Thanks for the tutorial. The code looks great and it's really helpful.

    ReplyDelete
  10. Great tutorial! However, for me to get it to work, I had to add an "event" parameter to the $('#newForm').submit and $('#editForm').submit functions in users.jsp.

    ReplyDelete
  11. the add/edit aren't working. I've tried to comment/uncomment the InitMongoService. I know when I restart the server it will delete all data, however only the delete function works. Any help??
    Nevertheless exc tutorials, thank you.

    ReplyDelete
    Replies
    1. Can you check in your browser, i.e using Google Developer Tools, the JSON response from the server? Also, check the logs (enable them if needed) if the call to add and edit are being passed to the service layer.

      Delete
    2. FIXED. I just moved the id='newForm' to the form tag instead to use it in dvd tag. then I just commented this line event.preventDefault();. I don't know why that line doesn't allow to call the submitNewRecord/submitUpdateRecord function.

      Delete
  12. Fast, clean, informative, and it worked the first time i tried it!

    ReplyDelete
  13. in user.jsp - change as following to make it work in mozilla

    $('#newForm').submit(function(event) {
    event.preventDefault();
    submitNewRecord();
    });

    $('#editForm').submit(function(event) {
    event.preventDefault();
    submitUpdateRecord();
    });

    ReplyDelete
  14. First of all thank you for the great tutorials.

    Now the other day I was also following your tutorial about Spring Security 3.1 what works perfectly but can someone send me in the right direction for implementing this tutorial with Spring Security 3.1?
    Doesn't really mather to my wich database to use SQL / Mongodb.

    Thx!

    ReplyDelete
  15. thanks for the tutorial, it was very informative and helped me in understanding the spring framework, thanks!

    ReplyDelete
  16. Are you creating the user and role collection manually from the command prompt. And also please tell that what is the use of the annotation mapping on pojo classes as you are using directly the collection name in the mongotemplate.insert method ?

    ReplyDelete
  17. Thanks you, very good tutorial

    ReplyDelete
  18. Thanks for your great tutorial.At first when I imported it directly it keeps giving me error that it cannot read spring-tx and spring-jdbc zip file in .m2(maven repository).Then I installed maven in windows and try running the command mvn tomcat:run still in the project folder directory,it was giving the same error cannot open spring-tx and spring-jdbc zip file.
    I google it and found the below link
    http://stackoverflow.com/questions/7600028/maven-error-in-opening-zip-file-when-running-maven

    So I deleted the spring-tx and spring-jdbc folder in .m2 repository,mine was user/a/.m2 and ran the mvn tomcat:run again this it only showed error in spring-jdbc so I again deleted the spring-jdbc folder (.m2/repository/org/springframework/ and thankfully it built successfully.
    Just wanted share this with you so if anyone else has this odd problem they can fix it.

    thanks again for such a nice tutorial

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. This is really a good tutorial, thanks for clear and detailed explaining :)Great !

    ReplyDelete
  21. Thanks for the tutorial. It works great. Now I am going to play a little with it.

    ReplyDelete
  22. Hi, I got across with the tutorial and it looks good. How can I download the code from github? help appreciated.

    ReplyDelete