how to create an html form for a model in playfram

2019-08-12 05:29发布

问题:

In my application ,I have to present the user with an address input form.I have an Address model as given below.Do I need to create an html page from scratch to capture the user input,Or can I generate a form similar to the crud page in admin area?Which is the correct way to do this in playframework?

public class Address extends Model {    
    @Required
    String addressLine1;

    String addressLine2;

    String city;

    String state;

    String pincode;

    String phoneNumber;

    String country;
...
}

回答1:

Using CRUD module to capture user input is easier and quicker way. CRUD Module exists for such cases. Regarding whether this is the correct way, it depends. It you have lot of customization then you can overide the crud templates and it should work.

Just create a Addresses Controller extending CRUD. It would be available as part of /admin/. Thats it.



回答2:

You can reuse the CRUD tags without the CRUD controllers!

#{crud.form class:'models.ModelName' /} 
#{curd.form object:anyInstance /} 

Please see my answer Advanced customization of CRUD forms and controllers in Play for more details.

PS.

This question was already asked but the accepted question was not satisfying for me, and I wanted to make it into a more generic question. So I asked it again but, still not getting any answer!, I finally found the solution myself :)