3

I have a Java application that I am implementing following the MVC pattern.

I want to have a window (view) that contains a form which allows the user to add several text fields. There will also be a button that will allow the user to add more text-fields to the form.

I have several questions about this.

Will the code for adding new text-fields be considered part of the controller or the view?

  1. If controller, then this controller will not do anything to the model, it will just modify my view, so is this a violation of MVC?
  2. If view, then will it be a violation of MVC as the changes in view were not directed from the model?

2 Answers 2

7

A couple of things here:

  • There are several ways of implementing MVC, each technology and kind of app has it. See this great article to have a better understanding.

  • An action that involves only VC is not a violation, not every user action has to touch the three parts of MVC.

    1. The code that will add new text-fields will be controllers, which add a view (textfield) and should add a model representing what that textfield means in your app (if the view alone has any meaning then this VC-only relationship is ok).

    2. Changes in the view can come from two places: from the controller or from the model itself BUT by notifications (this means that the model must not know the views directly, but have a group of listeners that listens his changes).

I hope this answer plus the article clarifies a little bit more your knowledge of MVC and its implementation in your app.

Good luck!

-3

Modifying the page by adding extra fields has nothing to do with MVC.

You will be adding elements to the DOM with jQuery/JavaScript. This is simple presentation logic and it'll be a part of your view.

5
  • 1
    lol :D you know that Java and Javascript are different languages right?
    – Ankit
    Apr 29, 2012 at 4:31
  • Button for allowing user to modify DOM by adding an input of type text is done by JavaScript or libraries such as jQuery.
    – CodeART
    Apr 29, 2012 at 8:35
  • @Ankit Maybe you didn't understand my answer? Adding extra fields to the form doesn't need to make a round trip. You would add few extra elements through jQuery or plain javascript on the client side. Once you are satisfied, you will submit the entire form with all the new fields to your controller. Question was do I do 'X' or 'Y'. I'm saying that you don't need to do 'X' or 'Y' - it's a client side logic and it should have nothing to do with controller or a view model.
    – CodeART
    Nov 7, 2013 at 15:37
  • 1
    I think you misunderstood. My front-end was in Java. I do not have a web interface. So, no HTML or javascript.
    – Ankit
    Nov 7, 2013 at 23:55
  • In that case my answer is irrelevant as I've assumed that you are referring to an MVC application in a Java context, i.e. servlets, JSPs, etc.
    – CodeART
    Nov 8, 2013 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.