Created
October 21, 2014 19:29
-
-
Save guilhermesilveira/1b9034d903297fcf4085 to your computer and use it in GitHub Desktop.
Data Binding MVC example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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