1

playframework 2 と extjs4 で Web サービスを構築しています。今日まで、どんな困難も乗り越えてきました。しかし、これは私一人では対処できないことです。チュートリアルに従いましたが、役に立ちませんでした。まあ、最初はそうでした。私はそれがうまくいったことを知っています.データベース(mongodb)にいくつかの値を追加することができました. それが重要かどうかは疑問ですが、

  • すべてが正常に機能しました。いくつかのテストデータをdbに追加しました
  • 私はJavaでいくつかの他のメソッドを書きました
  • db.Category.remove()モンゴコレクションで使用しました
  • サーバーは現在、フォームから値を受け取ることができません

私はExt.FormPanel2つのフィールドを持っています:

items: [
    { 
        id: 'categoryName',
        fieldLabel: 'Category name', 
        name: 'categoryName', 
        allowBlank: false 
    },{
        xtype: 'textarea',
        id: 'categoryDescription',
        fieldLabel: 'Description',
        name: 'categoryDescription',
        allowBlank: true
    }
]

対応Modelするクラスは次のようになります。

@MongoCollection(name = "Category")
public class CategoryModel{
    private String categoryName;
    private String categoryDescription;

    @JsonProperty("categoryName")
    public String getName() {
        return categoryName;
    }

    @JsonProperty("categoryName")
    public void setName(String name) {
        this.categoryName = name;
    }

    @JsonProperty("categoryDescription")
    public String getDescription() {
        return categoryDescription;
    }

    @JsonProperty("categoryDescription")
    public void setDescription(String description) {
        this.categoryDescription = description;
    }

    public String toString(){
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(this);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return json;
    }
}

そして最後に、値を受け取りたい場所は次のとおりです。

Form<CategoryModel> categoryForm = form(CategoryModel.class);
System.out.println(categoryForm);

Play コンソールからの出力:

Form(of=class kunto.models.CategoryModel, data={}, value=None, errors={})

Firebug は、POST 値が正しく送信されていることを示しています。

categoryDescription test
categoryName    test

そしてPOST、この特定のリクエストに対してサーバー側で定義された conf/routes にリクエストタイプがあります。

2 つの質問があります。

  • それを修正するにはどうすればよいですか?
  • もっと重要なのは、なぜそれが起こったのですか?

EDIT @adisが示唆したように:

Form<CategoryModel> categoryForm = form(CategoryModel.class).bindFromRequest();
System.out.println(categoryForm);
System.out.println(categoryForm.data());

出力:

Form(of=class kunto.models.CategoryModel, data={categoryName=test, categoryDescription=test}, value=Some({"categoryName":null,"categoryDescription":null}), errors={})
{categoryName=test, categoryDescription=test}

私もチェックしました

CategoryModel categoryModel = form(CategoryModel.class).bindFromRequest().get();
System.out.println(categoryModel);

それは出力します:{"categoryName":null,"categoryDescription":null}

誰でもこれを説明できますか?

4

2 に答える 2

1

コメントしたように、次の方法でリクエストをモデルにバインドする必要があります。

 Form<CategoryModel> categoryForm = form(CategoryModel.class).bindFromRequest();

これにより、@Contraints.Required のように定義している場合、モデル内のプロパティのチェックも行うことができます。

バインド後、フォーム オブジェクトとgetモデル インスタンスを取得します。

CategoryModel newCatModel = categoryForm.get();

これにより、投稿リクエストからのデータが取り込まれたモデルのインスタンスが得られます。通常、このメソッドは次のようになります。

Form<Task> taskForm = form(Task.class).bindFromRequest();

if (taskForm.hasErrors()) {  // check for constraints
    Logger.info("Error:" + taskForm);
    return badRequest(views.html.task.create.render(taskForm));
}
// here get the model instance
Task newTask = taskForm.get();

お役に立てれば。

于 2012-05-03T19:44:57.623 に答える
0

推測ですが、ゲッターの代わりにプロパティに注釈を付けるべきではありませんか?

@JsonProperty("description")
public void setDescription(String description) {
    this.categoryDescription = description;
}

する必要があります

@JsonProperty("description")
private String description;

上記のコードにも矛盾があります。説明のゲッターは「categoryDe​​scription」にバインドされ、セッターは「説明」にバインドされています

于 2012-05-06T10:36:15.300 に答える