简体   繁体   中英

Apache Wicket forms using mongodb

I have been trying to create a basic form in Eclipse using Apache Wicket. Used quickstart maven to setup the project. I started off with two form fields, name and gender.

import org.apache.wicket.markup.html.WebPage;
import java.util.*;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.markup.html.form.*;

public class WelcomePage extends WebPage {

private static final long serialVersionUID = -5223126205489216801L;

private List<String> genderChoices = new ArrayList<String>();

public WelcomePage(){
    genderChoices.add("Male");
    genderChoices.add("Female");
    final USerModel uSerModel = new USerModel();

    Form<?> form = new Form("form");

    TextField<String> text = new TextField<String>("text", new PropertyModel<String>(uSerModel, "name"));

    DropDownChoice<String> gender = new DropDownChoice<String>("gender", new PropertyModel<String>(uSerModel, "gender"),genderChoices);

    Button button = new Button("submit"){

        @Override
        public void onSubmit() {
            super.onSubmit();

            System.out.println("Name :"+ uSerModel.getName());
            System.out.println("Gender :"+ uSerModel.getGender());

        }
    };  

    add(form);

    form.add(text);
    form.add(gender);
    form.add(button);

}

}

And with the HTML code too.

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
</head>
<body>
<form wicket:id="form">

    <input type="text" wicket:id="text" /><br />
    <select wicket:id="gender">
        <option></option>
    </select><br />
    <input type="submit" wicket:id="submit" />


</form>


</body>
</html>

Now, all these are working fine running in the tomcat server and displaying the output in the console window.

The issue came up when i had to enter the values in the form and the values were to be stored in a local database. I am using mongoDb for this purpose. So within the program, i setup a JDBC driver, wrote the code in a separate class for it.

Am not able to find out a way on how i can direct all my inputs to my local mongoDB database. Am a total beginner at both of these technologies. A little hand would be great. Thank you.

You may want to check https://docs.mongodb.org/getting-started/java/ . MongoDB is not JDBC compliant. It has its own driver and APIs. There are libraries like the ones listed as POJO mappers at https://docs.mongodb.org/ecosystem/drivers/java/ which make it easier to deal with BSON objects.

Have fun!

We have a very nice with http://jongo.org/ Its very nice library that wraps MongoDB driver and provides very nice syntax.

DB db = new MongoClient().getDB("dbname");

Jongo jongo = new Jongo(db);
MongoCollection friends = jongo.getCollection("friends");

MongoCursor<Friend> all = friends.find("{name: 'Joe'}").as(Friend.class);
Friend one = friends.findOne("{name: 'Joe'}").as(Friend.class);

Friend joe = new Friend("Joe", 27);
friends.save(joe);
joe.age = 28;
friends.save(joe);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM