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.
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.
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.
DispatcherServlet is the front controller in Spring Web MVC. Incoming requests for the HTML file are forwarded to the DispatcherServlet.
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 .
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).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With