I’ve a Java webapp with these frameworks and I want to know if my implementation meets with MVC pattern:
Controller Layer (V)
I’m using JSF
@ManagedBean
public class Controller{
@ManagedProperty("#{business}")
private Business business;
public void insert(){
business.insert();
}
}
Business Layer (C)
I’m using Spring
public interface Business{
public void insert();
}
@Service("business")
public class BusinessImpl implements Business{
@Autowired
private DaoMapper mapper;
@Override
@Transactional
public void insert(){
mapper.insert();
}
}
Data Layer (M)
I’m using MyBatis (This java interface is associated to XML file of MyBatis)
public interface DaoMapper{
public void insert();
}
My layers implemented MVC pattern? I’m confused :S…
Your code specifically implementing MVC pattern then No but is it actually MVC, well that answer is YES.
As Bart van Ingen Schenau points out the data as well as behavior to retrieve the appropriate data are encompassed by the model. This is correct but the important thing to remember about JSF is that the framework itself is inherently MVC.
JSF Model
The managed bean and Spring injected business logic encapsulates the model.
JSF View
Your XHTML markup represents the View components. This markup also contains Controller hooks that bind your Model (Managed Beans, ORM entities, etc…) to the View components.
JSF Controller
This is just FacesServlet. It is the controller that creates the interaction between View and Model so that the web developer doesn’t need to worry about this.
5
No, your layers, as described, do not implement the MVC pattern.
Within the MVC pattern, the Model contains most of the application-specific logic, in particular the business logic and all the layers below it. So, in your three-layer structure, that would be both the Business and the Data layers.
The View contains the code for driving the the user interface. The View classes produce the HTML, JSON or XML that gets displayed by the browser.
The Controller classes tie it all together. They receive the requests from the browser, and instruct the Model and View to take the relevant actions based on that request.
2