Search code examples
postplayframeworkgetput

Understanding web services : post()/put()/get()/delete()


I am trying to understand how web-services work and I think I need some help with my controller.
For example, I am trying to add a user into my data base ...
This is what I have:

 public static Result addUser(){

        DynamicForm form = Form.form().bindFromRequest(); 
        String url = "http://my-url-qqq";

        WSResponse response;
        WSRequestHolder holder = WS.url(url);

        holder.setHeader("Cookie", "sessionid="+ session("sessionid")); 
        Map<String,String> anyData = new HashMap();
        JsonNode content = response.asJson();

        // how can i put all this things togeter 
        //to put the elements from my form in 
        //my database ... ?
        //and what is the role of all the pieces ?

        return ok(index.render("Bello! Now you can log in!"));
    }

And I have this model:

@Entity
public class registerForm extends Model {


    //for registration
 @Id
    public String Id;
    public String username;
    public String first_name;
    public String last_name;
    public String password1;
    public String re_password1;
    ....
}

routes:

  GET     /register                    controllers.Application.register()
  POST    /register                    controllers.Application.addUser() 

and my html form:

    <form action="@routes.Application.addUser()" method="POST">    
        <div class="col-md-offset-1 col-md-4">
        <h3><b>Register : </b></h3>
        <br>
        Username :
        <input type="input" name="username" class="form-control"><br>
        First Name :
        <input type="input" name="first_name" class="form-control"><br>
        Last Name :
        <input type="input" name="last_name" class="form-control"><br>
         Email :
        <input type="input" name="email" class="form-control"><br>
        Password:
        <input type="password" name="password" class="form-control"><br>
        Repeat Password : 
        <input type="password" name="re_password" class="form-control"><br>
        <input type="submit"  class="btn">
        <br><br><br><br>
       <h2> ^_^ : have fun .</h2>
     </div>
     </form>

Can anyone explain/translate how to connect this things ? I'll appreciate any kind of example ...


Solution

  • First dont use DynamicForm when your form has same structure like your entity class means DynamicForm are used when For ex. if you want to search user from database then your form will have only one field in that case you can use DynamicForm where you can search from predefined entity field.If your form have same field like your entity fields

    Second I think you misunderstood entity i.e. entity is a POJO(Plane Old Java Object) your class represent a table in database and your entity name is registrationforn and I think thats not look good you should name your entity like User or Member.This is completely optional for you but it gives better understanding

    To save data do

    public static Result addUser(){
    
            registerForm user = Form.form(registerForm.class).bindFromRequest().get; 
            user.save();   //and the data is saved
            return ok(index.render("Hello! Now you can log in!"));
        }
    

    And delete, find entity etc check Play Ebean Sample Application.