In MVC paradigm you can perfectly have a controller in the application scope (such as a Servlet
already by default is), the representative class only should not contain fields which are associated with request scoped variables. You should declare them in the method block.
Thus not for example
public class Controller {
private SomeObject field; // Not threadsafe as this will be shared among all requests!
public void control(Request request, Response response) {
this.field = request.getSomething();
}
}
but more so
public class Controller {
public void control(Request request, Response response) {
SomeObject field = request.getSomething(); // Threadsafe.
}
}
The View
and the associated Action
should be handled threadlocal, i.e. declared in the method block. E.g.
public class Controller {
public void control(Request request, Response response) {
View view = new View(request, response);
Action action = ActionFactory.getAction(request);
action.execute(view);
view.navigate();
}
}
The Model
is to be handled in the Action
class, also in the threadlocal scope.
public class SomeAction implements Action {
public void execute(View view) {
Model model = new Model();
// ...
}
}
In the JSF context you in fact only have the Model
class which is in essence nothing more than a backing bean. The Controller
and View
parts are already handled by JSF with help of FacesServlet
controlling the request/response/lifecycle/actions and the UIViewRoot
which is built up from JSF pages. Thus, for request scoped data your JSF bean should be request scoped.