Let us take a basic example. In a servlet app I would have those classes:
Register(User)
. Would call the DAO.WEB-INF\JSPs\registration.jsp
which would take care of the view.All this made perfect sense to me and clearly separated the concerns.
I have tried since to wrap my head around the JSF/Facelets way of thinking. I have went through many excellent resources here in SO and otherplaces but there is not any place that addresses this simple question - namely how does the dao/service/controller/view pattern translate into JSF.
To continue my example - I have set up a JSF project in eclipse, have translated my DB schema to "entities"
and now I am left wondering - what kind of package, with what king of classes should I create to handle user Registration ? I understand I have to create xhtml pages for the view but where is the controller ?
Please do not point me to tutorials - I am aware of many, amongst which the @BalusC one - my problem is not to make this work but to understand the pattern. I have seen "session" packages containing "managed beans", "abstract facade" patterns etc etc but nothing that makes as clear sense as the good old servlets pattern.
So - among the "entities" created by the DB there is a User.java class that looks like the model to me. The view will be an xhtml. The controller ?
NB: this is not asking as so many duplicates here the differences between those technologies - I am asking about a clear translation of the very clear and intuitive controller/dao/service pattern to the JSF framework - or a clear statement that there is not such a translation.
See also:
MVC With Servlets and JSP To implement a web application based on MVC design pattern, we'll create the Student and StudentService classes – which will act as our Model layer. S tudentServlet class will act as a Controller, and for the presentation layer, we'll create student-record.jsp page.
In light of the MVC design pattern, the servlet acts as a controller and JSP as a view, whereas JSF is a complete MVC. As we already know, the servlet will need manual HTML tags in Java code.
In a Java context, the Model consists of simple Java classes, the Controller consists of servlets and the View consists of JSP pages. Here're some key features of the pattern: It separates the presentation layer from the business layer The Controller performs the action of invoking the Model and sending data to View
Part of the reason why it's often not entirely clear in JSF and many other web frameworks which parts of it correspond to which part of MVC, is that the MVC pattern was originally devised for desktop applications.
There are two levels of MVC in JSF 2:
@ManagedBean
, View = Facelets/xhtml, Controller = FacesServlet
if you look only at jsf@ManagedBean
, View = xhtml if you look at your application at a whole.The typical package layout in a JSF application is the same:
com.foo.domain.featurex.service
<- modelcom.foo.domain.featurex.model
<- modelcom.foo.presentation.featurex
<- controllerNot that big of a difference here except that your controller is now the FacesServlet
(the heart of JSF) together with your @ManagedBean/@Named
which would reside in a class like com.foo.presentation.featurex.Controller/Handler
or something like that.
Example for a simple registration:
// we talk about the big picture here, not about how jsf is built
// but how your app is built
// model
package com.foo.domain.registration.entity;
@Entity
public class User {
// fields
}
package com.foo.domain.registration.service;
// also model
@Stateless
public class RegistrationService {
@PersistenceContext
EntityManager em;
public void register(User u) {
em.persist(u);
}
}
package com.foo.presentation
// controller
@Named
@ViewScoped
public class RegistrationController {
@Inject
RegistrationService rs;
User current = new User();
public void register() {
rs.register(u);
}
// get set for current user
}
// view
// quite a lot boilerplate omitted
// form omitted which displays all the necessary field of your current user
// via value="#{registrationContoller.current.name}"
<h:commandButton value="submit" type="submit" action="#{registrationController.register}" />
It does not. They are conceptual different. MVC uses a MVC model, and jsf uses a handler based model.
The face of you page is done with the XHTML file. The handler handles the use input like submitting form. A binding exists between the form elements and the handler, normally using getters and setters. So if the user submits a form an method of you handler is called and all the submitted data is already in place for use to process (thank to the load of event phases).
So your XHTML file gives you username and password. The handler method is called. You do the lookup of the credentials and return a string, forwarding the user to a new page.
As said before, you cannot map the two technologies. It's like sending an email or sending a written letter. Yes, they transport information, and yes it is a relay messeging system. But the way how it is done is totally different. It is the same clash of paradigm when writing jsf and then switching to wicket.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With