简体   繁体   English

使用mongodb的Apache Wicket表单

[英]Apache Wicket forms using mongodb

I have been trying to create a basic form in Eclipse using Apache Wicket. 我一直在尝试使用Apache Wicket在Eclipse中创建基本表单。 Used quickstart maven to setup the project. 使用Quickstart Maven设置项目。 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. 以及HTML代码。

<!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. 现在,所有这些在tomcat服务器中正常运行,并在控制台窗口中显示输出。

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. 我为此使用mongoDb。 So within the program, i setup a JDBC driver, wrote the code in a separate class for it. 因此,在程序中,我设置了JDBC驱动程序,并为此在单独的类中编写了代码。

Am not able to find out a way on how i can direct all my inputs to my local mongoDB database. 我无法找到有关如何将所有输入定向到本地mongoDB数据库的方法。 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/ . 您可能要检查https://docs.mongodb.org/getting-started/java/ MongoDB is not JDBC compliant. MongoDB不兼容JDBC。 It has its own driver and APIs. 它具有自己的驱动程序和API。 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. https://docs.mongodb.org/ecosystem/drivers/java/上有一些库被列为POJO映射器,使处理BSON对象更容易。

Have fun! 玩得开心!

We have a very nice with http://jongo.org/ Its very nice library that wraps MongoDB driver and provides very nice syntax. 我们对http://jongo.org/非常满意。它的非常漂亮的库包装了MongoDB驱动程序并提供了非常好的语法。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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