Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?
share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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!

share|improve this answer
add comment

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.

share|improve this answer
1  
lol :D you know that Java and Javascript are different languages right? –  Ankit Apr 29 '12 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 '12 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 '13 at 15:37
    
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 '13 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 '13 at 9:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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