Repel invalid requests
Command objects
Model Objects
A Web App Framework WEB4J
Strings into Integer, BigDecimal, and so on
These operations will vary depending on the framework you're using.
Example
This example is taken from an application built with the WEB4J framework.
/** Build a Model Object from request parameters. */ public final class RestoAction extends ActionTemplateListAndEdit { public static final RequestParameter RESTO_ID = RequestParameter.withLengthCheck("Id"); public static final RequestParameter NAME = RequestParameter.withLengthCheck("Name"); public static final RequestParameter LOCATION = RequestParameter.withLengthCheck("Location"); public static final RequestParameter PRICE = RequestParameter.withLengthCheck("Price"); public static final RequestParameter COMMENT = RequestParameter.withLengthCheck("Comment"); protected void validateUserInput() { try { ModelFromRequest builder = new ModelFromRequest(getRequestParser()); fResto = builder.build(Resto.class, RESTO_ID, NAME, LOCATION, PRICE, COMMENT); } catch (ModelCtorException ex){ //displays error message to end user addError(ex); } } //..many items elided //target Model Object, built from user input private Resto fResto; }In this case, the parsing of raw HTTP request parameters is shared between these classes:
RequestParameter
represents the raw underlying parameters accepted by the ActionRequestParser
defines default policies for translating single request parameters into String, Date,
Boolean, Integer, and BigDecimal objects, and Collections thereof.ModelFromRequest
translates sets of request parameters into Model Objects. While a RequestParser returns single "building block"
objects (Integer, Date, etc.), ModelFromRequest goes a step further,
and returns an entire Model Object, which more or less encapsulates a
number of "building block" objects.Validation and parsing of HTTP request parameters are related topics.
See the Repel invalid requests topic for further information.