Search code examples
javamodel-view-controllerstruts2architectural-patterns

Which files are responsible specifically for Model, View and Controller in struts2 MVC framework?


My MVC concept is not clear.

It seems me that .xml files are for Modeling; .jsp files are for Viewing; .java files are for Controlling. This is actually represents MVC system.

I have the working basic concepts on MVC formula but I am not clear on which files are responsible specifically?


Solution

  • First of all, dive into Wikipedia.

    Your conception of MVC is wrong, and so is dividing concepts using technologies (.xml, .jsp, .java...);

    enter image description here

    • The Model contains the data (WHAT is displayed);
    • The View renders the data to the user (HOW it is displayed);
    • The Controller manipulates the data, both automatically or based on user's interaction (WHY it is displayed).

    Then, when you've clear what MVC is, try to understand how Struts2 implements MVC:

    • the JSP file is the View;
    • the Action is the Controller (and part of the Model);
    • the Model is whatever you use to carry the data (Bean, POJO, Map, String, etc...), unless you explicitly define a model object through ModelDriven (but don't), in which case that object is the Model.

    EDIT: as suggested by Dave, it is worth mentioning that, when talking of Actions, we're also implying the StrutsPrepareAndExecuteFilter (responsible of which Action to call) and the Interceptor Stack (through which every Action must pass before - and after - a result execution).