Skip to content

Instantly share code, notes, and snippets.

@guilhermesilveira
Created October 21, 2014 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermesilveira/1b9034d903297fcf4085 to your computer and use it in GitHub Desktop.
Save guilhermesilveira/1b9034d903297fcf4085 to your computer and use it in GitHub Desktop.
Data Binding MVC example
package br.com.caelum.mvc;
public class ClientsController {
// reflection based conversion is NOT safe
public void save1(Client client) {
}
// primitives are safe
// no annotations required in java8
public void save2(String name, int age) {
}
// primitives are safe
// annotations required prior to java8
public void save3(@Param int age, @Param String name) {
}
// primitives are safe
public void save4(int i) {
}
// white listing is safe
public void save5(Form form) {
form = form.keep("name", "age");
String name = form.extract("name");
}
// white listing with converters is safe
public void save6(Form form) {
form = form.keep("name", "age")
.with(new IntConverter());
String name = form.extract("name");
}
// white listed reflection construction is safe
public void save7(Form form) {
// early setting, good citizen
Client constructed = form.keep("name", "age").create(Client.class);
}
// white listed reflection setting is safe
public void save8(Form form) {
// late setting, either via setters or (ugh!) accessing private fields
Client set = form.keep("name", "age").fill(new Client());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment