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
Dependencies
- Spring core 3.1.0.RELEASE
- Spring Data MongoDB 1.0.0.RELEASE
- MongoDB (server) 2.0.2
- MongoDB (Java driver) 2.7.2
- See pom.xml for details
Github
To access the source code, please visit the project's Github repository (click here)
Functional Specs
Before we start, let's define our application's specs as follows:- A CRUD page for managing users
- Use AJAX to avoid page refresh
- Users have roles. They are either admin or regular (default)
- Everyone can create new users and edit existing ones
- When editing, users can only edit first name, last name, and role fields
- A username is assumed to be unique
Here's our Use Case diagram:
Database
If you're new to MongoDB and coming from a SQL-background, please take some time to read the following resource SQL to Mongo Mapping Chart. It's a comparison between SQL and MongoDB.We have two collections (tables in SQL): user and role. The user collection contains personal information of each user; whereas the role collection contains role values for each user where a value of 1 represents an admin and a value of 2 represents a regular user.
Here's the Class diagram:
User document
Below is the JSON structure of the User document after Spring Data MongoDB has created the collection:{
"_id" : "" ,
"_class" : "org.krams.domain.User"
"firstName" : ""
"lastName" : ""
"username" : ""
"password" : "" ,
"role" : { "$ref" : "role" , "$id" : "" }
}
Notice the role property is a reference to a Role record as indicated by $ref: role property.Role document
Below is the JSON structure of the Role document after Spring Data MongoDB has created the collection:{
"_id" : ""
"_class" : "org.krams.domain.Role"
"role" : ""
}
Screenshots
Let's preview how the application will look like after it's finished. This is also a good way to clarify further the application's specs.The Activity diagram:
Entry page
The entry page is the primary page that users will see. It contains a table showing user records and four buttons for adding, editing, deleting, and reloading data. All interactions will happen in this page.
Edit existing record
When user clicks the Edit button, an Edit Record form shall appear after the table.
When a user submits the form, a success or failure alert should appear.
When the operation is successful, the update record should reflect on the table.
Create new record
When a user clicks the New button, a Create New Record form shall appear after the table.
When a user submits the form, a success or failure alert should appear.
When the operation is successful, the new record should appear on the table.
Delete record
When user clicks the Delete button, a success or failure alert should appear.
Reload record
When user clicks the Reload button, the data on the table should be reloaded.
Errors
When user clicks the Edit or Delete button without selecting a record first, a "Select a record first!" alert should appear.
Next
In the next section, we will study how to setup a MongoDB server both in Windows and Ubuntu. Click here to proceed.|
Share the joy:
|












You should have a look to my Spring MVC 3.1 - Spring Data - MongoDB XML free example application https://github.com/sdeleuze/spring-backbone-todo
ReplyDelete@Bouiaw, thanks. I'm looking at it right now. It would be great if you can write a blog about this one.
ReplyDelete@Bouiaw, the use of XML-less configuration is great. However, I find it anticlimactic when it comes to the repository. I was expecting you would utilize the Spring Data MongoDB repository support, but instead you used directly the MongoTemplate. Though nothing's wrong with that, I find that part somewhat wanting.
ReplyDeleteQuestion: Do you know if there is any way to prevent Spring from storing the fully qualified classname under _class in the database (example: "_class" : "org.krams.domain.User") when inserting? Or will the mapping stop working by doing this?
ReplyDelete@Anonymous, I have to investigate further if that's possible
ReplyDeleteI was wondering if you have any example integrating MongoDB with Spring Security.
ReplyDeleteSorry, I don't have any yet. Is it possible? I think so. You just have to provide a custom user details service which delegates the data access to MongoDB dao or repository.
DeleteHi,
ReplyDeletegreat example that has helped me a lot. What I cannot find anywhere is how to make a multiple-field search form (search by field1, field2... fieldN if they are given values).
You can search by any combination of any of the fiels (with AND...). I've tried to do it through a "findByField1AndField2AndField3And....". The idea was that if I put "null" in one field, that field would not be used in the search, but Mondo does it, so it cannot be done with this approach.
How, if any combination of fields to find could be created in the form, could it be done? Do I need to create every combination of "findBy...." that are possible to be searched for?
Thanks in advance :)
This comment has been removed by the author.
ReplyDeleteHi, nice work.
ReplyDeleteI noticed that the new/create functionality added a new Role for each new user instead of referencing an existing role.
I fixed this by
1) removing the role save in UserService.create:
public User create(User user) {
user.setId(UUID.randomUUID().toString());
// user.getRole().setId(UUID.randomUUID().toString());
...
// roleRepository.save(user.getRole());
return userRepository.save(user);
}
2) adding methods
Role findByRole(Integer role); to RoleRepository
public Role getRole(Integer role) {
return roleRepository.findByRole(role);
} to UserService
3) replacing the new Role with the role lookup in UserController.create:
// Role newRole = new Role();
// newRole.setRole(role);
Role newRole = service.getRole(role);
Cheers,
John
using codebase to a tomcat maven build there appears to be ajax related issues on any type of form submission using your js code. Appears to only work on chrome base browsers. (well having said that only used ff and chromium and it was not working in ff) Didnt bother investigating the underlying issues though. Does work smoothly in chromium.
ReplyDeleteHello!
ReplyDeleteWhen I've imported source code on my local PC using Spring Tool Suite, there're errors on jquery-1.6.4.min.js