Monday, 20 June 2016

How components are bind to HTML page ?


In wicket framework whatever we show on GUI using form tag, we generate the java object for each component and bind it with HTML page.

WelcomePage.html
<!DOCTYPE html>
<html xmlns="http://wicket.apache.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <form wicket:id="form">
     <input type="text" wicket:id="text"/> <br></br>
     <select wicket:id="gender">
        <option > </option>
     </select>
     <input type="submit" wicket:id="submit"/>
  </form>
</body>

</html>


Create the corresponding java class
WelcomePage.java
public class WelcomePage extends WebPage {
   private List<String> genderChoice = new ArrayList<String>();
   public WelcomePage() {
       genderChoice.add("male");
       genderChoice.add("female");
             
       final UserModel userModel = new UserModel();
       Form<?> form = new Form("form");
             
       TextField<String> textField = new TextField<String>("text"
                   new PropertyModel<String>(userModel, "name"));
       DropDownChoice<String> gender
           new DropDownChoice<String>("gender"
             new PropertyModel<String>(userModel, "gender") ,
                                       genderChoice);
       Button button = new Button("submit"){
           public void onSubmit() {
               super.onSubmit();
               System.out.println("Name: "+userModel.getName());
               System.out.println("Gender"+userModel.getGender());
           };
       };
             
       add(form);
             
       form.add(textField);
       form.add(gender);
       form.add(button);
   }
}

We have added here three form components TextField, DropDownChoice and Button and add the corresponding id in the html form.


WebApplication class MyApplication.java

public class MyApplication extends WebApplication {
     @Override
     public Class<? extends Page> getHomePage() {
         return WelcomePage.class;  // return default page
     }
}

UserModel.java (POJO)
public class UserModel { 
    private String name;
    private String gender;
      
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public String getGender() {
       return gender;
    }
    public void setGender(String gender) {
       this.gender = gender;
    }      
}


After executing the project we got the following form : 










After filling the input and click on Submit Query we got the fields value in the logs : 









Output
Name: Rahul
Gender: male



No comments:

Post a Comment

Note: only a member of this blog may post a comment.