Friday, March 1, 2013

Java MVC Forms With MongoDB And Morphia, part 1

I assume you have MongoDB up and running, and that you have a functional copy of NetBeans.

The first thing we need to do is download the necessary JAR files. You can find a MongoDB driver for Java at https://github.com/mongodb/mongo-java-driver/downloads. Get the most recent version. Morphia will be used to interact between Java and MongoDB. You can find the JARs at http://code.google.com/p/morphia/downloads/list. You will only need Morphia itself for this example. You will find life easier if you copy the two JARs to your JDK. In Windows, that will most likely be in C:\Program Files\Java. Go to your JDK folder, then put the JARs in the \jre\lib\ext subdirectory.

Now, start up NetBeans, and start a new project. Choose "Java Web" > "Web Application", then click Next. Give the project a name, then press Next. Choose the defaults on the next screen, then select the Spring MVC option on the one after. We will want JSTL. Now we're finished with that step.

Run the application as is. You should get a web page that says "Hello! This is the default welcome page for a Spring Web MVC project." as the first line. index.htm is the default home page; we will change that later.

The next step is creating the model class. In this example, we'll just be storing people's names, with the first and last names as separate fields. Create a Java class ("Java" > "Java class") called "Name" under a package called "controller". Add the following line between the "package" and "class" lines:

import com.google.code.morphia.annotations.*;

Next, put the following line right before the class declaration:

@Entity("names")

That annotation tags this class as a Morphia Entity. "names" is the name of the document collection. Now, start the class body as follows:

    @Id
    String id;
   
    @Serialized("firstName")
    String firstName;
   
    @Serialized("lastName")
    String lastName;


The @Id annotation is needed to designate the field as the document ID. The @Serialized annotations include those fields in the documents. They also determine their field names in the JSON that actually get stored.  Now, right-click on the code window, select "Insert Code", then select "getter and setter". Click on the class's checkbox and OK the change. Your code should now look like:

package controller;
/**
 *
 * @author Ross
 */
import com.google.code.morphia.annotations.*;

@Entity("names")
public class Name {
    @Id
    String id;
   
    @Serialized("firstName")
    String firstName;
   
    @Serialized("lastName")
    String lastName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}


Getters and setters are necessary for the JSP form to access this class correctly.
That's the first part. In the next entry, we will finish the Java code.



No comments:

Post a Comment