I am building a webservice with playframework 2 and extjs4. Until today I have managed to overcome any obstacle. But this is something I can’t deal with alone. I followed tutorial, but it didn’t help me. Well, at first it did. I know it worked, I was able to add some values to the database (mongodb). I doubt if it is important, but:
- everything worked fine, I added some test data into db
- I wrote some other methods in Java
- I used
db.Category.remove()on my mongo collection - server is unable to receive any values from form now
I have a Ext.FormPanel with 2 fields:
items: [
{
id: 'categoryName',
fieldLabel: 'Category name',
name: 'categoryName',
allowBlank: false
},{
xtype: 'textarea',
id: 'categoryDescription',
fieldLabel: 'Description',
name: 'categoryDescription',
allowBlank: true
}
]
Corresponding Model class looks like that:
@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;
}
}
And finally, here’s where I want to receive values:
Form<CategoryModel> categoryForm = form(CategoryModel.class);
System.out.println(categoryForm);
Output from Play console:
Form(of=class kunto.models.CategoryModel, data={}, value=None, errors={})
Firebug shows that POST values are being sent correctly:
categoryDescription test
categoryName test
And I have POST request type in conf/routes defined on the server side for this particular request.
I have 2 questions:
- what to do to fix it?
- more importantly, why did it happen?
EDIT
As @adis suggested:
Form<CategoryModel> categoryForm = form(CategoryModel.class).bindFromRequest();
System.out.println(categoryForm);
System.out.println(categoryForm.data());
outputs:
Form(of=class kunto.models.CategoryModel, data={categoryName=test, categoryDescription=test}, value=Some({"categoryName":null,"categoryDescription":null}), errors={})
{categoryName=test, categoryDescription=test}
I also checked
CategoryModel categoryModel = form(CategoryModel.class).bindFromRequest().get();
System.out.println(categoryModel);
it outputs: {"categoryName":null,"categoryDescription":null}
Anyone can explain this?
As commented you have to bind the request to a model by:
This will allow you do also checks on the properties in you model if you have defined them, like: @Contraints.Required.
After the binding, you get the form object and
getthe model instance:This should give you an instance of your model populated with the data from your post reqeust. Usually this method looks like:
Hope this helps.