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.Table of Contents
Part 1: Introduction and Functional SpecsPart 2: MongoDB setup
Part 3: Java classes
Part 4: XML configuration
Part 5: HTML Files (with AJAX)
Part 6: Running the Application
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
- Run MongoDB (see Part 2 for instructions)
- There's no need to create any collections because Spring will create them automatically
- There's no need to populate the database with sample data because our InitMongoService will insert our sample data automatically
Building with Maven
- Ensure Maven is installed
- Open a command window (Windows) or a terminal (Linux/Mac)
- Run the following command:
mvn tomcat:run
- 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
Note: If the project will not build due to missing repositories, please enable the repositories section in the pom.xml!
Access the Entry page
- Follow the steps with Building with Maven
- Open a browser
- Enter the following URL (8080 is the default port for Tomcat):
http://localhost:8080/spring-mongodb-tutorial/
Import the project in Eclipse
- Ensure Maven is installed
- Open a command window (Windows) or a terminal (Linux/Mac)
- Run the following command:
mvn eclipse:eclipse -Dwtpversion=2.0
- 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
- 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 |
Share the joy:
|
Subscribe by reader Subscribe by email Share
Thank you for this tutorial! I will try this. I'm always learn new things because of your blogs. More Power!
ReplyDeleteThanks. I always like your tutorials for the following reasons.
ReplyDelete1) good explanation
2) source code available either on googlecode or github
3) usage of latest versions and new tools
Thanks also for the tutorial.
ReplyDeleteAdding 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?
I have the same bug, Any new??
Delete@Joe, did you import the project using the mvn eclipse:eclipse -Dwtpversion=2.0 command?
ReplyDeleteKindly 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.
I can't read English well. But I read your tutorial , i knew my problems, thanks you so much!
ReplyDeleteNice tutorial. TY
ReplyDeleteHi Krams,
ReplyDeleteThanks for the great tutorial. May I know what changes needed to be made if I want a xml response instead of json ?
@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.
ReplyDeleteHi Krams,
DeleteThanks 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
Hi Krams,
DeleteAfter some googling, I've found the solution, annotate the domain object with XmlRootElement will get a xml response.
Best Regards,
John
Tried it, program worked somewhat, nothing into database, disappointing.
DeleteSteve
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! :-)
ReplyDeleteWhen 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.
DeleteThanks for the tutorial. The code looks great and it's really helpful.
ReplyDeleteGreat 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.
ReplyDeletethe 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??
ReplyDeleteNevertheless exc tutorials, thank you.
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.
DeleteFIXED. 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.
DeleteFast, clean, informative, and it worked the first time i tried it!
ReplyDeletein user.jsp - change as following to make it work in mozilla
ReplyDelete$('#newForm').submit(function(event) {
event.preventDefault();
submitNewRecord();
});
$('#editForm').submit(function(event) {
event.preventDefault();
submitUpdateRecord();
});
First of all thank you for the great tutorials.
ReplyDeleteNow 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!
Fixed previous.
ReplyDeletethanks for the tutorial, it was very informative and helped me in understanding the spring framework, thanks!
ReplyDeleteAre 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 ?
ReplyDeleteThanks you, very good tutorial
ReplyDeleteThanks 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.
ReplyDeleteI 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
This comment has been removed by the author.
ReplyDeleteThis is really a good tutorial, thanks for clear and detailed explaining :)Great !
ReplyDeleteThanks for the tutorial. It works great. Now I am going to play a little with it.
ReplyDeleteHi, I got across with the tutorial and it looks good. How can I download the code from github? help appreciated.
ReplyDeleteThanks... it works!
ReplyDeleteThanks a lot Krams :)
ReplyDelete