Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

naming conventions of Controller and Model Java classes in Spring / MVC webapp?

Let's say I have "settings" classes in my controllers and models, in my Java/Spring/MVC webapp. Now, in both /controllers/ and /models/, should I...

1) ...name them both Settings.java?

2) ...name them SettingsController.java and SettingsModel.java or something similar?

I'm curious about typical naming conventions as well as naming conventions used in your personal experience that worked well. Feel free to reference your own webapp project in place of my, perhaps poor, example.

This is my first MVC webapp, and I'm trying to get a feel for it.

like image 738
Xonatron Avatar asked Jan 27 '12 19:01

Xonatron


People also ask

What is the naming convention for classes in Java?

In Java, class names generally should be nouns, in title case with the first letter of each separate word capitalized. and interface names generally should be adjectives, in title case with the first letter of each separate word capitalized.

What is model-view-controller in Spring MVC?

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.

Which is the front controller class of Spring MVC?

DispatcherServlet is the front controller in Spring Web MVC. Incoming requests for the HTML file are forwarded to the DispatcherServlet.

What are naming conventions for packages in Java?

Naming Conventions Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .


2 Answers

Since model classses describe 'real-life' entities it's better to call them by name, so in your case it would be Settings.java.

Controller, on the other hand, is just a byproduct of using specific architecture (MVC) so it gets the Controller suffix, and in your case becomes SettingsController.java.

If you did your application using jsf, for example, you could still have Settings.java as model, but SettingsBean.java as a controller (obviously that's just another convention and you could call it however you like).

like image 35
soulcheck Avatar answered Sep 19 '22 18:09

soulcheck


I'd suggest you to call your controller SettingsController and model just Settings. It is because model actually contains you data. What kind of data does your model contain? The answer is: settings. So, call it settings.

Controller is different story. It is a class that deals with you data. There are probably many classes that deal with settings: SettingsBuilder, SettingsFactory, SettingsUtil, SettingsService etc. This one is controller, so call it SettingsController.

like image 170
AlexR Avatar answered Sep 19 '22 18:09

AlexR