Attila The Framework is a simple Controller-View -framework that is built on top of the Servlet specification.
It is designed to be lightweight and easy to use.
public class FooController extends AttilaTheController{ public void index(HttpServletRequest request, HttpServletResponse response){ } }
<servlet> <servlet-name>FooController</servlet> <servlet-class>com.example.FooController</servlet-class> </servlet> <servlet-mapping> <servlet-name>FooController</servlet-name> <url-pattern>/foo/*</url-pattern> </servlet-mapping>
SampleView view = new SampleView() view.render(request, response);
Views come in two flavors: ad-hoc views and concrete views. If you have a simple view that doesn't need a complex setup or is only used in one place you should use ad-hoc views for rendering your pages. If your view is complex to setup, contains lot of data and you feel a need to encapsulate it to a neat class then you should use concrete views.
When you create an ad-hoc view, you just create an instance of one of Attila's generic views, set the Tiles2 layout or a jsp-page, set the data (optional) and render the view.
@HTTPMethod public void create(HttpServletRequest request, HttpServletResponse response){ GenericJSPCompositeViewable view = new GenericJSPCompositeViewable("user.create"); view.render(request, response); }
public class SampleView extends JSPViewable{ public SampleView(){ setJspPage("/view/index.jsp"); } }
public void setUsername(String username){ this.username = username; } public String getUsername(){ return username; }
<p>Hello ${username}!</p>
NOTE! Paths to your public JSPs go as follows:
Path in your project: ${project}/variants/${variant}/public/view/index.jsp
Path in a deployed app: /view/index.jsp
Use @Authentication-annotation to make controllers or actions authenticable by Attila.
HINT! You can exclude actions from authentication process by marking an action with @Authentication(checkAuthentication=false)-annotation
Before you can use the authentication mechanism, you have to:
@Authentication public class StoryController extends AttilaTheController { @Override public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} @HTTPMethod(method=HttpMethodEnum.POST) public void createnew(int themeId, HttpServletRequest request, HttpServletResponse response) throws IOException{ } }
Attila will check user's authentication status on index() but not on createnew().
@Authentication public class StoryController extends AttilaTheController { @Override public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} @Authentication(checkAuthentication=false) @HTTPMethod(method=HttpMethodEnum.POST) public void createnew(int themeId, HttpServletRequest request, HttpServletResponse response) throws IOException{ } }
There are situations when some users should be allowed to access an action and some users should not. The usual way is to get the user model instance of the signed in user and then check if she has the correct usage rights. This usually ends up causing multiple nested if's and duplication of the same code everywhere. Attila's solution for simplifying and encapsulating this process is @AuthLevel-annotation and AuthenticationLevel-interface.
To use this feature you need to implement AuthenticationLevel and assign it to an action or a controller with @AuthLevel.
Below is a simplistic example of an AuthenticationLevel-implementation:
import attila.core.auth.*; public class AdminLevel implements AuthenticationLevel { @Override public boolean checkUserAuthenticationLevel(AttilaTheUser attilaUser) { User user = (User) attilaUser; if(user.getId() == 1){ return true; }else{ return false; } } }
Example of using the above implementation to restrict access to an action only for one user:
@AuthLevel(level=AdminLevel.class,errorController=DashboardController.class) @HTTPMethod(method=HttpMethodEnum.POST) public void createnew(HttpServletRequest request, HttpServletResponse response) throws IOException{ }
NOTE! Using @AuthLevel alone does not enable authentication process. @AuthLevel is only an instruction for the framework to be used in the authentication process. You must always use @Authentication in addition to @AuthLevel if you want to enable finer grained access control.
The biggest advantage of Attila over the usual servlet programming is Attila's ability to map URLs to methods radically increasing readability and maintainability of the application. Attila also supports passing parameters to methods provided they're simple types. Currently Attila supports the following types in actions:
http://example.com/[controller]/[action]/[param1]/[param2]/[nth-param]
http://example.com/foo/bar/24 maps to:
public void bar(int b, HttpServletRequest request, HttpServletResponse response){}
Since r180 Attila supports overloaded actions with certain limitations. The biggest limitation is that Attila differs between two different actions by the amount of parameters they take in. So you are not able to use two actions that take same amount of parameters but of different types (resolving the requested action would require guesswork that would make the framework unpredictable).
Below is an example of valid overloading:
@HTTPMethod public void createnew(HttpServletRequest request, HttpServletResponse response) throws IOException{ } @HTTPMethod public void createnew(String username, HttpServletRequest request, HttpServletResponse response) throws IOException{ }
public class FooForm extends SimpleForm{}
private Field username; public FooForm(){ username = new SimpleField("username", new StringRule(), "username fail"); addField(username); }
public void add(HttpServletRequest request, HttpServletResponse response){ Form fooForm = new FooForm(); if(fooForm.validate(request)){ //do something } }
Read parameters safely from requests by using attila.helper.RequestHelper and its static methods. For reading from Cookies, use attila.helper.CookieHelper
Import taglib to each of your JSPs where you intend to use them:
<%@ taglib uri="/WEB-INF/taglib.tld" prefix="atf" %>