1. Guillermo Szeliga
  2. specter

Wiki

Clone wiki

specter / controllers

Controllers

In order to register a class as a controller, the first thing to do is to add the annotation Svnadmin at class level. This annotations tells the framework at runtime that the current class contains a group of more or one handlers and model attributes. By default, the controller will be registered with an id generated from the class name, but, if we don't want that behavior, there's a chance to specified a custom id by using the attribute name of the annotation mentioned before, like the following example:

@Controller(name="records")
public class RecordsController 
{
}

Once we've got registered the controller, the following step is to start defining model attributes. The attributes can be declared using the annotation @ModelAttr, which contains two main properties:

  1. qualifier: Contains a string from which the framework will register the attribute under. In case no value is specified, the default value will be the actual property name.
  2. scope: As usual, a model attribute could be registered within two scopes: request, where it will exist as long as the current request, or session, where the attribute will live as long as the user session in the server.

Following with the previous sample, several model attributes could be declared as follows:

@Controller(name="records")
public class RecordsController 
{
	@ModelAttr(qualifier="user", scope=AttributeScopeType.SESSION)
	private User _user;
	
	@ModelAttr(qualifier="style")
	private Integer _style;
	
	@ModelAttr(qualifier="result")
	private List<Record> _lstRecords = new ArrayList<Record>();

}

Finally, after declaring model attributes, the final step would be to declare hadlers. This components are declared making use of annotation @HttpHandlerMethod which contains two properties:

  1. name: String that will be used to register the handler within the controller, so that it'll will be access by the form {context}/controller/handler. In case is left blank, then the default value will be the related method name.
  2. errorHandlerName: In case of exceptions during execution, an error handler that matches the specified name will be searched, and if any error handler is found, then will be executed the default one.

Ending the previous sample, as you can see below, you'll find that a handler under the name of list was declared (no name attribute specified), and then we've got a reference to an error handler called listErrors, declared afterwards by the use of the annotation @FlowErrorHandler

@Controller(name="records")
public class RecordsController 
{
	@ModelAttr(qualifier="user", scope=AttributeScopeType.SESSION)
	private User _user;
	
	@ModelAttr(qualifier="style")
	private Integer _style;
	
	@ModelAttr(qualifier="result")
	private List<Record> _lstRecords = new ArrayList<Record>();

	@Messages
	private MessageBasket _message;

	@HttpHandlerMethod(errorHandlerName="listErrors")
	public String list()
	{
		if(_style != null)
		{
			if(_style >= Style.values().length)
			{
				_message.addError(new ErrorMessage("record.style.not.exist"));
			}
			else
			{
				_lstRecords = _mapRecords.get(Style.values()[_style]);
			}
		}
		
		return "records/showList";
	}
	
	
	@FlowErrorHandler(name="listErrors")
	public String listErrorsHandler()
	{
		return "records/showList";
	}
}

As we can see, there's a particular component annotated as messages. This component has the particularity that is where the framework is going to check if there were any problems during the execution, so it's important that, whenever a problem occurs, it must be registered using this component, which consists of an instance of MessageBasket provided by the framework, along with the related information (as could be an unique id or a descriptive message), so that the framework, when it finds that something went wrong during the execution of a handler, it delivers control to the referenced error handler.

Updated