The basic Action input contract:
The action Input provides methods to get values from the input as Object, int, float, double, boolean, etc. It performs the necessary conversions and also have versions that return a default value. It has methods to return properties (remoteAddr, method, authType, etc.) of the http request (through reflection) and the http headers. You can also use the method keys() to list all keys in the action input. The default implementation is RequestInput. Below are some usage example:
// get a String from the this input... String username = input.getString("username"); // get the code as an int or return 100 if it is not present in the input... int code = input.getInt("code", 100); // get a boolean from the input... boolean accepted = input.getBoolean("accepted", false); // checkbox: get an array of all values with they key "interest" int[] interestsAsInt = input.getInts("interest"); String[] interestsAsString = input.getStrings("interest"); // get any object in the action input MyObject obj = (MyObject) input.getValue("myObject"); // list all keys and values Iterator<String> iter = input.keys(); while(iter.hasNext()) { String key = iter.next(); Object value = input.getValue(key); System.out.println("Key: " + key + " / Value: " + value.toString()); } // check if input has a value for the given key boolean hasKey = input.hasValue("code"); // get a header from this input String agent = input.getHeader("User-Agent"); // get a property from this input String ip = input.getProperty("remoteAddr"); // IP of the request...
Getting and populating any object with the values from the Action input with just one method call:
Let's say you have all the properties of an User object in the action input. You can get one by one to instantiate the User object or you can just call the getObject() method:
// Instantiate and populate (by reflection) the User object with any properties present in the action input User user = input.getObject(User.class); // You can also inject the properties into an already instantiated object, // which is useful for when you are updating some properties User user = userDAO.loadById(userId); input.inject(user); userDAO.update(user);
The basic action Output contract:
The action Output provides works pretty much like a map, where you can set any value by a String key. This output is later rendered to the client somehow, by a JSP page, a velocity template, a JSON builder, etc.
output.setValue("username", username); output.setValue("user", user); boolean isEmpty = output.isEmpty(); // list all keys and values Iterator<String> iter = output.keys(); while(iter.hasNext()) { String key = iter.next(); Object value = output.getValue(key); System.out.println("Key: " + key + " / Value: " + value.toString()); }
Setting all the properties of an object in the action output with just one method call:
Let's say you have an User object that you want to return as a result from your action. You can place the whole object in the output and let the view decide what to do with its properties OR you can extract and place each of its properties in the action output with just one method call. That will be useful when you have an html form and you want to display all the properties of the user in the form.
User user = userDAO.loadById(userId); output.setValue("theUser", user); // OR output.setObject(user); // that will extract all the properties and place them in the output for the view