Binding a Bean to your Handler Method - geetools/geemvc GitHub Wiki

The Java 8 MVC Framework geeMVC also lets you bind beans to your handler method, which is particularly useful when working with forms. geeMVC will also populate and validate the beans for you if the appropriate request parameters and validation annotations exist.

Notice how in the following example we have not annotated the parameter AccountForm accountForm. geeMVC will recognize automatically that the object is a bean and instantiate it for you. If request parameters exist with the name beanName.propertyName, geeMVC will map and populate the matching bean properties accordingly. In the following example such a field or property could be accountForm.forename.

package com.example;

@Controller
@Request("/accounts")
public class AccountController {

    @Request("form")
    public String showForm() {
        return "view: account/form";
    }

    @Request(path = "add", onError = "account/form")
    public String createAccount(AccountForm accountForm) {

        // Form bean is processed and saved to database ...

        return "view: account/success";
    }
}

To keep this example simple, we will not add any validation yet, but this is what the accountForm could look like:

package com.example;

// Java bean for holding our account data. Check http://www.javatpoint.com/java-bean
// for more information on Java beans.
public class AccountForm {
    protected String username = null;
    protected String password = null;
    protected String forename = null;
    protected String surname = null;
    protected String email = null;
    protected int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getForename() {
        return forename;
    }

    public void setForename(String forename) {
        this.forename = forename;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Your JSP or template would look like the following if you wanted to directly populate your bean with geeMVC:

<!-- /WEB-INF/jsp/pages/account/form.jsp -->

<h3>Please enter your username and password:</h3>

<!-- Simple form with 2 fields. -->
<form action="/accounts/add">
	<!-- geeMVC will automatically populate these for you in the form bean. -->
	Username: <input type="text" name="accountForm.username" /><br/>
	Password: <input type="password" name="accountForm.password" /><br/>
	<input type="submit" value="Send form!" />
</form>

Note that we are not using the geeMVC taglibs yet. We will come to that later in the taglibs section and in "Working with Forms".


Find out how to bind and automatically populate JPA beans in the next section.

⚠️ **GitHub.com Fallback** ⚠️