In order to read request paremeters you should use method parameter or the @Parameter annotation.
The following example illustrates how you can read simple properties in your logic.
<html> <form action="person.add.logic" method="get"> Name: <input name="name"/><br/> Address: <input name="address"/><br/> Preferred number: <input name="preferredNumber"/><br/> <input type="submit"/> </form> </html>
package org.vraptor.examples.first; @Component public class PersonLogic { @Parameter private String name, address; @Parameter private int preferredNumber; public void add() { System.out.println("Adding " + name + ", " + address + ", " + preferredNumber + " to the database!"); } }
VRaptor will set your field's values by reading the parameters name, address and preferredNumber.
The approach mentioned earlier is quite complex if it comes to read loads of values. It is much easier to create a Person in your logic and work with that person by using a path as:
person.name, person.address and person.preferredNumber
<html> <form action="person.add.logic" method="get"> Name: <input name="person.name"/><br/> Address: <input name="person.address"/><br/> Preferred number: <input name="person.preferredNumber"/><br/> <input type="submit"/> </form> </html>
You can use method parameter injection in order to read the desired fields:
@Component public class PersonLogic { public void add(Person p) { System.out.println("Adding ", p.getName()); } }
Note that VRaptor uses the type name person, and not the variable name: p.
You could also use @Parameter in your logic. So the same example with @Parameter is:
@Component public class PersonLogic { @Parameter private Person person = new Person(); public void add() { System.out.println("Adding ", person.getName()); } }
Most frameworks have problems while trying to fill POJO's lists. The following example shows how you can simulate this problem in this and other frameworks while the next section shows an elegant solution.
<html> <form action="person.add.logic" method="get"> Name: <input name="persons[0].name"/><br/> Address: <input name="persons[0].address"/><br/> Preferred number: <input name="persons[0].preferredNumber"/><br/> <br/> Name: <input name="persons[1].name"/><br/> Address: <input name="persons[1].address"/><br/> Preferred number: <input name="persons[1].preferredNumber"/><br/> <br/> <input type="submit"/> </form> </html>
@Component public class PersonLogic { @Parameter private List<Person> persons; public PersonLogic() { // ugly... typical solution... try avoiding it persons = new ArrayList(Person); persons.add(new Person()); persons.add(new Person()); } public void add() { System.out.printf("Adding %s", persons.toString()); } }
The solution is to tell VRaptor that it should dynamically create the objects that it needs in order to fill the field...
Note: it will use ArrayList and an internal Set as the default implementation for List and Set.
@Component public class PersonLogic { @Parameter(create=true) private List<Person> persons; public void add() { System.out.printf("Adding %s", persons.toString()); } }
Do you see? There is nothing ugly in the code above... it simply instantiates objects as needed.
You can read a full tutorial on reading parameters lists here.
Be careful when using create=true as you might expose to many setters to be used with request parameters!